Advertisement
raffaele181188

Gson deserialization

Apr 6th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package com.zybnet.so;
  2.  
  3. import java.math.BigDecimal;
  4. import com.google.gson.Gson;
  5. import com.google.gson.GsonBuilder;
  6. import com.google.gson.JsonElement;
  7. import com.google.gson.JsonSyntaxException;
  8.  
  9.  
  10. public class GsonDeserialization {
  11.  
  12.     public static void main(String[] args) throws JsonSyntaxException, ClassNotFoundException {
  13.         // Create the order
  14.         Order order = new Order(new Customer("John", City.NEW_YORK), new BigDecimal("9.99"));
  15.        
  16.         // Create the envelope
  17.         Gson gson = new GsonBuilder().setPrettyPrinting().create();
  18.         Envelope<Order> envelope = new Envelope<Order>(order);
  19.         String serialized = gson.toJson(envelope);
  20.         System.out.println(serialized);
  21.        
  22.         // Read the envelope
  23.         Envelope<?> receivedEnvelope = gson.fromJson(serialized, Envelope.class);
  24.         Object result = gson.fromJson(receivedEnvelope.contents, Class.forName(receivedEnvelope.clazz));
  25.         System.out.println("Result: " + result);
  26.        
  27.         assert result.getClass().equals(Order.class);
  28.     }
  29.  
  30.     public static class Envelope<T> {
  31.         public String clazz;
  32.         public JsonElement contents;
  33.         public Envelope (T contents) {
  34.             this.clazz = contents.getClass().getName();
  35.             this.contents = new Gson().toJsonTree(contents, contents.getClass());
  36.         }
  37.     }
  38.    
  39.     public static enum City {
  40.         NEW_YORK("New York City", "00100"),
  41.         SAN_FRANCISCO("San Francisco", "00200");
  42.        
  43.         public String name, zip;
  44.        
  45.         City(String name, String zip) {
  46.             this.name = name;
  47.             this.zip = zip;
  48.         }
  49.        
  50.         public String toString() {
  51.             return String.format("%s, zip code: %s", name, zip);
  52.         }
  53.     }
  54.    
  55.     public static class Customer {
  56.         public Customer(String name, City city) {
  57.             this.name = name;
  58.             this.city = city;
  59.         }
  60.        
  61.         String name;
  62.         City city;
  63.        
  64.         public String toString() {
  65.             return String.format("%s from %s", name, city);
  66.         }
  67.     }
  68.    
  69.     public static class Order {
  70.         public Order(Customer customer, BigDecimal total) {
  71.             this.customer = customer;
  72.             this.total = total;
  73.             id = 123456789L;
  74.         }
  75.        
  76.         Long id;
  77.         Customer customer;
  78.         BigDecimal total;
  79.        
  80.         public String toString(){
  81.             return String.format("Order by %s, total $%s", customer, total);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement