julman99 / gson-fire

A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getters) serialization, pre/post processors and many others. Check out the documentation to learn how to use it!
http://gsonfire.io
Other
229 stars 37 forks source link

Reuse Objects #33

Closed cbedoya closed 6 years ago

cbedoya commented 7 years ago

I have an issue with the Gson library, and I found your library, which looks that can suit almost all my needs, except for this one, the possibility to reuse already parsed objects, for example, lets say you have this json:

{
    "data" : [
        {
            "id" : 1
            "type" : "student"
            "name" : "Student 1",
            "subjects" : [
                 {
                     "id": 1,
                     "type" : "subject"
                  }
             ]
        },
        {
            "id" : 2
            "type" : "student"
            "name" : "Student 2",
            "subjects" : [
                 {
                     "id": 1,
                     "type" : "subject",
                     "name" : "Math"
                 }
             ]
        },
        {
            "id" : 3
            "type" : "office"
            "name" : "student 3",
            "subjects" : [
                 {
                     "id": 1,
                     "type" : "subject",
                     "name" : "History"
                 },
                 {
                     "id": 2,
                     "type" : "subject",
                     "name" : "History"
                 }
             ]
        }
    ]
}

This is a very basic example, there are 3 students who assist to 0 or many classes. As you can see 2 or more students can have the same reference to an specific subject, but when I parse the object, Student 1 will have an object for subject 1, Student 2 will have a different object but with same information as the one for Student 1, so my question basically is, can this be achieved using the post-processors you have?

In theory what comes to mind is that before creating an object instance, there should be some kind of pool, so I look using the id and type, if I find the object I return it, otherwise I let the parser to create the instance for me. Do you have something for that case? Looks like its something between the Preprocessor and the Post-processor where you generate the actual Class.

I will appreciate any help or thoughts on this.

julman99 commented 7 years ago

Hello

I would suggest you implement a TypeAdapterFactory. This factory creates a type adapter that would check if the object being deserialized is already cached, and if not will use the original type adapter to deserialize it from json into an object and then cache it.

You need to be careful with memory usage, so your pool of objects needs to be composed of weak references or self clean after a while.

julman99 commented 7 years ago

Was my suggestion useful for you?

Thanks