Helper class to decode and encode objects to and from Json (Ext GWT) : Utility « GWT « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » GWT » Utility 




Helper class to decode and encode objects to and from Json (Ext GWT)
     
/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 [email protected]
 
 * http://extjs.com/license
 */
//package com.extjs.gxt.ui.client.js;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONNull;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

/**
 * Helper class to decode and encode objects to and from Json. Converter handles
 * simple data types (strings, numbers, booleans) and lists and maps.
 */
public class JsonConverter {

  /**
   * Decodes a Json string into a map.
   
   @param jsonString the Json string
   @return the map
   */
  public static Map<String, Object> decode(String jsonString) {
    JSONValue v = JSONParser.parse(jsonString);
    if (v.isObject() != null) {
      return decode(v.isObject());
    else {
      return null;
    }
  }

  /**
   * Decodes a JSONObject to a map.
   
   @param jso the JSONObject
   @return the map
   */
  public static Map<String, Object> decode(JSONObject jso) {
    Map<String, Object> map = new FastMap<Object>();
    for (String key : jso.keySet()) {
      JSONValue j = jso.get(key);
      if (j.isObject() != null) {
        map.put(key, decode(j.isObject()));
      else if (j.isArray() != null) {
        map.put(key, decodeToList(j.isArray()));
      else if (j.isBoolean() != null) {
        map.put(key, j.isBoolean().booleanValue());
      else if (j.isString() != null) {
        map.put(key, decodeValue(j.isString().stringValue()));
      }
    }
    return map;
  }

  /**
   * Encodes a map into a JSONObject.
   
   @param map the map
   @return the JSONObject
   */
  public static JSONObject encode(Map<String, Object> map) {
    return encodeMap((Map<String, Object>map);
  }

  /**
   * Encodes a model data instance into a JSONObject.
   
   @param model the model data object
   @return the JSONObject
   */
  public static JSONObject encode(ModelData model) {
    return encodeMap(((ModelDatamodel).getProperties());
  }

  protected static Object decodeValue(String value) {
    try {
      if (value == null || value.length() 3) {
        return null;
      }
      String type = value.substring(02);
      String val = value.substring(2);
      if (type.equals("d:")) {
        long time = Long.parseLong(val);
        return new Date(time);
      else if (type.equals("i:")) {
        return Integer.decode(val);
      else if (type.equals("f:")) {
        return new Float(val);
      }
      return val;
    catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  protected static List<Object> decodeToList(JSONArray array) {
    List<Object> list = new ArrayList<Object>();
    for (int i = 0; i < array.size(); i++) {
      JSONValue v = array.get(i);
      if (v.isObject() != null) {
        list.add(decode(v.isObject()));
      else if (v.isArray() != null) {
        list.add(decodeToList(v.isArray()));
      else if (v.isNull() != null) {
        list.add(null);
      else if (v.isNumber() != null) {
        list.add(v.isNumber().doubleValue());
      else if (v.isBoolean() != null) {
        list.add(v.isBoolean().booleanValue());
      else if (v.isString() != null) {
        list.add(decodeValue(v.isString().stringValue()));
      }
    }

    return list;
  }

  protected static String encodeValue(Object value) {
    if (value instanceof Date) {
      return "d:" ((Datevalue).getTime();
    else if (value instanceof Integer) {
      return "i:" + value;
    else if (value instanceof Float) {
      return "f:" + value;
    }
    return "s:" + value.toString();
  }

  @SuppressWarnings("unchecked")
  protected static JSONObject encodeMap(Map<String, Object> data) {
    JSONObject jsobj = new JSONObject();
    for (String key : data.keySet()) {
      Object val = data.get(key);
      if (val instanceof String) {
        jsobj.put(key, new JSONString(encodeValue(val)));
      else if (val instanceof Date) {
        jsobj.put(key, new JSONString(encodeValue(val)));
      else if (val instanceof Number) {
        jsobj.put(key, new JSONString(encodeValue(val)));
      else if (val instanceof Boolean) {
        jsobj.put(key, JSONBoolean.getInstance((Booleanval));
      else if (val == null) {
        jsobj.put(key, JSONNull.getInstance());
      else if (val instanceof Map) {
        jsobj.put(key, encodeMap((Map<String, Object>val));
      else if (val instanceof List) {
        jsobj.put(key, encodeList((List<Object>val));
      else if (val instanceof ModelData) {
        jsobj.put(key, encodeMap(((ModelDataval).getProperties()));
      }
    }

    return jsobj;
  }

  @SuppressWarnings("unchecked")
  protected static JSONArray encodeList(List<Object> data) {
    JSONArray jsona = new JSONArray();
    for (int i = 0; i < data.size(); i++) {
      Object val = data.get(i);
      if (val instanceof ModelData) {
        jsona.set(i, encodeMap(((ModelDataval).getProperties()));
      else if (val instanceof Map) {
        jsona.set(i, encodeMap((Map<String, Object>val));
      else if (val instanceof List) {
        jsona.set(i, encodeList((List<Object>val));
      else if (val instanceof String) {
        jsona.set(i, new JSONString(encodeValue(val)));
      else if (val instanceof Number) {
        jsona.set(i, new JSONString(encodeValue(val)));
      else if (val instanceof Boolean) {
        jsona.set(i, JSONBoolean.getInstance((Booleanval));
      else if (val == null) {
        jsona.set(i, JSONNull.getInstance());
      else if (val instanceof Date) {
        jsona.set(i, new JSONString(encodeValue(val)));
      }
    }
    return jsona;
  }
}

   
    
    
    
    
  














SmartGWT.zip( 9,880 k)
Related examples in the same category
1.Use reflection to generate the async interface from the Service interface as per GWT standard
2.Array Utils for client side GWT
3.A simple number formatting/ parsing class
4.GWT window utility
5.Implement java.util.regex.Pattern with Javascript RegExp object
6.GWT color class
7.DOM related helper methods (Smart GWT)
8.Formatting functions for GWT client side (Ext GWT)
9.String util for GWT client side (Smart GWT)
10.GWT DOM util
11.Replace string for GWT
12.GWT style util
13.gwt DateTimeFormat
14.A utility class that provides utility methods to work with arrays for GWT.
15.Date compare util for GWT
16.GWT DOM utility functions for use when programmatically constructing a UI.
17.Generate a faster string comparison than String.equals() for web mode code.
18.Js Array Util
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.