All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.antiaction.common.json.JSONStructureUnmarshaller Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
/*
 * JSON library.
 * Copyright 2012-2013 Antiaction (http://antiaction.com/)
 *
 * 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.
 */

package com.antiaction.common.json;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * De-serialize a JSON structure into Java Object(s).
 *
 * @author Nicholas
 * Created on 24/07/2013
 */
public class JSONStructureUnmarshaller {

	protected JSONObjectMappings objectMappings;

	protected Map classMappings;

	public JSONStructureUnmarshaller(JSONObjectMappings objectMappings) {
		this.objectMappings = objectMappings;
		this.classMappings = objectMappings.classMappings;
	}

	public  T toObject(JSONStructure json_struct, Class clazz) throws JSONException {
		return toObject( json_struct, clazz, null );
	}

	public  T toObject(JSONStructure json_struct, Class clazz, JSONConverterAbstract[] converters) throws JSONException {
		Boolean booleanVal;
		Integer intVal;
		Long longVal;
		Float floatVal;
		Double doubleVal;
		BigInteger bigIntegerVal;
		BigDecimal bigDecimalVal;
		String stringVal;
		byte[] byteArray;
		Object object;
		JSONObject json_object;

		JSONArray json_array;
		List json_values;
		boolean[] arrayOf_boolean;
		int[] arrayOf_int;
		long[] arrayOf_long;
		float[] arrayOf_float;
		double[] arrayOf_double;
		Boolean[] arrayOf_Boolean;
		Integer[] arrayOf_Integer;
		Long[] arrayOf_Long;
		Float[] arrayOf_Float;
		Double[] arrayOf_Double;
		BigInteger[] arrayOf_BigInteger;
		BigDecimal[] arrayOf_BigDecimal;
		String[] arrayOf_String;
		Object[] arrayOf_Object;

		JSONObjectMapping json_om = classMappings.get( clazz.getName() );
		if ( json_om == null ) {
			throw new IllegalArgumentException( "Class '" + clazz.getName() + "' not registered." );
		}
		if ( json_om.converters == true && converters == null ) {
			throw new JSONException( "Class '" + clazz.getName() + "' may required converters!" );
		}

		JSONObject srcJSONObject = json_struct.getObject();
		T dstObj = null;

		try {
			dstObj = clazz.newInstance();

			Iterator fieldMappingsIter = json_om.fieldMappingsList.iterator();
			JSONObjectFieldMapping fieldMapping;
			JSONValue json_value;
			while ( fieldMappingsIter.hasNext() ) {
				fieldMapping = fieldMappingsIter.next();
				json_value = srcJSONObject.get( fieldMapping.jsonName );
				if ( json_value != null ) {
					switch ( fieldMapping.type ) {
					case JSONObjectMappingConstants.T_PRIMITIVE_BOOLEAN:
						if ( fieldMapping.converterId == -1 ) {
							booleanVal = json_value.getBoolean();
						}
						else {
							booleanVal = converters[ fieldMapping.converterId ].getBoolean( fieldMapping.fieldName, json_value );
						}
						if ( booleanVal == null ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is primitive and can not be null." );
						}
						fieldMapping.field.setBoolean( dstObj, booleanVal );
						break;
					case JSONObjectMappingConstants.T_PRIMITIVE_INTEGER:
						if ( fieldMapping.converterId == -1 ) {
							intVal = json_value.getInteger();
						}
						else {
							intVal = converters[ fieldMapping.converterId ].getInteger( fieldMapping.fieldName, json_value );
						}
						if ( intVal == null ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is primitive and can not be null." );
						}
						fieldMapping.field.setInt( dstObj, intVal );
						break;
					case JSONObjectMappingConstants.T_PRIMITIVE_LONG:
						if ( fieldMapping.converterId == -1 ) {
							longVal = json_value.getLong();
						}
						else {
							longVal = converters[ fieldMapping.converterId ].getLong( fieldMapping.fieldName, json_value );
						}
						if ( longVal == null ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is primitive and can not be null." );
						}
						fieldMapping.field.setLong( dstObj, longVal );
						break;
					case JSONObjectMappingConstants.T_PRIMITIVE_FLOAT:
						if ( fieldMapping.converterId == -1 ) {
							floatVal = json_value.getFloat();
						}
						else {
							floatVal = converters[ fieldMapping.converterId ].getFloat( fieldMapping.fieldName, json_value );
						}
						if ( floatVal == null ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is primitive and can not be null." );
						}
						fieldMapping.field.setFloat( dstObj, floatVal );
						break;
					case JSONObjectMappingConstants.T_PRIMITIVE_DOUBLE:
						if ( fieldMapping.converterId == -1 ) {
							doubleVal = json_value.getDouble();
						}
						else {
							doubleVal = converters[ fieldMapping.converterId ].getDouble( fieldMapping.fieldName, json_value );
						}
						if ( doubleVal == null ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is primitive and can not be null." );
						}
						fieldMapping.field.setDouble( dstObj, doubleVal );
						break;
					case JSONObjectMappingConstants.T_BOOLEAN:
						if ( fieldMapping.converterId == -1 ) {
							booleanVal = json_value.getBoolean();
						}
						else {
							booleanVal = converters[ fieldMapping.converterId ].getBoolean( fieldMapping.fieldName, json_value );
						}
						if ( booleanVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, booleanVal );
						break;
					case JSONObjectMappingConstants.T_INTEGER:
						if ( fieldMapping.converterId == -1 ) {
							intVal = json_value.getInteger();
						}
						else {
							intVal = converters[ fieldMapping.converterId ].getInteger( fieldMapping.fieldName, json_value );
						}
						if ( intVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, intVal );
						break;
					case JSONObjectMappingConstants.T_LONG:
						if ( fieldMapping.converterId == -1 ) {
							longVal = json_value.getLong();
						}
						else {
							longVal = converters[ fieldMapping.converterId ].getLong( fieldMapping.fieldName, json_value );
						}
						if ( longVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, longVal );
						break;
					case JSONObjectMappingConstants.T_FLOAT:
						if ( fieldMapping.converterId == -1 ) {
							floatVal = json_value.getFloat();
						}
						else {
							floatVal = converters[ fieldMapping.converterId ].getFloat( fieldMapping.fieldName, json_value );
						}
						if ( floatVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, floatVal );
						break;
					case JSONObjectMappingConstants.T_DOUBLE:
						if ( fieldMapping.converterId == -1 ) {
							doubleVal = json_value.getDouble();
						}
						else {
							doubleVal = converters[ fieldMapping.converterId ].getDouble( fieldMapping.fieldName, json_value );
						}
						if ( doubleVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, doubleVal );
						break;
					case JSONObjectMappingConstants.T_BIGINTEGER:
						if ( fieldMapping.converterId == -1 ) {
							bigIntegerVal = json_value.getBigInteger();
						}
						else {
							bigIntegerVal = converters[ fieldMapping.converterId ].getBigInteger( fieldMapping.fieldName, json_value );
						}
						if ( bigIntegerVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, bigIntegerVal );
						break;
					case JSONObjectMappingConstants.T_BIGDECIMAL:
						if ( fieldMapping.converterId == -1 ) {
							bigDecimalVal = json_value.getBigDecimal();
						}
						else {
							bigDecimalVal = converters[ fieldMapping.converterId ].getBigDecimal( fieldMapping.fieldName, json_value );
						}
						if ( bigDecimalVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, bigDecimalVal );
						break;
					case JSONObjectMappingConstants.T_STRING:
						if ( fieldMapping.converterId == -1 ) {
							stringVal = json_value.getString();
						}
						else {
							stringVal = converters[ fieldMapping.converterId ].getString( fieldMapping.fieldName, json_value );
						}
						if ( stringVal == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, stringVal );
						break;
					case JSONObjectMappingConstants.T_BYTEARRAY:
						if ( fieldMapping.converterId == -1 ) {
							byteArray = json_value.getBytes();
						}
						else {
							byteArray = converters[ fieldMapping.converterId ].getBytes( fieldMapping.fieldName, json_value );
						}
						if ( byteArray == null && !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						fieldMapping.field.set( dstObj, byteArray );
						break;
					case JSONObjectMappingConstants.T_OBJECT:
						json_object = json_value.getObject();
						if ( json_object != null ) {
							object = toObject( json_object, fieldMapping.clazz, converters );
						}
						else if ( !fieldMapping.nullable ) {
							throw new JSONException( "Field '" + fieldMapping.fieldName + "' is not nullable." );
						}
						else {
							object = null;
						}
						fieldMapping.field.set( dstObj, object );
						break;
					case JSONObjectMappingConstants.T_ARRAY:
						json_array = json_value.getArray();
						if ( json_array != null ) {
							switch (  fieldMapping.arrayType ) {
							case JSONObjectMappingConstants.T_PRIMITIVE_BOOLEAN:
								json_values = json_array.values;
								arrayOf_boolean = new boolean[ json_values.size() ];
								for ( int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy