java - How to deserialize an array of objects with type adapter with Gson? -


how deserialize array of objects type adapter gson?

package tests;  import java.io.ioexception; 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.jsonobject; import com.google.gson.jsonparseexception; import com.google.gson.typeadapter; import com.google.gson.stream.jsonreader; import com.google.gson.stream.jsonwriter;  public class try03 {      public static class b {          double value;          public b() {             this(0);         }          public b(double value) {             this.value = value;         }          double getvalue() {             return value;         }          void setvalue(double value) {             this.value = value;         }      }      public static class {          b[] values = new b[3];         transient double sum;          public a() {             sum = 0;         }          void refresh() {             sum = values[0].getvalue() + values[1].getvalue() + values[2].getvalue();         }          set(int index, b b) {             if( values[index] != null ) {                 sum -= values[index].getvalue();             }             values[index] = b;             if( values[index] != null ) {                 sum += values[index].getvalue();             }             return this;         }      }      public static class ades implements jsondeserializer<a> {          @override         public deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception {              jsonobject obj = json.getasjsonobject();              b[] values = context.deserialize(obj.get("values"), b[].class);              ans = new a();             ans.values = values;             ans.refresh();              return ans;         }      }      public static class aadap extends typeadapter<a> {          @override         public void write(jsonwriter out, value) throws ioexception {         }          @override         public read(jsonreader in) throws ioexception {              in.beginobject();              if( !in.nextname().equals("values") ) {                 throw new assertionerror();             }              in.beginarray();              // how read elements of type b?              in.endarray();              in.endobject();              ans = new a();              return ans;         }      }      static string[] bsrc = {              "{\n" +                      "\"value\": 12.0\n" +              "}",              "{\n" +                      "\"value\": 10.0\n" +              "}",              "{\n" +                      "\"value\": 7.0\n"  +              "}"       };      static string asrc = "{\n" +           "\"values\": [\n" +             "{\n" +               "\"value\": 32.0\n" +             "},\n" +             "{\n" +               "\"value\": 5.0\n" +             "},\n" +             "{\n" +               "\"value\": 8.0\n" +             "}\n" +           "]\n" +         "}";      // works     public static void main01() {          gson gson = new gsonbuilder().enablecomplexmapkeyserialization().serializenulls().setprettyprinting().setversion(1.0)                 .create();          b b = new b();         b.setvalue(12);          system.out.println(gson.tojson(b));      }      // works     public static void main02() {          gson gson = new gsonbuilder().enablecomplexmapkeyserialization().serializenulls().setprettyprinting().setversion(1.0)                 .create();          b b = gson.fromjson(bsrc[0], b.class);          system.out.println(b);      }      // works     public static void main03() {          gson gson = new gsonbuilder().enablecomplexmapkeyserialization().serializenulls().setprettyprinting().setversion(1.0)                 .create();          b[] b = { new b(32), new b(5), new b(8) };         a = new a();         a.set(0, b[0]).set(1, b[1]).set(2, b[2]);          system.out.println(gson.tojson(a));      }      // works not initialize transient field     public static void main04() {          gson gson = new gsonbuilder().enablecomplexmapkeyserialization().serializenulls().setprettyprinting().setversion(1.0)                 .create();          a = gson.fromjson(asrc, a.class);          system.out.println(a);      }       // works , initialize transient field     // not use typeadapter     public static void main05() {          gson gson = new gsonbuilder().enablecomplexmapkeyserialization().serializenulls().setprettyprinting().setversion(1.0)                 .registertypeadapter(a.class, new ades())                 .create();          a = gson.fromjson(asrc, a.class);          system.out.println(a);      }      // don't know how write type adapter     public static void main06() {          gson gson = new gsonbuilder().enablecomplexmapkeyserialization().serializenulls().setprettyprinting().setversion(1.0)                 .registertypeadapter(a.class, new aadap())                 .create();          a = gson.fromjson(asrc, a.class);          system.out.println(a);      }       public static void main(string[] args) {         // main01();         // main02();         // main03();         // main04();         // main05();         main06();     } } 


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -