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

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

/*
 * 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.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * A JSON object mapping description.
 *
 * @author Nicholas
 * Created on 22/11/2012
 */
public class JSONObjectMapping {

	/** Object mapping type. */
	public static final int OMT_OBJECT = 1;
	/** Array mapping type. */
	public static final int OMT_ARRAY = 2;

	/** Mapping type, object or array. */
	public int type;

	/*
	 * Object.
	 */

	/** Field names to ignore when mapping objects. */
	public Set ignore;

	/** Field names which can be null. */
	public Set nullableSet;

	/** Field names which can have null values. */
	public Set nullValuesSet;

	/** Map of mapped fields. */
	public Map fieldMappingsMap;

	/** List of mapped fields. */
	public List fieldMappingsList;

	/** Array of mapped fields. */
	public JSONObjectFieldMapping[] fieldMappingsArr;

	/** Boolean indicating if one or more field mapping(s) requires a converter. */
	public boolean converters;

	/*
	 * Array.
	 */

	/** JSON Java array type identifier. */
	public int arrayType;

	/** Field class type name. */
	public String className;

	/** Field class. */
	public Class clazz;

	/** Field object mapping. */
	public JSONObjectFieldMapping fieldMapping;

	/** Object mapping, if array is an object type. */
	public JSONObjectMapping objectMapping;

	private JSONObjectMapping() {
	}

	public static JSONObjectMapping getObjectMapping() {
		JSONObjectMapping om = new JSONObjectMapping();
		om.type = OMT_OBJECT;
		om.ignore = new HashSet();
		om.nullableSet = new TreeSet();
		om.nullValuesSet = new TreeSet();
		om.fieldMappingsMap = new TreeMap();
		om.fieldMappingsList = new LinkedList();
		return om;
	}

	public static JSONObjectMapping getArrayMapping() {
		JSONObjectMapping om = new JSONObjectMapping();
		om.type = OMT_ARRAY;
		return om;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		toString( sb );
		return sb.toString();
	}

	public void toString(StringBuilder sb) {
		switch ( type ) {
		case OMT_OBJECT:
			sb.append( "  type: Object(" );
			sb.append( type );
			sb.append( ")\n" );
			break;
		case OMT_ARRAY:
			sb.append( "  type: Array(" );
			sb.append( type );
			sb.append( ")\n" );
			sb.append( "  arrayType: " );
			sb.append( JSONObjectMappingConstants.typeString( arrayType ) );
			sb.append( "(" );
			sb.append( arrayType );
			sb.append( ")\n" );
			break;
		}
		sb.append( "  className: " );
		sb.append( className );
		sb.append( "\n" );
		sb.append( "  clazz: " );
		sb.append( clazz );
		sb.append( "\n" );

		sb.append( "  ignore: " );
		toString( ignore, sb );
		sb.append( "\n" );
		sb.append( "  nullableSet: " );
		toString( nullableSet, sb );
		sb.append( "\n" );
		sb.append( "  nullValuesSet: " );
		toString( nullValuesSet, sb );
		sb.append( "\n" );
		sb.append( "  fieldMapping: " );
		if ( fieldMapping != null ) {
			sb.append( "\n" );
			fieldMapping.toString( sb );
		}
		else {
			sb.append( "null" );
			sb.append( "\n" );
		}
		sb.append( "  objectMapping: " );
		if ( objectMapping != null ) {
			sb.append( objectMapping.className );
		}
		else {
			sb.append( "null" );
		}
		sb.append( "\n" );
		sb.append( "  fieldMappingsArr[]: " );
		if ( fieldMappingsArr != null ) {
			sb.append( "\n" );
			for ( int i=0; i set, StringBuilder sb) {
		if ( set != null ) {
			sb.append( "[" );
			Iterator iter = set.iterator();
			String str = null;
			while ( iter.hasNext() ) {
				if ( str != null ) {
					sb.append( ", " );
				}
				str = iter.next();
				sb.append( str );
			}
			sb.append( "]" );
		}
		else {
			sb.append( "null" );
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy