2012-06-14 15 views
25

Chciałbym serializacji ten obiekt do JSON StringSerializować java obiekt z GSON

public class Person { 
    public String id; 
    public String name; 
    public Person parent; 
} 

i uzyskania efektu takiego:

{id: 1, name: "Joe", parent: 2} 

Próbowałem użyć

Person p = new Person(1, "Joe", new Person(2, "Mike")); 
Gson gson = new GsonBuilder() 
      .registerTypeAdapter(Persona.class, new PersonSerializer()).create(); 
String str = gson.toJson(p); 

ale zamiast tego dostałem:

"1" 

PersonSerializer:

public class PersonSerializer implements JsonSerializer<Person> { 
    public JsonElement serialize(Person src, Type typeOfSrc, ...) { 
     return new JsonPrimitive(src.id); 
    } 
} 

proszę wszelkie sugestie są mile widziane

Dzięki Mario

+0

http://stackoverflow.com/q/7085545/1815624 i http://stackoverflow.com/q/10067441/1815624 związane były w moim przypadku, ale było http://stackoverflow.com/questions/10067441/gson-converts-to-linkedhashmap-instead-of-my-object#comment62206167_33561747 naprawiono to – CrandellWS

Odpowiedz

55

Aby uzyskać wynik pragnienie, trzeba napisać serializatora tak:

public static class PersonSerializer implements JsonSerializer<Person> { 
    public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) { 
     JsonObject result = new JsonObject(); 
     result.add("id", new JsonPrimitive(person.getId())); 
     result.add("name", new JsonPrimitive(person.getName())); 
     Person parent = person.getParent(); 
     if (parent != null) { 
      result.add("parent", new JsonPrimitive(parent.getId())); 
     } 
     return result; 
    } 
} 

Wynik dla

Person p = new Person(1, "Joe", new Person(2, "Mike")); 
    com.google.gson.Gson gson = new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer()) 
      .create(); 
    System.out.println(gson.toJson(p)); 

będzie

{"id":1,"name":"Joe","parent":2} 

Kompletny kod:

import java.lang.reflect.Type; 

import com.google.gson.GsonBuilder; 
import com.google.gson.JsonElement; 
import com.google.gson.JsonObject; 
import com.google.gson.JsonPrimitive; 
import com.google.gson.JsonSerializationContext; 
import com.google.gson.JsonSerializer; 

public class GsonSimpleTest { 

    public static class Person { 
     public int id; 
     public String name; 
     public Person parent; 

     public Person(final int id, final String name) { 
      super(); 
      this.id = id; 
      this.name = name; 
     } 

     public Person(final int id, final String name, final Person parent) { 
      super(); 
      this.id = id; 
      this.name = name; 
      this.parent = parent; 
     } 

     public int getId() { 
      return id; 
     } 

     public void setId(final int id) { 
      this.id = id; 
     } 

     public String getName() { 
      return name; 
     } 

     public void setName(final String name) { 
      this.name = name; 
     } 

     public Person getParent() { 
      return parent; 
     } 

     public void setParent(final Person parent) { 
      this.parent = parent; 
     } 

    } 

    public static class PersonSerializer implements JsonSerializer<Person> { 
     public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) { 
      JsonObject result = new JsonObject(); 
      result.add("id", new JsonPrimitive(person.getId())); 
      result.add("name", new JsonPrimitive(person.getName())); 
      Person parent = person.getParent(); 
      if (parent != null) { 
       result.add("parent", new JsonPrimitive(parent.getId())); 
      } 
      return result; 
     } 
    } 

    public static void main(final String[] args) { 
     Person p = new Person(1, "Joe", new Person(2, "Mike")); 
     com.google.gson.Gson gson = new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer()) 
       .create(); 
     System.out.println(gson.toJson(p)); 
    } 

} 
+0

Dziękuję Spaeth, fajne podejście – Mario

6

Właśnie dostałem odpowiedź. Jednak chcę udostępnić Ci inny sposób za pomocą adnotacji @JsonAdapter.

Adnotacje fasoli osoba jak to

@JsonAdapter(PersonAdatper.class) 
public class Person { 
    public int id; 
    public String name; 
    public Person parent; 
} 

Tworzenie niestandardowych Adapter

obiekt serializacji do json ciąg

Person p = new Person(1, "Joe", new Person(2, "Mike")); 
Gson gson = new Gson();  
String result = gson.toJson(p); 

Wytwarza wyjścia jak poniżej:

{"id":1,"name":"Joe","parent":2} 

Znalazłem ten sposób z samouczka GSON Annotations Example using JsonAdapter

Powiązane problemy