Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing base64 encoded byte arrays return different results in 2 library versions #574

Closed
ruhaim opened this issue May 21, 2015 · 4 comments

Comments

@ruhaim
Copy link

ruhaim commented May 21, 2015

  JObject j = JObject.Parse("{'responseArray':'AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA'}");

  byte[] r = j["responseArray"].ToObject<byte[]>(JsonSerializer.Create());

  string s = Convert.ToBase64String(r);

Deserializing the above code in v6.0.8.18111 gives errorneous result

but the same works properly in v4.5.6.14930

NOTE - the encoded string is a byte array having values 1|0 of length 24
v6.0.8.18111 takes this erroneously as an array of length 17

@xanatos
Copy link

xanatos commented May 21, 2015

I think that the breaking change happened between v6.0.3 - v6.0.4

@dbc2
Copy link

dbc2 commented May 21, 2015

The issue is the base 64 string in question, 'AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA', also happens to be parsable as a Guid in JsonReader.ReadAsBytesInternal():

        if (s.Length == 0)
        {
            data = new byte[0];
        }
        else if (ConvertUtils.TryConvertGuid(s, out g))
        {
            data = g.ToByteArray();
        }
        else
        {
            data = Convert.FromBase64String(s);
        }

This short-circuits the base 64 conversion.

For an analysis of the problem and workaround, see here: https://stackoverflow.com/questions/30369274/deserializing-base64-encoded-byte-arrays-return-different-results-in-2-json-net/30384841#30384841

@JamesNK
Copy link
Owner

JamesNK commented Jun 7, 2015

I can't think of a nice work around.

Edit: I could make it so that a GUID has to have dashes.

@JamesNK
Copy link
Owner

JamesNK commented Jun 10, 2015

Fixed.

GUIDs have to be the 00000000-0000-0000-0000-000000000000 format which will prevent base64 content accidentally being converted to a GUID before being converted to bytes.

@JamesNK JamesNK closed this as completed Jun 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants