Java tutorial
package org.adminmap.core.rpc; /** * Copyright 2013 Philip Koshy Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.util.Arrays; import org.adminmap.core.utility.JSON; import org.json.JSONObject; public class Response extends JSONizer { private JSONObject result; private String error; public Response() { this.result = null; this.error = null; } @Override public String toJSONString() { final JSONObject jsonObject = new JSONObject(); if (this.error == null) { jsonObject.put("result", result); jsonObject.put("error", JSONObject.NULL); } else { jsonObject.put("result", JSONObject.NULL); jsonObject.put("error", error); } return jsonObject.toString(); } // end of toJSONString() @Override public void fromJSONString(final String json) { final JSONObject jsonObject = new JSONObject(json); result = jsonObject.getJSONObject("result"); Object errorObject = jsonObject.get("error"); if (errorObject == JSONObject.NULL) error = null; else error = (String) errorObject; } public <T> void setResult(final Class<?> classProperty, final T object) { // Get the class name final String className = classProperty.getName(); // Make sure we know how to serialize the provided type into a JSON object if (!JSON.isSupportedType(classProperty)) throw new IllegalArgumentException("Unsupported type passed to setResult(): " + className); // Serialize the object into a org.json.JSONObject final JSONObject resultJSON = new JSONObject(); if (JSONizer.class.isInstance(object)) resultJSON.put(className, ((JSONizer) object).toJSONObject()); else if (classProperty == Boolean[].class || classProperty == Integer[].class || classProperty == Long[].class || classProperty == Double[].class || classProperty == String[].class) { @SuppressWarnings("unchecked") T[] objects = (T[]) object; resultJSON.put(className, Arrays.asList(objects)); } else resultJSON.put(className, object); result = resultJSON; } // end of setResult() @SuppressWarnings("unchecked") public <T> T getResult(final Class<T> classProperty) { if (result == null) throw new RuntimeException("The Response object did not have a 'result' field encoded"); // Make sure we know how to deserialize the provided type from a JSON object if (!JSON.isSupportedType(classProperty)) throw new RuntimeException("Unsupported type passed to getResult(): " + classProperty.getName()); // Determine the class property that is encoded in our result object final String className = JSONObject.getNames(result)[0]; // The key is the class name Class<?> encodedClassProperty = null; try { encodedClassProperty = Class.forName(className); } catch (ClassNotFoundException e) { throw new RuntimeException("Cannot find class encoded in JSON result string: " + className); } // Make sure it is the same class property that was provided to this function if (!JSON.isPrimitiveType(classProperty) && classProperty != encodedClassProperty) throw new IllegalArgumentException( "The class specified does not match the class encoded in the JSON result string: " + classProperty.getSimpleName() + " vs " + encodedClassProperty.getSimpleName()); // Determine the super class final Class<?> superClassProperty = classProperty.getSuperclass(); // Create a new instance of the result object according to the class property that we've been provided T resultObject = null; if (superClassProperty != null && superClassProperty.equals(JSONizer.class)) { try { resultObject = (T) classProperty.newInstance(); } catch (InstantiationException | IllegalAccessException e) { System.err.println(e.getMessage()); throw new RuntimeException("Could not instantiate the result object"); } ((JSONizer) resultObject).fromJSONObject(result.getJSONObject(className)); } else if (classProperty == boolean.class) resultObject = (T) JSON.getBoolean(result); else if (classProperty == boolean[].class) resultObject = (T) JSON.getBooleanArray(result); else if (classProperty == Boolean.class) resultObject = (T) JSON.getBoolean(result); else if (classProperty == Boolean[].class) resultObject = (T) JSON.getBooleanArray(result); else if (classProperty == int.class) resultObject = (T) JSON.getInteger(result); else if (classProperty == int[].class) resultObject = (T) JSON.getIntegerArray(result); else if (classProperty == Integer.class) resultObject = (T) JSON.getInteger(result); else if (classProperty == Integer[].class) resultObject = (T) JSON.getIntegerArray(result); else if (classProperty == long.class) resultObject = (T) JSON.getLong(result); else if (classProperty == long[].class) resultObject = (T) JSON.getLongArray(result); else if (classProperty == Long.class) resultObject = (T) JSON.getLong(result); else if (classProperty == Long[].class) resultObject = (T) JSON.getLongArray(result); else if (classProperty == double.class) resultObject = (T) JSON.getDouble(result); else if (classProperty == double[].class) resultObject = (T) JSON.getDoubleArray(result); else if (classProperty == Double.class) resultObject = (T) JSON.getDouble(result); else if (classProperty == Double[].class) resultObject = (T) JSON.getDoubleArray(result); else if (classProperty == String.class) resultObject = (T) JSON.getString(result); else if (classProperty == String[].class) resultObject = (T) JSON.getStringArray(result); else throw new RuntimeException("Could not cast the result to unsupported type: " + className); return resultObject; } // end of getResult() void setError(final String error) { this.error = error; } public String getError() { return error; } } // end of class Response