K
K
korvin22022-03-12 15:11:49
Java
korvin2, 2022-03-12 15:11:49

How to convert very large json to java object?

I get json from the backend and translate it into an object using Gson. Like this:

public void saveData(JsonObject data) {
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()).create();
    Structure structure = gson.fromJson(data.get("structure").toString(), Structure.class);
    //работаю дальше со structure 
}

But sometimes the api returns a very large json (about 50MB) which causes an out of memory exception.
What structure looks like:
@Parcel(value = Parcel.Serialization.BEAN, analyze = {Structure.class})
public class Structure extends RealmObject {

@SerializedName("id")
@PrimaryKey
@Expose
@Index
public int id;

@SerializedName("date")
@Expose
public String date;

@SerializedName("notes")
@Expose
@ParcelPropertyConverter(RealmListParcelConverter.class)
public RealmList<StructureNote> notes;

//другие поля

The main problem with the " notes " field is that it contains about 80% of the entire json file that I get from the api.
How can such a large json be converted into a Java object? I've been thinking about splitting the notes into several parts and then just filling in the list one by one, but I don't know if that's even possible or how.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
korvin2, 2022-03-17
@korvin2

This article helped https://proandroiddev.com/retrofitting-and-rxjavin...

S
Sergey Gornostaev, 2022-03-12
@sergey-gornostaev

Gson, like Jackson, has a Streaming API specifically for such cases. True, you have to forget about converting JSON to an object and work with individual fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question