tidwall / sjson

Set JSON values very quickly in Go
MIT License
2.44k stars 167 forks source link

Fix for missing escape characters for backslashes #25

Closed thirstycoda closed 5 years ago

thirstycoda commented 5 years ago

Hi,

I had a problem with SetBytes where setting a value to a UNC path with the backslashes escaped such as "\\\\127.0.0.1" produces a JSON document with missing slashes, i.e. "\\127.0.0.1" which once escaped is \127.0.0.1 This was found by using gjson.GetBytes to grab a UNC path and use sjson.SetBytes to write it to a different JSON document. The result was a different number of backslashes between the original document and new document. I also noticed that if the original value is instead "\\\\127.0.01\r" then setBytes produces the correct result.

The fix seems to be to change mustMarshalString so it returns true when a backslash is found as opposed to only if its found at the end of the string. I couldn't think why you would want to marshal the string when the backslash is at the end but not marshal if it appears anywhere else. Please let me know if you can think of a scenario

The change did break a unit test but the unit test seems to be trying to create a field with the name ":\1" which is not a valid JSON name as the \ character is an escape character and can only be followed by ", \, /, b, n, r, t or u.

thirstycoda commented 5 years ago

Just realized there is already a pull request to fix this issue. Feel free to reject this

tidwall commented 5 years ago

Thanks for alerting me to the issue. I merging the previous pull request.