Dan Snow Dan Snow - 3 months ago 8
Java Question

Json incomprehensible length increase

It's may be a dumb question but i don't know why the searilization of an object to json increase the size factor by at least 4.

for exemple I have a byte array with these carateristics

54351 length before serialization
198464 length after serialization


Edit:
code

byte b[] = Files.readAllBytes(imgFile.toPath());
System.out.println("size pic " +b.length);
String json = gson.toJson(b);
System.out.println("size pic json " +json.length());

Answer

Well, let's take an example: the array [123, -77]. This is an array of 2 bytes. It thus takes 2 bytes in memory. Transforming it to JSON produces the following string: "[123,-77]". That makes 9 characters: [, 1, 2, 3, ,, etc. The 4-times factor thus looks pretty normal to me.