tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > GOOGLE GSON > Versioning

Versioning 

Google-gson is a java library from Google for encoding and decoding JSON text. Get the latest binaries from http://code.google.com/p/google-gson/. Versioning allows to serialize only a certain properties based on version. We can annotate the required properties with @Since annotation.

File Name  :  
com/bethecoder/tutorials/google_gson/common/NamedClass.java 
   
package com.bethecoder.tutorials.google_gson.common;

import com.google.gson.annotations.Since;

public class NamedClass {

  private String name;

  @Since(1.0)
  private String name1;

  @Since(2.0)
  private String name2;

  @Since(3.0)
  private String name3;
  
  @Since(4.0)
  private String name4;

  public String getName() {
    return name;
  }

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

  public String getName1() {
    return name1;
  }

  public void setName1(String name1) {
    this.name1 = name1;
  }

  public String getName2() {
    return name2;
  }

  public void setName2(String name2) {
    this.name2 = name2;
  }

  public String getName3() {
    return name3;
  }

  public void setName3(String name3) {
    this.name3 = name3;
  }

  public String getName4() {
    return name4;
  }

  public void setName4(String name4) {
    this.name4 = name4;
  }
}
   

File Name  :  
com/bethecoder/tutorials/google_gson/tests/VersionTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.google_gson.tests;

import com.bethecoder.tutorials.google_gson.common.NamedClass;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class VersionTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    NamedClass namedCls = new NamedClass()
    namedCls.setName("BTC");
    namedCls.setName1("BTC1");
    namedCls.setName2("BTC2");
    namedCls.setName3("BTC3");
    namedCls.setName4("BTC4");
    
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    System.out.println("Non versioned");
    System.out.println(gson.toJson(namedCls));      
    
    // Field or type marked with a version higher than this value are 
    // ignored during serialization or de-serialization.
    gson = new GsonBuilder().setVersion(1.0).setPrettyPrinting().create();
    System.out.println("Fields versioned less or equal to 1.0");
    System.out.println(gson.toJson(namedCls));
    
    gson = new GsonBuilder().setVersion(2.0).setPrettyPrinting().create();
    System.out.println("Fields versioned less or equal to 2.0");
    System.out.println(gson.toJson(namedCls));
    
    gson = new GsonBuilder().setVersion(3.0).setPrettyPrinting().create();
    System.out.println("Fields versioned less or equal to 3.0");
    System.out.println(gson.toJson(namedCls));
  }

}
   

It gives the following output,
Student.json
Non versioned
{
  "name": "BTC",
  "name1": "BTC1",
  "name2": "BTC2",
  "name3": "BTC3",
  "name4": "BTC4"
}

Fields versioned less or equal to 1.0
{
  "name": "BTC",
  "name1": "BTC1"
}

Fields versioned less or equal to 2.0
{
  "name": "BTC",
  "name1": "BTC1",
  "name2": "BTC2"
}

Fields versioned less or equal to 3.0
{
  "name": "BTC",
  "name1": "BTC1",
  "name2": "BTC2",
  "name3": "BTC3"
}



 
  


  
bl  br