Mercurial > hg > org.digitalhps.storage.triplestoreimplementation
view src/org/digitalhps/storage/triplestoreimplementation/utils/JSONTools.java @ 0:33d1589a61a7 default tip
initial
author | dwinter |
---|---|
date | Thu, 08 Dec 2011 09:18:00 +0100 |
parents | |
children |
line wrap: on
line source
package org.digitalhps.storage.triplestoreimplementation.utils; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.util.Iterator; import org.digitalhps.storage.triplestoreimplementation.exceptions.JSONToObjectTransformException; import org.digitalhps.storage.triplestoreimplementation.exceptions.JSONandClassMissmatchException; import org.digitalhps.storage.triplestoreimplementation.exceptions.ObjectToJSONTransformException; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.restlet.data.Status; /** * Tools zum Umwandlen von Java-Objekten in JSON und umgekehrt * * @author dwinter * */ public class JSONTools { /** * @param inputObject Nimmt ein Java object und Ÿbersetzen des Felder des Objectes in JSON, dazu mŸssen die Felder "public" sein. * @return JSONArray oder JSONObject je nach Type des inputObjectes (Arrays werden in Arrays Ÿbersetzt), Null wird in ein leeres JSONObject Ÿbersetzt. * @throws ObjectToJSONTransformException */ static public Object transformToJson(Object inputObject) throws ObjectToJSONTransformException { if (inputObject==null) // special case, generate empty object. return new JSONObject(); Object ret=null; Class<?>cls = inputObject.getClass(); if (!cls.isArray()){ // kein Array JSONObject retObject = new JSONObject(); Field[] fs=cls.getFields(); for(int i=0;i<fs.length;i++){ Field field=fs[i]; Class<?> T = field.getType(); String fieldName=field.getName(); Object fieldValue; try { fieldValue = field.get(inputObject); } catch (IllegalArgumentException e) { throw new ObjectToJSONTransformException(); } catch (IllegalAccessException e) { throw new ObjectToJSONTransformException(); } //van only be string, an object or an array. if (String.class.isInstance(fieldValue)){ try { retObject.put(fieldName, (String)fieldValue); } catch (JSONException e) { throw new ObjectToJSONTransformException(); } } else if (!T.isArray()) { //field is not an array} JSONObject jsonsubObject = (JSONObject) transformToJson(fieldValue); try { retObject.put(fieldName, jsonsubObject); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (T.isArray()){ // Feldwert ist ein Array JSONArray ja = new JSONArray(); if (fieldValue!=null){ for (int j=0;j<Array.getLength(fieldValue);j++){ Object arrayElement = Array.get(fieldValue, j); ja.put(transformToJson(arrayElement)); } try { retObject.put(fieldName, ja); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } ret = retObject; } else if(cls.isArray()){// Object ist ein Array JSONArray retObject = new JSONArray(); for (int j=0;j<Array.getLength(inputObject);j++){ Object arrayElement = Array.get(inputObject, j); retObject.put(transformToJson(arrayElement)); } ret=retObject; } return ret; } /** * Erzeugt ein JAVA Object aus einem JSON Object * @param cls Klasses die Erzeugt werden soll, falls das jsonObject vom Typ JSONArray sein, muss cls ein Arraytyp sein. * @param jsonObject Object das umgewandelt werden soll. * @return * @throws JSONandClassMissmatchException jsonObject ist ein Array und cls keins oder umgekehert. * @throws JSONToObjectTransformException Fehler bei Transformierten, i.a. tritt das aus falls die Felder in der Javaclass nicht schreibbar sind. Fehlende Felder werden ignoriert. */ static public Object createObject(Class<?> cls, Object jsonObject) throws JSONandClassMissmatchException, JSONToObjectTransformException { Object newObject; try { newObject = cls.newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } if (JSONObject.class.isInstance(jsonObject)){ if (cls.isArray()){ throw new JSONandClassMissmatchException(); } JSONObject jo = (JSONObject)jsonObject; Iterator keys = jo.keys(); while (keys.hasNext()){ String key=(String) keys.next(); Field f; try { f = cls.getField(key); } catch (SecurityException e) { throw new JSONToObjectTransformException(); } catch (NoSuchFieldException e) { e.printStackTrace(); // Feld fehlt, dieses kann ignoriert werden. continue; } Class<?> T = f.getType(); String name= T.getName(); //van only be string, an object or an array. if (name.equals("java.lang.String")){ String value; try { value = jo.getString(key); } catch (JSONException e) { e.printStackTrace(); throw new JSONToObjectTransformException(); } try { f.set(newObject,value); } catch (IllegalArgumentException e) { throw new JSONToObjectTransformException(); } catch (IllegalAccessException e) { throw new JSONToObjectTransformException(); } } else if (!T.isArray()) { // Feld ist kein Array und kein String} JSONObject jsonsubObject; try { jsonsubObject = jo.getJSONObject(key); } catch (JSONException e) { throw new JSONToObjectTransformException(); } Class<?> c = T.getClass(); Object newObject2=createObject(c,jsonsubObject); try { f.set(newObject, newObject2); } catch (IllegalArgumentException e) { throw new JSONToObjectTransformException(); } catch (IllegalAccessException e) { throw new JSONToObjectTransformException(); } } else if (T.isArray()){ // Feld is ein array JSONArray ja; Class<?> c = T.getComponentType(); Object objects; int objectCounter=0; try { ja = jo.getJSONArray(key); objects= Array.newInstance(c, ja.length()); } catch (JSONException e) { throw new JSONToObjectTransformException(); } for (int i=0;i<ja.length();i++){ Object jsonArryelement; try { jsonArryelement = ja.get(i); } catch (JSONException e) { throw new JSONToObjectTransformException(); } //schaue welches Klasse das nach definition in RelationEvent sein soll //speichere das object Array.set(objects,objectCounter,createObject(c,jsonArryelement)); objectCounter++; } try { f.set(newObject,objects); } catch (IllegalArgumentException e) { throw new JSONToObjectTransformException(); } catch (IllegalAccessException e) { throw new JSONToObjectTransformException(); } } } } else if (JSONArray.class.isInstance(jsonObject)){ //JSONArray wurde Ÿbergeben. JSONArray ja = (JSONArray)jsonObject; if (!cls.isArray()){ throw new JSONandClassMissmatchException(); } newObject = Array.newInstance(cls.getComponentType(), ja.length()); for(int i=0;i<ja.length();i++){ try { Array.set(newObject, i, createObject(cls.getComponentType(), ja.get(i))); } catch (ArrayIndexOutOfBoundsException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { throw new JSONToObjectTransformException(); } catch (JSONException e) { throw new JSONToObjectTransformException(); } } } return newObject; }; }