Gson - Setting object reference on-the-fly using InstanceCreator

后端 未结 2 1146
渐次进展
渐次进展 2020-12-18 09:27

I\'m stuck in a problem where I need to set reference of my object being parsed to its child objects during deserialization using Gson and InstanceCreator

2条回答
  •  隐瞒了意图╮
    2020-12-18 10:18

    Problem:

    Currently, the workshift field inside Visit turns out to be null when deserialized.

    Solution:

    workshift field is a transient member in Visit class and transient members won't be serialized that's why you are getting null value when deserialized.

    To solve this problem you have to set the reference of workshift in the visit class manually via calling its setter method after getting workshift object when deserialized.

    when deserialized you have reference of both the object workshift and visit. Just need to pass the reference of workshift to visit will solve it.

    Visit.java:

    public class Visit {
        private final transient Workshift workshift;
    
        public Visit() {
    
        }
    
        public Workshift getWorkshift() {
            return workshift;
        }
    
        public void setWorkshift(Workshift workshift) {
            this.workshift = workshift;
        }
    
    }
    

    Use JsonDeserializer to set the reference of workshift into visit class.

    Sample Code:

    import java.io.IOException;
    import java.io.Serializable;
    import java.lang.reflect.Type;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonParseException;
    
        public class GSON {
    
            /**
             * @param args
             * @throws IOException
             * @throws ClassNotFoundException
             */
            public static void main(String[] args) throws Exception {
                // serialize
                Gson gson = new Gson();
                String json = gson.toJson(new Workshift());
                System.out.println("Workshift JSON:" + json);
    
                // deserialize
                GsonBuilder builder = new GsonBuilder();
                builder.registerTypeAdapter(Workshift.class, new WorkshiftDeserializer());
    
                Workshift workshift = builder.create().fromJson(json, Workshift.class);
                System.out.println("Reference of Workshift from Visit:"
                        + workshift.getVisit().getWorkshift());
    
            }
    
        }
    
        class Workshift implements Serializable {
            private Visit visit;
    
            public Workshift() {
                this.visit = new Visit(this);
            }
    
            public Visit getVisit() {
                return visit;
            }
    
            public void setVisit(Visit visit) {
                this.visit = visit;
            }
    
        }
    
        class Visit implements Serializable {
            private transient Workshift workshift;
    
            public Visit() {
    
            }
    
            public Visit(Workshift ws) {
                this.workshift = ws;
            }
    
            public Workshift getWorkshift() {
                return workshift;
            }
    
            public void setWorkshift(Workshift workshift) {
                this.workshift = workshift;
            }
    
        }
    
    
        class WorkshiftDeserializer implements JsonDeserializer {
            public Workshift deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
    
                Gson gson = new Gson();
    
                Workshift workshift = gson.fromJson(json, Workshift.class);
                workshift.getVisit().setWorkshift(workshift);
                return workshift;
            }
        }
    

提交回复
热议问题