I have to deserialize a Json object in Android with a huge Int Json array inside.
The schema is like:
{
"key1": "value1",
"key2": "value2",
"smallObject": {
"key": "value"
},
"smallArray" [
{}, {} ,{}
],
"hugeObject": {
"data": {
"hugeIntArray": [
-1, -1, 1, 2, -1 ...
]
},
"key": "value"
}
}
The length of hugeIntArray array can be over 2,000,000, and making JSON file larger than 15 MB.
I tried to parse with Gson, which will take 7 seconds, also Moshi, which will take 11 seconds.
I found Moshi can do lazy loading with customize JsonAdapter and sequence. But I can't apply it to my schema.
My target is to use it like a sequence or stream to convert the int values into pixels of a image.
Is there any way you can avoid using Json in this case? The data should really be in a binary format
Have you tried JsonReader? It's specifically made to read json files as a stream
Related
I have a very large json file within my android app with the following struture:
[
{
"id": 123,
"data": {
...
}
},
{
"id": 456,
"data": {
...
}
},
...
]
Depending on the item selected in the UI, I need to deserialize only the item with the corresponding id, but right now I'm deserializing the whole file and then do the filter, which takes time and memory to just extract the id I need.
Is there a way to deserialize only the section I need?
I'm using kotlinx.serialization.
The JSON result for getUsers I get from the server looks like this:
{
"result": [
{
"meta": {
"rows": "3"
}
},
{
"items": [
{
"id": "1",
"name": "Steve",
"age": "30"
},
{
"id": "2",
"name": "Mary",
"age": "29"
},
{
"id": "3",
"name": "Bill",
"age": "58"
}
]
}
]
}
How can I deserialize it by GSON in my android app (I'm using retrofit)?
I can't imagine any wrapper classes because of the different object types in result.
Any help would be appreciated!
For good example
Converting JSON to Java
Other way, you can convert your json to a java object
Please use org.json library http://www.json.org/java/index.html
Then, for example
json = new JSONObject(YOUR_JSON).getJSONObject("result");
JSONArray items = data.getJSONArray("items");
String name = items.getJSONObject(0).getString("name");
You can write a TypeAdapter for a type that will be (de)serialized to(from) array. You can even make it generic, so it will work with type like Pair<A, B>. Here is an example for non-generic type: https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java — it (de)serializes Entry to(from array).
Disclaimer — I have not written nor tested that code, but it seems legit.
If you only encounter such problem once (like in your example), you may not bother making it generic, just write TypeAdapter for your specific pair of 2 different classes. The reading code is quite straightforward:
in.beginArray();
SomeClass1 info1 = gson.getAdapter(SomeClass1.class).read(in);
SomeClass2 info2 = gson.getAdapter(SomeClass2.class).read(in);
in.endArray();
return new SomeContainerClass(info1, info2);
(see https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java#L52)
I have been trying to wrap my head around how to parse nested objects and arrays with GSON, still stuck. How can I parse the nested items listed in the "results" array?
{
"item": {
"results": [
{
"__metadata": {
"url": "google.com",
"type": "website"
},
"listed": true,
"market": 225,
"town": "Toronto"
},
{
"__metadata": {
"url": "twitter.com",
"type": "website"
},
"listed": true,
"market": 225,
"town": "Calgary"
}
]
}
}
How can I easily do this with GSON within Android?
Thank you!
Android Studio
Download Plugin "GsonFormat"
Create your model class
Open Code->Generate->Gson
Paste your json click ok - if json is valid then it will convert the following json to java class (pojo)
Now create Gson object
Gson gson=new Gson();
Convert Json to java object
T obj = gson.fromJson(contents, tClass);
Now use this object "obj" to get values
I'm loading JSON with a completely dynamic field from an API using Retrofit on Android.
This is an example:
{
"success": true,
"messages": {
"success": [
"SUCCESS_MESSAGE"
],
"error": {
"specific_error": {
…
}
}
},
"session": {
"id": "fj4qewqrewq43fdsa",
"expire": null,
"YII_CSRF_TOKEN": "fda432fdafasd78978fdas"
},
"metadata": {…}
}
The structure of the messages object can change with every request. Sometimes there is the success field and sometimes not. Same goes for the error field. The error field especially can be an array but also an object with several other objects and fields inside.
Does it make sense to deserialise the messages field into different POJOs and if so how would I do that? Would it make more sense to keep the field as a JSON object and get values from the object when they are needed?
Just as the title says.. Is it possible to do this? Say I have a JSON array of something like this
{
"locations": [
{
"id": 1,
"loc": "miami"
},
{
"id": 2,
"loc": "manhattan"
},
{
"id": 3,
"loc": "lasVegas"
}
]
}
Sure, extend BaseAdapter and use the JSONArray as your backing model. However, it would be better (but by no means necessary) to convert it to a more "natural" representation in Java and use that instead.
you can convert the json to a java object. and bind the data.