java - How to do Custom Serialization using GSON? -


i have case use gson custom serialization feature.

class student{    public string name;    public int rollnumber;     public student(string name, int rollnumber){         this.name = name;         this.rollnumber = rollnumber;    }     public int getrollnumber(){        return this.rollnumber;    }     public string getname(){        return this.name;    }   } class school{      public student[] students;      public school(student[] students){           this.students = students;     }      public students[] getstudents(){        return this.students;    }  } 

now when do

private static final gson gson = new gsonbuilder().disablehtmlescaping().create(); 

student[] students = new student[2];

students[0] = new student("sam", 1);  students[1] = new student("tom", 2);  school school = new school(students);  gson.tojson(school); 

i output this:

[{"name":"sam","rollnumer":1},{"name":"tom","rollnumer":2}] 

but want :

["student":{"name":"sam","rollnumer":1},"student":{"name":"tom","rollnumer":2}] 

how achieve using gson custom serialization?

i have tried this , this. did not much.

this

["student":{"name":"sam","rollnumer":1},"student":{"name":"tom","rollnumer":2}] 

is not valid json (you can verify using online tool jsonlint). see details json specification:

definition of object:

an object unordered set of name/value pairs. object begins { (left brace) , ends } (right brace). each name followed : (colon) , name/value pairs separated , (comma).

definition of array:

an array ordered collection of values. array begins [ (left bracket) , ends ] (right bracket). values separated , (comma).

definition of value:

a value can string in double quotes, or number, or true or false or null, or object or array. these structures can nested.

your output defines json array, objects in array aren't surrounded braces. correct representation this:

[{"student":{"name":"sam","rollnumer":1}}, {"student":{"name":"tom","rollnumer":2}}] 

which can generated simple gson typeadapter:

class studentadapter extends typeadapter<student> {      @override     public void write(final jsonwriter writer, final student student)             throws ioexception {         if (student == null) {             writer.nullvalue();             return;         }          writer.beginobject();         writer.name("student");         writer.beginobject();         writer.name("name");         writer.value(student.getname());         writer.name("rollnumber");         writer.value(student.getrollnumber());         writer.endobject();         writer.endobject();     }      @override     public student read(final jsonreader reader) throws ioexception {         if (reader.peek() == jsontoken.null) {             reader.nextnull();             return null;         }          final student student = new student();         reader.beginobject();         reader.nextname(); // discard 'student' wrapper property         reader.beginobject();         while (reader.hasnext()) {             final string attrname = reader.nextname();             if ("name".equals(attrname)) {                 student.setname(reader.nextstring());             } else if ("rollnumber".equals(attrname)) {                 student.setrollnumber(reader.nextint());             }         }         reader.endobject();         reader.endobject();          return student;     } } 

test method:

@test public void testwriteschoolsasjsonwithgsonandcustomoutput()         throws exception {     final gson gson = new gsonbuilder().registertypeadapter(student.class,             new studentadapter()).create();      student[] students = new student[2];     students[0] = new student("sam", 1);     students[1] = new student("tom", 2);      school school = new school(students);      final string outputjson = gson.tojson(school);     system.out.println(outputjson);      school = gson.fromjson(outputjson, school.class);     system.out.println(school); } 

and 'relevant' output:

{"students":[{"student":{"name":"sam","rollnumber":1}},{"student":{"name":"tom","rollnumber":2}}]} 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -