Describe the bugJSONGet does not produce the correct output for Cyrillic text.
To Reproduce
Steps to reproduce the behavior:
Use JSONSet to write a structure with string fields. Fill the fields with Unicode characters.
Use res, err := JSONGet() to read the structure.
Convert res into []byte either manually (res.[]byte), or using redigo.Bytes(res).
Unmarshall the []byte result via json.Unmarshall() into the structure.
Compare values you have written with values json.Unmarshal produced from JSONGet result.
New structure will contain fields with different (seemingly random) characters.
Expected behavior
Fields in first structure (which we have written) and the second one (which was read) should match.
Additional context
The problem I found lies within rjs.StringToBytes function, which is called from JSONGet. There are the following lines (_lst is a string, by is []byte) :
for _, s := range _lst {
by = append(by, byte(s))
}
Here, s is a rune, which is an alias for int32. When we convert it into byte, we loose all but the least significant byte. Fix is pretty straightforward, we just need to convert string into []byte directly, without looping over each rune:
by = []byte(_lst)
I've copied JSONGet in my own code and applied this fix, and my Unicode problem was solved.
Describe the bug
JSONGet
does not produce the correct output for Cyrillic text.To Reproduce Steps to reproduce the behavior:
JSONSet
to write a structure withstring
fields. Fill the fields with Unicode characters.res, err := JSONGet()
to read the structure.res
into[]byte
either manually (res.[]byte
), or usingredigo.Bytes(res)
.[]byte
result viajson.Unmarshall()
into the structure.json.Unmarshal
produced fromJSONGet
result.Expected behavior Fields in first structure (which we have written) and the second one (which was read) should match.
Additional context The problem I found lies within
rjs.StringToBytes
function, which is called fromJSONGet
. There are the following lines (_lst
is astring
,by
is[]byte
) :Here,
s
is arune
, which is an alias forint32
. When we convert it intobyte
, we loose all but the least significant byte. Fix is pretty straightforward, we just need to convertstring
into[]byte
directly, without looping over eachrune
:I've copied
JSONGet
in my own code and applied this fix, and my Unicode problem was solved.