I’ve got a class written in Java and the same class written in C#. I’m serializing the C# class into a json string and I am trying to deserialize it on the Java side.
It all went perfectly fine until I’ve added a byte[] field to both classes.
here are the classes definitions:
C#:
public class RegisterRequest : GenericRequest
{
public string name { set; get; }
public string sex { set; get; }
public string birthday { set; get; }
public string from { set; get; }
public string about { set; get; }
public byte[] image { set; get; }
}
Java:
public class RegisterRequest extends GenericRequest{
private String name;
private String sex;
private String birthday;
private String from;
private String about;
private String pictureUrl;
private byte[] image;
}
The serialization on the C# side uses: request.ToJson() (Json.NET)
and the Java deserialization uses: RegisterRequest rr = gsonObject.fromJson(msg, RegisterRequest.class); (using Gson. the msg is the json string)
When I don’t send anything in the byte array it still works. But when I do fill the array I get an exception on the Java side: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 159089
I’m guessing that there needs to be something that marks the begining of an array which Gson identifies but Json.NET doesn’t adds to the string?
Given the piece of input from a comment above
and looking at how Gson serializes
byte[]the answer is obvious: Gson uses a json array while json.net uses a json string. You must change one or the other. Writing a custom serializer for gson is easy (although I’ve never tried with
byte[]), the same probably holds for the other tool.