org.kopitubruk.util.json
Class JSONReflectedClass

java.lang.Object
  extended by org.kopitubruk.util.json.JSONReflectedClass
All Implemented Interfaces:
Cloneable

public class JSONReflectedClass
extends Object
implements Cloneable

This class wraps a class to be explicitly reflected and allows you to choose the names of the fields to be reflected, regardless of privacy as well as set up aliases for field names in the JSON output. This gives you more precise control over what is shown. It should be created and then sent to JSONConfig.addReflectClass(Object) or JSONConfig.addReflectClasses(java.util.Collection) just like any other object for which you wish to use selective reflection.

If you do not specify field names (fieldNames is null or empty) then normal reflection will be done.

Using this object with explicit field names allows you a few abilities that normal reflection does not.

You can also specify a fieldAliases map so that field names are aliased in the JSON output to a custom name that you specify with the map. Any unmapped names will be left as is.

Since:
1.9
Author:
Bill Davidson

Constructor Summary
JSONReflectedClass(Object obj)
          Create a new JSONReflectedClass
JSONReflectedClass(Object obj, Collection<String> fieldNames)
          Create a new JSONReflectedClass
JSONReflectedClass(Object obj, Collection<String> fieldNames, Map<String,String> fieldAliases)
          Create a new JSONReflectedClass
JSONReflectedClass(Object obj, Map<String,String> fieldAliases)
          Create a new JSONReflectedClass
JSONReflectedClass(String className)
          Create a JSONReflectedClass using the given class name as a string optionally followed by field names and/or field name aliases.
 
Method Summary
 JSONReflectedClass clone()
           
 boolean equals(Object obj)
           
 Map<String,String> getFieldAliases()
          Get the field aliases map.
 Set<String> getFieldNames()
          Get a copy of the set of field names to reflect.
 Class<?> getObjClass()
          Get the class being reflected.
 int hashCode()
           
 void setFieldAliases(Map<String,String> fieldAliases)
          Set the custom names map.
 void setFieldNames(Collection<String> fieldNames)
          Set the set of field names to reflect.
 void setObjClass(Object obj)
          Set the object class.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONReflectedClass

public JSONReflectedClass(Object obj)
Create a new JSONReflectedClass

Parameters:
obj - An object of the type to be reflect or its class.

JSONReflectedClass

public JSONReflectedClass(Object obj,
                          Collection<String> fieldNames)
Create a new JSONReflectedClass

Parameters:
obj - An object of the type to be reflect or its class.
fieldNames - The names of the fields to include in the reflection. Internally, this gets converted to a Set which you can access via getFieldNames(). If the input collection has an iteration order, that order will be preserved in the JSON output.

JSONReflectedClass

public JSONReflectedClass(Object obj,
                          Map<String,String> fieldAliases)
Create a new JSONReflectedClass

Parameters:
obj - An object of the type to be reflect or its class.
fieldAliases - Map from object field names to custom names for output.

JSONReflectedClass

public JSONReflectedClass(Object obj,
                          Collection<String> fieldNames,
                          Map<String,String> fieldAliases)
Create a new JSONReflectedClass

Parameters:
obj - An object of the type to be reflect or its class.
fieldNames - The names of the fields to include in the reflection. Internally, this gets converted to a Set which you can access via getFieldNames(). If the input collection has an iteration order, that order will be preserved in the JSON output.
fieldAliases - Map from object field names to custom names for output.

JSONReflectedClass

public JSONReflectedClass(String className)
                   throws ClassNotFoundException
Create a JSONReflectedClass using the given class name as a string optionally followed by field names and/or field name aliases.

If you wish to use reflection with fields, you can append the field names to the class name, separated by commas before each field name. Field names which do not look like valid Java identifier names will be silently discarded. For example, if you want to reflect a class called "org.example.Widget" and it has fields called "a", "b" and "c" but you only want "a" and "c", then you can pass "org.example.Widget,a,c" to this method.

If you wish to use custom field names with reflection you can use name=alias pairs separated by commas as with the field names. For example, if you want to reflect a class called "org.example.Widget" and it has a field called "foo" but you want that field encoded as "bar" you can pass "org.example.Widget,foo=bar" to this method.

Parameters:
className - The name of the class suitable for ClassLoader.loadClass(String) followed optionally by a comma separated list of field names and/or field aliases.
Throws:
ClassNotFoundException - If the class cannot be loaded.
Since:
1.9.3
Method Detail

getObjClass

public Class<?> getObjClass()
Get the class being reflected.

Returns:
the objClass

setObjClass

public void setObjClass(Object obj)
Set the object class.

Parameters:
obj - An object of the type to be reflect or its class.

getFieldNames

public Set<String> getFieldNames()
Get a copy of the set of field names to reflect. Modifications to the returned Set will not affect reflection. You must use setFieldNames(Collection) in order to change the set of field names to reflect for this object.

Returns:
a copy of the list of field names to reflect.

setFieldNames

public void setFieldNames(Collection<String> fieldNames)
Set the set of field names to reflect. This silently discards any names that are not valid Java identifiers. The collection is copied so you cannot modify what is stored except by replacing the entire collection by using this method.

Parameters:
fieldNames - The field names to include in reflected JSON output. If the Collection that you send to this method has a set iteration order, that order will be preserved.

getFieldAliases

public Map<String,String> getFieldAliases()
Get the field aliases map.

Returns:
the fieldAliases

setFieldAliases

public void setFieldAliases(Map<String,String> fieldAliases)
Set the custom names map. Makes a copy of the map, trimming the keys and values and discarding keys that are not valid Java identifiers and values that don't have at least one character. The names being mapped from have to be valid Java identifiers. This does no validation to see if the aliases are valid ECMAScript or JSON identifiers other than to make sure that they have a length of at least one. It leaves validation to JSONUtil.toJSON(Object, JSONConfig, java.io.Writer) and the methods that it calls according to the configuration options that you've set. Anything that appears invalid will be silently discarded. If there are no valid aliases then the aliases map will be set to null.

Parameters:
fieldAliases - the fieldAliases to set

clone

public JSONReflectedClass clone()
Overrides:
clone in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object


Copyright © 2016. All rights reserved.