Gson.toJson gives StackOverFlowError, how to get proper json in this case? (public static class)

后端 未结 1 707
深忆病人
深忆病人 2021-01-06 12:38

I currently have the following class:

 static final class TabInfo{
   public final String tag;
   public final Class clss;
   public Bundle args;

          


        
1条回答
  •  被撕碎了的回忆
    2021-01-06 13:17

    As I suggested the "change from class" to string,
    I allow myself for the sake of our readers to answer here for the first part:
    Don't use Class as a field, but use String that will contain the full class name
    TabInfo should look like:

    static final class TabInfo{
       public final String tag;
       public final String clss;
       public Bundle args;
    
       TabInfo(String _tag, Class _class, Bundle _args) {
           tag = _tag;
           clss = _class.getName();
           args = _args;
       }
     }
    




    Regarding 2nd part:
    I don't know what Bundle class is - please provide info,
    as I had to change a bit and write my own class for experiment.

    Person class is:

    public class Person implements Serializable {
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public Person() {
    
        }
    
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        private String name;
        private int age;
    }
    

    Main class for checking is:

    public class Main {
        public static void main(String[] args) {
            Type type = new TypeToken>>(){}.getType();
            Gson gson = new Gson();
            HashMap> map = new HashMap>();
            map.put("yair", new Stack());
            map.get("yair").add(new Person("Yair",36));
            String str = gson.toJson(map,type);
            System.out.println(str);
            map = gson.fromJson(str,type);
            String str2 = gson.toJson(map,type);
            System.out.println(str2);
    
        }
    }
    

    Feel free to run it, you will see both str and str2 are printed just fine.

    Update
    I checked the Bundle class, and saw that it contains too much information (In my humble opinion) to be a simple arguments collection.
    I don't see why in the above question a simple collection cannot be used instead.
    Serialization should contain as minimal data as possible
    (think about cases in which you take a serialized data and store it on some storage device, or send it over the network). So unless Bundle provides you a special functionality that a collection doesn't - don't use it.
    As a rule of thumb remember you cannot serialize everything with Json - there are limitations (recursive data-types for example) - so yes, there will be cases where you will have to translate from one type to a JSON-serializable type.
    This pattern is also used in other cases, I suggest you read more about data transfer objects

    0 讨论(0)
提交回复
热议问题