public class JSONReflectedClass extends Object implements Cloneable
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.
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
public JSONReflectedClass(Object obj)
obj - An object of the type to be reflect or its class.public JSONReflectedClass(Object obj, Collection<String> fieldNames)
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.public JSONReflectedClass(Object obj, Map<String,String> fieldAliases)
obj - An object of the type to be reflect or its class.fieldAliases - Map from object field names to custom names for output.public JSONReflectedClass(Object obj, Collection<String> fieldNames, Map<String,String> fieldAliases)
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.public JSONReflectedClass(String className) throws ClassNotFoundException
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.
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.ClassNotFoundException - If the class cannot be loaded.public Class<?> getObjClass()
public void setObjClass(Object obj)
obj - An object of the type to be reflect or its class.public Set<String> getFieldNames()
Set will not affect reflection. You must use
setFieldNames(Collection) in order to change the set of field
names to reflect for this object.public void setFieldNames(Collection<String> fieldNames)
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.public Map<String,String> getFieldAliases()
public void setFieldAliases(Map<String,String> fieldAliases)
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.fieldAliases - the fieldAliases to setpublic JSONReflectedClass clone()
Copyright © 2016. All rights reserved.