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

com.rathravane.till.data.json.JsonVisitor Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package com.rathravane.till.data.json;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

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

public class JsonVisitor
{
	public interface ArrayVisitor
	{
		/**
		 * Visit an array entry. Return false to stop iteration.
		 * @param t
		 * @return
		 * @throws JSONException
		 * @throws E
		 */
		boolean visit ( T t ) throws JSONException, E;
	}

	public interface ArrayOfObjectVisitor extends ArrayVisitor
	{
	}

	public interface ArrayOfStringVisitor extends ArrayVisitor
	{
	}

	@SuppressWarnings("unchecked")
	public static  void forEachElement ( JSONArray a, ArrayVisitor v ) throws JSONException, E
	{
		if ( a == null ) return;
		
		final int len = a.length ();
		for ( int i=0; i objectToMap ( JSONObject obj )
	{
		final HashMap map = new HashMap ();
		if ( obj != null )
		{
			for ( Object oo : obj.keySet () )
			{
				final String key = oo.toString ();
				final String val = obj.getString ( key );
				map.put ( key, val );
			}
		}
		return map;
	}

	public static boolean listContains ( JSONArray a, String t )
	{
		if ( a == null ) return false;

		final int len = a.length ();
		for ( int i=0; i arrayToList ( JSONArray a )
	{
		final LinkedList list = new LinkedList ();
		if ( a != null )
		{
			forEachElement ( a, new ArrayVisitor ()
			{
				@Override
				public boolean visit ( Object t ) throws JSONException
				{
					if ( t != null )
					{
						list.add ( t.toString () );
					}
					return true;
				}
			});
		}
		return list;
	}
	
	public static List arrayToIntList ( JSONArray a )
	{
		final LinkedList list = new LinkedList ();
		if ( a != null )
		{
			forEachElement ( a, new ArrayVisitor ()
			{
				@Override
				public boolean visit ( Integer t ) throws JSONException
				{
					list.add ( t );
					return true;
				}
			});
		}
		return list;
	}

	public static  JSONArray listToArray ( Collection list )
	{
		return collectionToArray ( list );
	}

	public static  JSONArray collectionToArray ( Collection list )
	{
		final JSONArray a = new JSONArray ();
		for ( T o : list )
		{
			a.put ( o );
		}
		return a;
	}

	public static JSONObject mapOfStringsToObject ( Map list )
	{
		final JSONObject obj = new JSONObject ();
		for ( Map.Entry e : list.entrySet () )
		{
			obj.put ( e.getKey (), e.getValue () );
		}
		return obj;
	}

	public interface ObjectVisitor
	{
		/**
		 * Visit an entry.
		 * @param key
		 * @param t
		 * @return true to continue, false to exit the loop
		 * @throws JSONException
		 * @throws E
		 */
		boolean visit ( String key, T t ) throws JSONException, E;
	}

	@SuppressWarnings("unchecked")
	public static  void forEachElement ( JSONObject object, ObjectVisitor v ) throws JSONException, E
	{
		if ( object == null ) return;
		for ( Object keyObj : object.keySet () )
		{
			final String key = keyObj.toString ();
			final Object val = object.get ( key );
			if ( ! ( v.visit ( key, (T)(val) ) ) )
			{
				break;
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy