tdewolff / minify

Go minifiers for web formats

Home Page:https://go.tacodewolff.nl/minify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

invalid character '\b' in string literal

RampantDespair opened this issue · comments

This is somewhat of a continuation to #685.

The ast tree produced by minify is unable to be parsed by json for the following 3 objects:

The code hasn't changed:

s.bodyParsed = strings.Split(s.bodyParsed, `.ReactDOMrender(`)[1]
s.bodyParsed = strings.Split(s.bodyParsed, `,document.getElementById(`)[0]
s.bodyParsed = fmt.Sprintf("(%s)", s.bodyParsed)

reader := strings.NewReader(s.bodyParsed)
input := parse.NewInput(reader)
astTree, err := js.Parse(input, js.Options{})
if err != nil {
	log.Printf("Failed to parse input (%v) -> %s", input, err)
	return
}

jsonBody, err := astTree.JSONString()
if err != nil {
	Debug("Failed to convert astTree to jsonBody (%s) -> %s", astTree, err) // <---- ERROR THROWN
}
if debug {
	os.WriteFile("../test.json", []byte(jsonBody), OS_ALL_RWX)
}

var bodyJson model.PageData
err = json.Unmarshal([]byte(jsonBody), &bodyJson)
if err != nil {
	log.Fatal(err)
}

They work fine for me, are you using the latest code from master (not just the latest version)? Are you sure you don't end up with body with twice the parenthesis (e.g. (({object})))?

@tdewolff
Yes I am sure that I am not ending up with double parenthesis because out of the 1001 objects I am parsing, it's the same 3 that fail with that error message.

That being said, I am new to golang so I'm not sure whether I am using the latest code and not version?
I ran this go get -u github.com/tdewolff/minify/v2 after you updated it on March 11th.
And I have this in my go.mod:

github.com/tdewolff/minify/v2 v2.20.19
github.com/tdewolff/parse/v2 v2.7.13-0.20240416152224-b5d42d60b215

Please try again, I've published a new version including the fix.

@tdewolff
With:

github.com/tdewolff/minify/v2 v2.20.20
github.com/tdewolff/parse/v2 v2.7.13

I just tested gist.github.com/RampantDespair/33dda35da104737596d46bfbdb656a23 on my local machine (instead of my vps) and am still getting the same error...

@tdewolff
None of the 3 work actually, using your latest version v2.7.13.
I just retested all 3, using the same code as above:

func TestUnmarshal(t *testing.T) {
	data, _ := os.ReadFile("object01.json") // and 02 - 03
	s := fmt.Sprintf("(%s)", string(data))
	log.Print(s)

	reader := strings.NewReader(s)
	input := parse.NewInput(reader)
	astTree, err := js.Parse(input, js.Options{})
	if err != nil {
		console.ErrorLite("Failed to parse input (%v) -> %s", input, color.FgYellow(err))
		return
	}

	jsonBody, err := astTree.JSONString()
	if err != nil {
		console.ErrorLite("Failed to convert astTree (%v) -> %s", astTree, color.FgYellow(err))
		return
	}
	os.WriteFile("unity.json", []byte(jsonBody), utils.OS_ALL_RWX)

	var bodyJson model.UnityPageData
	err = json.Unmarshal([]byte(jsonBody), &bodyJson)
	if err != nil {
		console.ErrorLite("Failed to unmarshal jsonBody (%v) -> %s", jsonBody, color.FgYellow(err))
		return
	}
}

@tdewolff
It fails at the unmarshal part

At the unmarshal part? Perhaps your model does not correspond to the JSON data, but that would not be a problem of this library.

I noticed that you did add the parentheses twice, removing the fmt.Sprintf("(%s)", string(data)) part fixed it since the JSON object files already contain parentheses. The code you posted works fine for me!

I have given you a lot of help and information in this issue and you should be able to fix your code from hereon yourself, good luck! ;-)

Oh yeah you've helped me a bunch and I am extremely grateful for that, thank you.

For anyone who would stumbles upon this issue while facing the same, the solution for me was to clean the input string using:
stringsx.

The final code being:

data, _ := os.ReadFile("object01.json") // and 02 - 03
s := fmt.Sprintf("(%s)", string(data))
s = stringsx.Clean(s)

reader := strings.NewReader(s)
input := parse.NewInput(reader)
astTree, err := js.Parse(input, js.Options{})
if err != nil {
	console.ErrorLite("Failed to parse input (%v) -> %s", input, color.FgYellow(err))
	return
}

jsonBody, err := astTree.JSONString()
if err != nil {
	console.ErrorLite("Failed to convert astTree (%v) -> %s", astTree, color.FgYellow(err))
	return
}
os.WriteFile("unity.json", []byte(jsonBody), utils.OS_ALL_RWX)

var bodyJson model.UnityPageData
err = json.Unmarshal([]byte(jsonBody), &bodyJson)
if err != nil {
	console.ErrorLite("Failed to unmarshal jsonBody (%v) -> %s", jsonBody, color.FgYellow(err))
	return
}

The issue was strictly related to the fact that control characters were somehow present at the time of unmarshaling.