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

io.continual.util.data.json.JsonVisitor Maven / Gradle / Ivy

There is a newer version: 0.3.14
Show newest version
/*
 *	Copyright 2019, Continual.io
 *
 *	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 io.continual.util.data.json;

import java.util.Arrays;
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
{
	private JsonVisitor() {
	}
	
	public interface ArrayVisitor
	{
		/**
		 * Visit an array entry. Return false to stop iteration.
		 * @param t an item of type T
		 * @return if true, iteration continues
		 * @throws JSONException when an underlying JSON operation throws
		 * @throws E an additional exception type that your processing may throw
		 */
		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 HashMap objectToMap ( JSONObject obj, ItemRenderer ir )
	{
		return objectToMap ( obj, new KeyRenderer ()
		{
			@Override
			public String render ( String key ) { return key; }
		}, ir );
	}

	public static HashMap objectToMap ( JSONObject obj, KeyRenderer kr, ItemRenderer ir )
	{
		final HashMap map = new HashMap<> ();
		if ( obj != null )
		{
			for ( Object oo : obj.keySet () )
			{
				final String keyStr = oo.toString ();
				final K key = kr.render ( keyStr );
				final T val = ir.render ( obj.get ( keyStr ) );
				map.put ( key, val );
			}
		}
		return map;
	}

	public interface ObjectFilter
	{
		boolean matches ( JSONObject item );
	}
	
	public static List findMatchingObjects ( JSONArray a, ObjectFilter of )
	{
		final LinkedList result = new LinkedList<> ();
		forEachObjectIn ( a, new ArrayOfObjectVisitor ()
		{
			@Override
			public boolean visit ( JSONObject item ) throws JSONException
			{
				if ( of.matches ( item ) )
				{
					result.add ( item );
				}
				return true;
			}
		} );
		return result;
	}

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

		final int len = a.length ();
		for ( int i=0; i 
	{
		T read ( F val );
	};

	public static  List arrayToList ( JSONArray a, final ValueReader vr )
	{
		final LinkedList list = new LinkedList ();
		if ( a != null )
		{
			forEachElement ( a, new ArrayVisitor ()
			{
				@Override
				public boolean visit ( F t ) throws JSONException
				{
					list.add ( vr.read ( t ) );
					return true;
				}
			});
		}
		return list;
	}

	public static List 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 interface ItemRenderer
	{
		O render ( T t ) throws IllegalArgumentException;
	}

	public interface KeyRenderer
	{
		K render ( String key );
	}

	public static class StdItemRenderer implements ItemRenderer
	{
		@Override
		public Object render ( T t )
		{
			if ( t instanceof JsonSerialized )
			{
				return ((JsonSerialized)t).toJson ();
			}
			return t;
		}
	}

	public static  JSONArray listToArray ( String[] list )
	{
		return listToArray ( Arrays.asList ( list ) );
	}

	public static  JSONArray listToArray ( Collection list )
	{
		return listToArray ( list, new StdItemRenderer () );
	}

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

	public static  JSONArray collectionToArray ( Collection list )
	{
		return collectionToArray ( list, new StdItemRenderer () );
	}
	
	public static  JSONArray collectionToArray ( Collection list, ItemRenderer renderer )
	{
		if ( list == null ) return null;

		final JSONArray a = new JSONArray ();
		for ( T o : list )
		{
			a.put ( renderer.render ( 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 static JSONObject mapToObject ( Map list )
	{
		return mapToObject ( list, i -> i );
	}

	public static JSONObject mapOfJsonToObject ( Map list )
	{
		return mapToObject ( list, i -> i.toJson () );
	}

	public static  JSONObject mapToObject ( Map list, ItemRenderer renderer )
	{
		return mapToObject ( list, k -> k, renderer );
	}

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

	public interface ObjectVisitor
	{
		/**
		 * Visit an entry.
		 * @param key the string key of the entry
		 * @param t the value of type T of the entry
		 * @return true to continue, false to exit the loop
		 * @throws JSONException when an underlying JSON operation throws
		 * @throws E an additional exception type your code may throw
		 */
		boolean visit ( String key, T t ) throws JSONException, E;
	}

	public static  void forEachElement ( JSONObject object, ObjectVisitor v ) throws JSONException, E
	{
		forEachElement ( object, v, false );
	}

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

			if ( val == JSONObject.NULL && allowNulls )
			{
				if ( ! ( v.visit ( key, null ) ) )
				{
					break;
				}
			}
			else if ( ! ( v.visit ( key, (T)(val) ) ) )
			{
				break;
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy