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

com.rathravane.till.nv.impl.nvJsonObject Maven / Gradle / Ivy

/*
 *	Copyright 2006-2013, Rathravane LLC
 *
 *	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.rathravane.till.nv.impl;

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

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

import com.rathravane.till.data.rrConvertor;
import com.rathravane.till.nv.rrNvWriteable;

public class nvJsonObject implements rrNvWriteable
{
	public nvJsonObject ()
	{
		fObject = new JSONObject ();
	}

	public nvJsonObject ( JSONObject o )
	{
		fObject = o;
	}

	@Override
	public String get ( String key )
	{
		return getString ( key, null );
	}

	@Override
	public String getString ( String key ) throws missingReqdSetting
	{
		try
		{
			final Object o = fObject.get ( key );
			if ( o != null )
			{
				return o.toString ();
			}
			throw new missingReqdSetting ( key );
		}
		catch ( JSONException e )
		{
			throw new missingReqdSetting ( key );
		}
	}

	@Override
	public String getString ( String key, String defValue )
	{
		return fObject.optString ( key, defValue );
	}

	@Override
	public char getCharacter ( String key )
		throws missingReqdSetting
	{
		try
		{
			final String s = fObject.getString ( key );
			if ( s.length () == 1 )
			{
				return s.charAt ( 0 );
			}
			throw new missingReqdSetting ( key );
		}
		catch ( JSONException e )
		{
			throw new missingReqdSetting ( key );
		}
	}

	@Override
	public char getCharacter ( String key, char defValue )
	{
		try
		{
			return getCharacter ( key );
		}
		catch ( missingReqdSetting e )
		{
			return defValue;
		}
	}

	@Override
	public boolean getBoolean ( String key )
		throws missingReqdSetting
	{
		try
		{
			return fObject.getBoolean ( key );
		}
		catch ( JSONException e )
		{
			throw new missingReqdSetting ( key );
		}
	}

	@Override
	public boolean getBoolean ( String key, boolean defValue )
	{
		return fObject.optBoolean ( key, defValue );
	}

	@Override
	public int getInt ( String key )
		throws missingReqdSetting
	{
		try
		{
			return fObject.getInt ( key );
		}
		catch ( JSONException e )
		{
			throw new missingReqdSetting ( key );
		}
	}

	@Override
	public int getInt ( String key, int defValue )
	{
		return fObject.optInt ( key, defValue );
	}

	@Override
	public long getLong ( String key )
		throws missingReqdSetting
	{
		try
		{
			return fObject.getLong ( key );
		}
		catch ( JSONException e )
		{
			throw new missingReqdSetting ( key );
		}
	}

	@Override
	public long getLong ( String key, long defValue )
	{
		return fObject.optLong ( key, defValue );
	}

	@Override
	public double getDouble ( String key )
		throws missingReqdSetting
	{
		try
		{
			return fObject.getDouble ( key );
		}
		catch ( JSONException e )
		{
			throw new missingReqdSetting ( key );
		}
	}

	@Override
	public double getDouble ( String key, double defValue )
	{
		return fObject.optDouble ( key, defValue );
	}

	@Override
	public byte[] getBytes ( String key )
		throws missingReqdSetting,
			invalidSettingValue
	{
		return rrConvertor.hexStringToBytes ( getString ( key ));
	}

	@Override
	public byte[] getBytes ( String key, byte[] defValue )
	{
		try
		{
			return getBytes ( key );
		}
		catch ( missingReqdSetting e )
		{
			return defValue;
		}
		catch ( invalidSettingValue e )
		{
			return defValue;
		}
	}

	@Override
	public String[] getStrings ( String key )
		throws missingReqdSetting
	{
		try
		{
			final JSONArray a = fObject.getJSONArray ( key );
			final int size = a.length ();
			final String[] result = new String [ size ];
			for ( int i=0; i getAllKeys ()
	{
		final LinkedList keys = new LinkedList ();
		for ( Object keyObj : fObject.keySet () )
		{
			keys.add ( keyObj.toString () );
		}
		return keys;
	}

	@Override
	public Map getCopyAsMap ()
	{
		final HashMap result = new HashMap ();
		copyInto ( result );
		return result;
	}

	@Override
	public void copyInto ( rrNvWriteable writeable )
	{
		for ( String key : getAllKeys () )
		{
			try
			{
				final Object o = fObject.get ( key );
				if ( o instanceof Integer )
				{
					writeable.set ( key, (Integer) o );
				}
				else if ( o instanceof Long )
				{
					writeable.set ( key, (Long) o );
				}
				else if ( o instanceof Double )
				{
					writeable.set ( key, (Double) o );
				}
				else if ( o instanceof Boolean )
				{
					writeable.set ( key, (Boolean) o );
				}
				else if ( o instanceof JSONArray )
				{
					writeable.set ( key, getStrings ( key ) );
				}
				else
				{
					writeable.set ( key, (String) o );
				}
				// FIXME: byte[] and char are converted to strings here
			}
			catch ( JSONException e )
			{
				throw new RuntimeException ( "error copying value for " + key, e ); 
			}
			catch ( missingReqdSetting e )
			{
				throw new RuntimeException ( "error copying value for " + key, e ); 
			}
		}
	}

	@Override
	public void copyInto ( Map writeable )
	{
		for ( String key : getAllKeys () )
		{
			try
			{
				writeable.put ( key, fObject.get ( key ).toString () );
			}
			catch ( JSONException e )
			{
				throw new RuntimeException ( "error copying value for " + key, e ); 
			}
		}
	}

	@Override
	public void rescan ()
	{
		// nothing to do
	}

	@Override
	public void clear ()
	{
		for ( String key : getAllKeys () )
		{
			fObject.remove ( key );
		}
	}

	@Override
	public synchronized void set ( String key, String value )
	{
		fObject.put ( key, value );
	}

	@Override
	public void set ( String key, char value )
	{
		fObject.put ( key, "" + value );
	}

	public synchronized void set ( String key, int value )
	{
		fObject.put ( key, value );
	}

	public synchronized void set ( String key, long value )
	{
		fObject.put ( key, value );
	}

	public synchronized void set ( String key, double value )
	{
		fObject.put ( key, value );
	}

	public synchronized void set ( String key, boolean value )
	{
		fObject.put ( key, value );
	}

	public synchronized void set ( Map map )
	{
		for ( Map.Entry e : map.entrySet () )
		{
			fObject.put ( e.getKey(), e.getValue() );
		}
	}

	@Override
	public synchronized void unset ( String key )
	{
		fObject.remove ( key );
	}

	@Override
	public synchronized void set ( String key, byte[] value )
	{
		set ( key, value, 0, value.length );
	}

	@Override
	public synchronized void set ( String key, byte[] value, int offset, int length )
	{
		set ( key, rrConvertor.bytesToHex ( value, offset, length ) );
	}

	@Override
	public void set ( String key, String[] values )
	{
		final JSONArray a = new JSONArray ();
		for ( String s : values )
		{
			a.put ( s );
		}
		fObject.put ( key, a );
	}

	private final JSONObject fObject;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy