One more library for JSON parsing

I want to introduce you with one more library, which help us to parse a JSON into java object and vise versa.
This is the GSON. More info you can get from oficial site. I just show you simple example.

E.g. we have a following json

[ {
  "title" : "Root1",
  "object_version" : 3,
  "status" : 1,
  "items" : []
 },
 {
  "title" : "Root2",
  "object_version" : 3,
  "status" : 5,
  "items" :
   [ {
     "title" : "Item1",
     "object_version" : 7,
     "status" : 3,
     "items" :
      [ {
        "title" : "Item1.1",
        "object_version" : 9,
        "status" : 5,
        "items" : []
       }]
    },
    {
     "title" : "Item2",
     "object_version" : 4,
     "status" : 1,
     "items" : []
    }]
 }]

Now we should implement a class.

public class MyJavaObject {
 private String title;
// we can define any name of property
 @SerializedName(value = "object_version")
 private Integer version;
// enum, we have to implement an adapter for it (see below)
 private MyStatus status;
 private LinkedList<MyJavaObject> items;
// you should create a new class and describe its properties
// private MyOtherJavaObject otherObject;
// Gson.DEFAULT_MODIFIER_BASED_EXCLUSION_STRATEGY contains modifiers which will be skipped
 private transient Long privateField;

// getters and setters are optional (values will set directly into properties)
 public void setVersion(Integer version) {
  this.version = version;
 }

// the field 'status' has type Integer but we want an enum instead of it
 public enum MyStatus {
  EXIST(1, "Exist"),
  DELETED(3, "Deleted"),
  MODIFIED(5, "Modified");

  private Integer id;
  private String title;
  private MyStatus(Integer id, String title) {
   this.id = id;
   this.title = title;
  }
  public static MyStatus getById(int id) {
   for (MyStatus status : values()) {
    if (status.id == id) {
     return status;
    }
   }
   return null;
  }
 }

// the adapter for 'status' looks very simple
 private static class MyStatusAdapter implements JsonDeserializer, JsonSerializer {
  @Override
  public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   return MyStatus.getById(json.getAsInt());
  }
  @Override
  public JsonElement serialize(Object src, Type typeOfSrc, JsonSerializationContext context) {
   return new JsonPrimitive(((MyStatus) src).id);
  }
 }
  
// now we should register all our adapters and realize methods which will parse a JSON into MyJavaObject and vise versa
 private static Gson createGson() {
  return new GsonBuilder().registerTypeAdapter(MyStatus.class, new MyStatusAdapter()).create();
 }

 public static String toJSON(LinkedList<MyJavaObject> objList) {
  return createGson().toJson(objList, new TypeToken<LinkedList<MyJavaObject>>(){}.getType());
 }

 public static LinkedList<MyJavaObject> fromJson(String json) {
  return createGson().fromJson(json, new TypeToken<LinkedList<MyJavaObject>>(){}.getType());
 }
}

Enjoy your work

 LinkedList<MyJavaObject> list = MyJavaObject.fromJson(json);
 list.get(0).setVersion(33);
 json = MyJavaObject.toJSON(list);