Package com.metaparadigm.jsonrpc

Source Code of com.metaparadigm.jsonrpc.MapSerializer

/*
* JSON-RPC-Java - a JSON-RPC to Java Bridge with dynamic invocation
*
* $Id: MapSerializer.java,v 1.4 2005/06/16 23:26:14 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public (LGPL)
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details: http://www.gnu.org/
*
*/

package com.metaparadigm.jsonrpc;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class MapSerializer extends AbstractSerializer
{
    private static Class[] _serializableClasses = new Class[]
  { Map.class, HashMap.class, TreeMap.class, LinkedHashMap.class };

    private static Class[] _JSONClasses = new Class[]
  { JSONObject.class };

    public Class[] getSerializableClasses() { return _serializableClasses; }
    public Class[] getJSONClasses() { return _JSONClasses; }

    public boolean canSerialize(Class clazz, Class jsonClazz)
    {
  return (super.canSerialize(clazz, jsonClazz) ||
    ((jsonClazz == null || jsonClazz == JSONObject.class) &&
     Map.class.isAssignableFrom(clazz)));
    }

    public ObjectMatch tryUnmarshall(SerializerState state,
             Class clazz, Object o)
  throws UnmarshallException
    {
  JSONObject jso = (JSONObject)o;
  String java_class = getJavaClass(jso);
  if(!(java_class.equals("java.util.Map") ||
       java_class.equals("java.util.AbstractMap") ||
       java_class.equals("java.util.LinkedHashMap") ||
       java_class.equals("java.util.TreeMap") ||
       java_class.equals("java.util.HashMap")))
      throw new UnmarshallException("not a Map");
  JSONObject jsonmap = getMap(jso);
  ObjectMatch m = new ObjectMatch(-1);
  Iterator i = jsonmap.keys();
  String key = null;
  try {
      while(i.hasNext()) {
    key = (String)i.next();
    m = ser.tryUnmarshall(state, null, jsonmap.get(key)).max(m);
      }
  } catch (JSONException e) {
      throw new UnmarshallException("key " + key + " " + e.getMessage());
  } catch (UnmarshallException e) {
      throw new UnmarshallException
    ("key " + key + " " + e.getMessage());
  }
  return m;
    }

    public Object unmarshall(SerializerState state, Class clazz, Object o)
  throws UnmarshallException
    {
  JSONObject jso = (JSONObject)o;
  String java_class = getJavaClass(jso);
  AbstractMap abmap = null;
  if(java_class.equals("java.util.Map") ||
     java_class.equals("java.util.AbstractMap") ||
     java_class.equals("java.util.HashMap")) {
      abmap = new HashMap();
  } else if(java_class.equals("java.util.TreeMap")) {
      abmap = new TreeMap();
  } else if(java_class.equals("java.util.LinkedHashMap")) {
      abmap = new LinkedHashMap();
  } else {
      throw new UnmarshallException("not a Map");
  }
  JSONObject jsonmap = getMap(jso);
  Iterator i = jsonmap.keys();
  String key = null;
  try {
      while(i.hasNext()) {
    key = (String)i.next();
    abmap.put(key, ser.unmarshall(state, null, jsonmap.get(key)));
      }
  } catch (JSONException e) {
      throw new UnmarshallException("key " + key + " " + e.getMessage());
  } catch (UnmarshallException e) {
      throw new UnmarshallException
    ("key " + key + " " + e.getMessage());
  }
  return abmap;
    }

    public Object marshall(SerializerState state, Object o)
  throws MarshallException
    {
  Map map = (Map)o;
  JSONObject obj = new JSONObject();
  JSONObject mapdata = new JSONObject();
  try {
      if (ser.getMarshallClassHints())
    obj.put("javaClass", o.getClass().getName());
      obj.put("map", mapdata);
  }
  catch (JSONException e) {
      throw new MarshallException("map metadata: " + e.getMessage());
  }
  Object key = null;
  Object val = null;
  try {
      Iterator i = map.entrySet().iterator();
      while(i.hasNext()) {
    Map.Entry ent = (Map.Entry)i.next();
    key = ent.getKey();
    val = ent.getValue();
    // only support String keys
    mapdata.put(key.toString(), ser.marshall(state, val));
      }
  } catch (JSONException e) {
      throw new MarshallException
    ("map key " + key + " " + e.getMessage());
  } catch (MarshallException e) {
      throw new MarshallException
    ("map key " + key + " " + e.getMessage());
  }
  return obj;
    }

    private String getJavaClass(JSONObject jso) throws UnmarshallException {
  try {
      return jso.getString("javaClass");
  } catch (JSONException e) {
      throw new UnmarshallException("no type hint");
  }
    }

    private JSONObject getMap(JSONObject jso) throws UnmarshallException {
  try {
      return jso.getJSONObject("map");
  } catch (JSONException e) {
      throw new UnmarshallException("map missing");
  }
    }

}
TOP

Related Classes of com.metaparadigm.jsonrpc.MapSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.