GSON Instance Creation


Process of Object creation to Gson Api is called Gson Instantiation. Here in this part discussing these

  1. Creation using new Gson()
  2. Simple Example on toJson
  3. Detailed Complete Object Based Example using toJson(),fromJson()

>_Creation using new Gson()

  • Gson is a key class of package 'com.google.gson'. We can create a Gson object simply by creating it with the new Gson() instruction. Here is how creating a Gson object looks:

    Gson gson = new Gson();

    Once we have created a Gson instance we can start using it 'to parse and generate JSON'. Lets See some main methods of Gson class

    Method Name Description
    String toJson(Object src) Serializes Object to Json Form
    <T> T fromJson(String json, Type typeOfT) Deserializes Json To Object .
    " toJson() and fromJson()" have various overloaded methods in Gson class, check this link to view Gson Class & its methods. Example on Gson() with the above Methods.

>_Simple toJson() Example on Primitives

  • /**
     * @author Tutorialtous.com
     */
    package com.tutorialtous.gson;
    
    import java.util.LinkedList;
    import java.util.List;
    
    import com.google.gson.Gson;
    
    public class ToGsonPrimitiveExample {
      public static void main(String args[]) {
        List<Object> list = new LinkedList<Object>();
        String names[]={"www","tutorialtous",".","com"};
        int digit = 9;
        Double num = new Double(7.89);
        list.add("Hi");
        list.add("This Is toJson() ");
        list.add("Simple Example");
        list.add("From");
        list.add(names);
        
        Gson gson = new Gson();
        
        System.out.println(gson.toJson(digit));
        System.out.println(gson.toJson(num));
        System.out.println(gson.toJson(names));
        String str=gson.toJson(list);
        System.out.println(str);
      }
    }
    
    Output:-
    9
    7.89
    ["www","tutorialtous",".","com"]
    ["Hi","This Is toJson() ","Simple Example","From",["www","tutorialtous",".","com"]] 

>_Complete Example of toJson(),fromJson() Using Object Concept

  • /**
     * @author Tutorialtous.com
     */
    package com.tutorialtous.gson;
    
    import com.google.gson.Gson;
    
    public class ToGsonObjecExample {
      public static void main(String args[]) {
        Player p, p2;
        String json = null;
        Gson gson = null;
        p = new Player("Hercules", 20, 12, 50000, 32070);
        gson = new Gson();
        json = gson.toJson(p);
        p2= gson.fromJson(json, Player.class);
        p2.setName("Lyra");
        System.out.println(json);
        System.out.println(p2.toString());
      }
    }
    
    class Player {
      private String name;
      private int gamesplayed;
      private int gameswon;
      private double totalbet;
      private double totalwin;
    
      public Player() {
    
      }
    
      public Player(String name, int gamesplayed, int gameswon, double totalbet,
          double totalwin) {
        super();
        this.name = name;
        this.gamesplayed = gamesplayed;
        this.gameswon = gameswon;
        this.totalbet = totalbet;
        this.totalwin = totalwin;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public int getGamesplayed() {
        return gamesplayed;
      }
    
      public void setGamesplayed(int gamesplayed) {
        this.gamesplayed = gamesplayed;
      }
    
      public int getGameswon() {
        return gameswon;
      }
    
      public void setGameswon(int gameswon) {
        this.gameswon = gameswon;
      }
    
      public double getTotalbet() {
        return totalbet;
      }
    
      public void setTotalbet(double totalbet) {
        this.totalbet = totalbet;
      }
    
      public double getTotalwin() {
        return totalwin;
      }
    
      public void setTotalwin(double totalwin) {
        this.totalwin = totalwin;
      }
    
      public String toString() {
        return "json formated toString" + new Gson().toJson(this);
      }
    }
    

    Output:-

    {"name":"Hercules","gamesplayed":20,"gameswon":12,"totalbet":50000.0,
    "totalwin":32070.0}
    json formated toString{"name":"Lyra","gamesplayed":20,"gameswon":12,"totalbet":50000.0,
    "totalwin":32070.0}
    
Copyright © 2018-2020 TutorialToUs. All rights reserved.