Can anybody help me with parsing a byte[] from a json file -
JSON File looks like this:
{
name:"test"
myImage:[67,78,....]
}
I do not want to create a new JSONObject() and parse, because this file can have multiple images and system runs out of memory.
I want to use GSON/Jackson/JSONReader(android>3.1), but it mostly seems to have getInt(), getString() objects.
How can i parse the byte[] itself into an object which i can later convert to a drawable.
I really appreciate any help.
What have you tried? In Jackson, you could use a POJO like:
class Stuff {
public String name;
public byte[] myImage;
}
Stuff stuff = new ObjectMapper().readValue(new File("stuff.json"), Stuff.class);
which should work.
But this JSON serialization is not the typical way to do it: it is more common to include byte array as base64 encoded data. This is how Jackson will serialize it as, if you use ObjectMapper.writeValue(...)
. The reason is that base64 encoding will be more compact, use less memory than number arrays.
I resolved it this way
InputStream instream = entity.getContent();
Gson gson = new Gson();
Reader reader = new InputStreamReader(instream);
JsonReader jsonReader = new JsonReader(jsonReader);
jsonReader.beginArray();
while (jsonReader.hasNext()) {
MyCustomObject cobj = gson.fromJson(jReader,
MyCustomObject.class);
byte[] image = cobj.Image;
}
JSONObject-> getString().getBytes();
GSON/JsonReader-> getString().getBytes();