org.kopitubruk.util.json
Class JSONConfigDefaults

java.lang.Object
  extended by org.kopitubruk.util.json.JSONConfigDefaults
All Implemented Interfaces:
Serializable, JSONConfigDefaultsMBean

public class JSONConfigDefaults
extends Object
implements JSONConfigDefaultsMBean, Serializable

This class provides a singleton object which is used to change static defaults used by JSONConfig and it is used as an MBean to allow JMX clients with MBean support to view and modify the defaults.

Keep in mind that changes made with this class affect all new JSONConfig objects created in the same class loader, including those created by toJSON() methods which don't take a JSONConfig, which could have undesirable side effects depending upon your app. Use with care, if at all. All data in this class is static. The only reason that there is an instance or instance methods is to support MBean access to the defaults. A few defaults are only available programmatically. Those are accessed statically.

It is possible to configure most of the default values via JNDI such as is typically available in a web application by adding values to your application's environment under java:/comp/env/org/kopitubruk/util/json. That way you can do things like having all of the validation turned on in development and testing servers and turn it off in production servers for faster performance, without changing any code.

Example for Tomcat, assuming your app is named "MyApp", and your host is named "host", put this into your $CATALINA_BASE/conf/Catalina/host/MyApp.xml file in order to set the appName to "myApp", disable property name validation and enable using full JSON identifier code points by default:

<Context path="/MyApp">
   <Environment name="org/kopitubruk/util/json/appName" type="java.lang.String" value="MyApp" override="false" />
   <Environment name="org/kopitubruk/util/json/validatePropertyNames" type="java.lang.Boolean" value="false" override="false" />
   <Environment name="org/kopitubruk/util/json/fullJSONIdentifierCodePoints" type="java.lang.Boolean" value="true" override="false" />
 </Context>

Because the appName tends to remain consistent across all deployments, it might be preferable to put it into your webapp's web.xml. This should work with all JEE web tier containers. There is a different syntax for adding context environment variables in web.xml:

<env-entry>
   <env-entry-name>org/kopitubruk/util/json/appName</env-entry-name>
   <env-entry-type>java.lang.String</env-entry-type>
   <env-entry-value>MyApp</env-entry-value>
 </env-entry>

You can use this method for any other JNDI environment variables provided that you always want them the same on all deployments.

These are the names and their normal defaults if you don't change them. See their setters for these for descriptions of what they do.

Validation related options.

Safe alternate encoding options.

Allow generation of certain types of non-standard JSON.

Could cause problems for some things that take JSON. Defaults are for standard JSON. Be careful about changing these.

It is possible to set the default locale in JNDI using the name "locale" and a String value that is a valid locale string that will be used to create a Locale. If you don't do this, then the default locale will be whatever is provided by the JVM.

You can set the default date generation format in JNDI using the name "dateGenFormat" and a String value that will be passed to SimpleDateFormat.SimpleDateFormat(String, Locale) using the locale from getLocale().

You can set any number of default date parsing formats using names that start with "dateParseFormat" followed by some positive base 10 integer (as in "dateParseFormat7") using String values as with "dateGenFormat" described above. They will be added in numeric order of the name.

Classes to use for automatic reflection can be set up via JNDI. This class will look for String variables named "reflectClass" followed by some positive base 10 integer (like "reflectClass5"). You can have any number of reflectClasses. The names merely need to be unique and follow that pattern. The class names need to load with ClassLoader.loadClass(String) or they will not be added to the list of reflected classes. If the class names are followed by a comma and strings separated by commas, those strings will be taken as field names to use for JSONReflectedClass. Field names which do not look like valid Java identifier names will be silently discarded. If the field names are of the form "name=alias" then a field called "name" will be represented as "alias" in the output. Aliases do not effect field selection so you can have aliases without selection. If you want to specify only field "foo" and you want it called "bar" then you will need to specify "foo,foo=bar" in the string. These reflect classes will be added to all JSONConfig objects that are created in the same class loader.

Number formats and date formats are cloned when they are added because they are not thread safe. They are cloned again when applied to a new JSONConfig for the same reason. Once you add a format, you can't modify it except by replacing it entirely.

It is possible to see and modify the default values of all of the boolean flags at runtime if you have a JMX client with MBean support connected to your server. It will be in org.kopitubruk.util.json.JSONConfigDefaults. You can disable MBean registration by setting a boolean variable named registerMBean to false in the environment as shown above for the flags. See also the clearMBean() method for information on how to remove the MBean from your server when your app is unloaded or reloaded.

There is some limited logging for access of JNDI and the MBean server. Most of it is debug, so you won't see it unless you have debug logging enabled for this package/class. It might be useful to enable debug logging for this class if you are having trouble with those.

You can disable JNDI lookups, MBean registration or logging by defining boolean system properties for org.kopitubruk.util.json.useJNDI, org.kopitubruk.util.json.registerMBean or org.kopitubruk.util.json.logging respectively as false. System properties may be set on the java command line using the "-D" flag or possibly programmatically if your program has permission to do it and does so before this class is loaded. If logging is not set via a system property at all, then JDNI can also be used to disable logging by setting the boolean named "org/kopitubruk/util/json/logging".

You can also set the appName on the command line if you didn't set it in JNDI as in "-Dorg.kopitubruk.util.json.appName=MyApp". The appName is used in the MBean ObjectName and is recommended when this library is used with multiple apps in the same web tier container because it allows you to have different MBeans for different apps at the same time. The appName may also be set via JNDI as a String and that's probably a better way to do it.

Author:
Bill Davidson
See Also:
JSONConfig, Serialized Form

Method Summary
static void addDateParseFormat(DateFormat fmt)
          Add a date parsing format to the list of parsing formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true.
 DateFormat addDateParseFormat(String fmtStr)
          Add a date parsing format to the list of date parsing formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true.
static void addDateParseFormats(Collection<? extends DateFormat> fmts)
          Add a collection of date parsing format to the list of date parsing formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true.
static void addNumberFormat(Class<? extends Number> numericClass, NumberFormat fmt)
          Add a default number format for a particular type that extends Number.
static void addNumberFormat(Number numericType, NumberFormat fmt)
          Add a default number format for a particular type that extends Number.
static void addNumberFormats(Map<Class<? extends Number>,NumberFormat> numFmtMap)
          Add a map of number formats to the current map of number formats.
static void addReflectClass(Object obj)
          Add the class of the given object to the set of classes that automatically get reflected.
 void addReflectClassByName(String className)
          Add the given class to the set of classes to be reflected.
static void addReflectClasses(Collection<?> classes)
          Add the classes of all of the given objects to the list of classes that automatically get reflected.
 void clearDateGenFormat()
          Clear date generation format.
 void clearDateParseFormats()
          Clear any date parse formats from the list of formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true.
static void clearMBean()
           When this package is used by a webapp, and you have an MBean server in your environment, then you should create a ServletContextListener and call this in its ServletContextListener.contextDestroyed(ServletContextEvent) method to remove the MBean when the webapp is unloaded or reloaded.
 void clearNumberFormats()
          Clear any default number formats.
 void clearReflectClasses()
          Clear all reflection classes, disabling all default automatic selective reflection.
 void clearReflectionCache()
          Clear the reflection cache, if any.
static IndentPadding getIndentPadding()
          Get the default indent padding object.
static JSONConfigDefaults getInstance()
          Return the JSONConfigDefaults singleton instance.
static Locale getLocale()
          Get the default locale for new JSONConfig objects.
 String getLocaleLanguageTag()
          Get the A IETF BCP 47 language tag version of the current default locale.
static NumberFormat getNumberFormat(Class<? extends Number> numericClass)
          Get the number format for the given class.
static NumberFormat getNumberFormat(Number num)
          Get the number format for the class of the given numeric type.
static JSONReflectedClass getReflectedClass(Object obj)
          Get the JSONReflectedClass for the given object if it is stored.
 int getReflectionPrivacy()
          Get the reflection privacy level.
 int getUndefinedCodePointPolicy()
          Get the default policy for undefined code points.
 int getUnmatchedSurrogatePolicy()
          Get the default policy for unmatched surrogates.
 boolean isAllowReservedWordsInIdentifiers()
          Get the default for allowing reserved words in identifiers.
 boolean isCacheReflectionData()
          Get the default cacheReflectionData policy.
 boolean isDetectDataStructureLoops()
          Get the default detect data structure loops policy.
 boolean isEncodeDatesAsObjects()
          Get the encode dates as objects policy.
 boolean isEncodeDatesAsStrings()
          Get the encode dates as strings policy.
 boolean isEncodeNumericStringsAsNumbers()
          Get the default encode numeric strings as numbers policy.
 boolean isEscapeBadIdentifierCodePoints()
          Get the default escape bad identifier code points policy.
 boolean isEscapeNonAscii()
          Get the default escape non-ASCII policy.
 boolean isEscapeSurrogates()
          Get the default escape surrogates policy.
 boolean isFastStrings()
          Get the fastStrings policy.
 boolean isFullJSONIdentifierCodePoints()
          Get the full JSON identifier code points policy.
 boolean isPassThroughEscapes()
          Get the pass through escapes policy.
 boolean isPreciseNumbers()
          Get the default preciseIntegers policy.
 boolean isQuoteIdentifier()
          Get the default quote identifier policy.
static boolean isReflectClass(Object obj)
          Return true if the given class is in the set of classes being automatically reflected.
 boolean isReflectUnknownObjects()
          Get the default reflection of unknown objects policy.
 boolean isSmallNumbers()
          Get the default smallNumbers policy.
 boolean isUnEscapeWherePossible()
          Get the default unEscape policy.
 boolean isUseECMA6()
          Get the default escape ECMAScript 6 code points policy.
 boolean isUsePrimitiveArrays()
          The default primitive arrays policy.
 boolean isValidatePropertyNames()
          Get the default validate property names policy.
 String listReflectedClasses()
          Get a string with newline separated list of classes that get reflected.
static void removeNumberFormat(Class<? extends Number> numericClass)
          Remove the requested class from the default number formats.
static void removeNumberFormat(Number num)
          Remove the requested class from the default number formats.
static void removeReflectClass(Object obj)
          Remove the given class from the list of automatically reflected classes.
 void removeReflectClassByName(String className)
          Remove the given class from the set of classes to be reflected.
static void removeReflectClasses(Collection<?> classes)
          Remove the classes of the given objects from the set of classes that automatically get reflected.
 void setAllowReservedWordsInIdentifiers(boolean dflt)
          Set default flag for allowing reserved words in identifiers.
static void setBadCharacterPolicy(int badCharacterPolicy)
          Convenience method to call both setUnmatchedSurrogatePolicy(int) and setUndefinedCodePointPolicy(int) using the same value.
 void setCacheReflectionData(boolean dflt)
          If true, then when an object is reflected its reflection data will be cached to improve performance on subsequent reflections of objects of its class.
 void setCodeDefaults()
          Reset all defaults to their original unmodified values.
static void setDateGenFormat(DateFormat fmt)
          Set the date string generation format used when encodeDatesAsStrings or encodeDatesAsObjects is true.
 DateFormat setDateGenFormat(String fmtStr)
          Set the date format used for date string generation when encodeDatesAsStrings or encodeDatesAsObjects is true.
 void setDetectDataStructureLoops(boolean dflt)
          Set the default flag for detecting data structure loops.
 void setEncodeDatesAsObjects(boolean dflt)
          If true, then Date objects will be encoded as Javascript dates, using new Date(dateString).
 void setEncodeDatesAsStrings(boolean dflt)
          Set the encodeDatesAsStrings policy.
 void setEncodeNumericStringsAsNumbers(boolean dflt)
          Set the default flag for encoding of numeric strings as numbers.
 void setEscapeBadIdentifierCodePoints(boolean dflt)
          If true, then any bad code points in identifiers will be escaped.
 void setEscapeNonAscii(boolean dflt)
          Set the default flag for forcing escaping of non-ASCII characters in strings and identifiers.
 void setEscapeSurrogates(boolean dflt)
          Set the default escapeSurrogates policy.
 void setFastStrings(boolean dflt)
          If true, then string values will be copied to the output with no escaping or validation.
 void setFullJSONIdentifierCodePoints(boolean dflt)
          If true, then the full set of identifier code points permitted by the JSON standard will be allowed instead of the more restrictive set permitted by the ECMAScript standard.
static void setIndentPadding(IndentPadding indentPadding)
          Set the padding object.
static void setLocale(Locale loc)
          Set a default locale for new JSONConfig objects to use.
 void setLocale(String languageTag)
          Deprecated. Use setLocaleLanguageTag(String) instead.
 void setLocaleLanguageTag(String languageTag)
          Set the default locale for new JSONConfig objects to use by specifying a IETF BCP 47 language tag.
 void setPassThroughEscapes(boolean dflt)
          If true, then escapes in strings will be passed through unchanged.
 void setPreciseNumbers(boolean dflt)
          If true then numbers which are not exactly representable by a 64 bit double precision floating point number will be quoted in the output.
 void setQuoteIdentifier(boolean dflt)
          Set the default flag for forcing quotes on identifiers.
 void setReflectionPrivacy(int dflt)
          Set the privacy level for reflection.
 void setReflectUnknownObjects(boolean dflt)
          Set the default unknown object reflection encoding policy.
 void setSmallNumbers(boolean dflt)
          If true then JSONParser will attempt to minimize the storage used for all numbers.
 void setUndefinedCodePointPolicy(int dflt)
          Tell JSONUtil what to do by default when it encounters undefined code points in strings and identifiers.
 void setUnEscapeWherePossible(boolean dflt)
          Set default flag for undoing inline escapes in strings.
 void setUnmatchedSurrogatePolicy(int dflt)
          Tell JSONUtil what to do by default when it encounters unmatched surrogates in strings and identifiers.
 void setUseECMA6(boolean dflt)
          If you set this to true, then by default when JSONUtil generates Unicode escapes, it will use ECMAScript 6 code point escapes if they are shorter than code unit escapes.
 void setUsePrimitiveArrays(boolean dflt)
          If true, then when JSONParser encounters a JSON array of non-null wrappers of primitives and those primitives are all compatible with each other, then instead of an ArrayList of wrappers for those primitives it will create an array of those primitives in order to save memory.
 void setValidatePropertyNames(boolean dflt)
          Set the default flag for validation of property names.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clearMBean

public static void clearMBean()

When this package is used by a webapp, and you have an MBean server in your environment, then you should create a ServletContextListener and call this in its ServletContextListener.contextDestroyed(ServletContextEvent) method to remove the MBean when the webapp is unloaded or reloaded.


 public class AppCleanUp implements ServletContextListener
 {
     public void contextDestroyed( ServletContextEvent sce )
     {
         JSONConfigDefaults.clearMBean();
     }
 }
 

You should add it to your web.xml for your webapp like this (assuming you named it org.myDomain.web.app.AppCleanUp).

<listener>
   <listener-class>org.myDomain.web.app.AppCleanUp</listener-class>
 </listener>

Note that the logging from this method may not work if the logging is removed/disabled before this method is called.


getInstance

public static JSONConfigDefaults getInstance()
Return the JSONConfigDefaults singleton instance.

Returns:
the JSONConfigDefaults singleton instance.

setCodeDefaults

public void setCodeDefaults()
Reset all defaults to their original unmodified values. This overrides JNDI and previous MBean changes.

Accessible via MBean server.

Specified by:
setCodeDefaults in interface JSONConfigDefaultsMBean

getLocaleLanguageTag

public String getLocaleLanguageTag()
Get the A IETF BCP 47 language tag version of the current default locale.

Accessible via MBean server.

Specified by:
getLocaleLanguageTag in interface JSONConfigDefaultsMBean
Returns:
The IETF BCP 47 language tag of the default locale.

setLocaleLanguageTag

public void setLocaleLanguageTag(String languageTag)
Set the default locale for new JSONConfig objects to use by specifying a IETF BCP 47 language tag.

Accessible via MBean server.

Specified by:
setLocaleLanguageTag in interface JSONConfigDefaultsMBean
Parameters:
languageTag - A IETF BCP 47 language tag.

setLocale

@Deprecated
public void setLocale(String languageTag)
Deprecated. Use setLocaleLanguageTag(String) instead.

Set the default locale for new JSONConfig objects to use.

Parameters:
languageTag - A language tag.

getLocale

public static Locale getLocale()
Get the default locale for new JSONConfig objects. If a default locale has not been set, then the locale returned by Locale.getDefault() will be returned.

Returns:
the default locale.
See Also:
JSONConfig.getLocale()

setLocale

public static void setLocale(Locale loc)
Set a default locale for new JSONConfig objects to use.

Parameters:
loc - the default locale.
See Also:
JSONConfig.setLocale(Locale)

getNumberFormat

public static NumberFormat getNumberFormat(Class<? extends Number> numericClass)
Get the number format for the given class.

Parameters:
numericClass - A class.
Returns:
A number format or null if one has not been set.
See Also:
JSONConfig.getNumberFormat(Class)

getNumberFormat

public static NumberFormat getNumberFormat(Number num)
Get the number format for the class of the given numeric type.

Parameters:
num - An object that implements Number.
Returns:
A number format or null if one has not been set.
See Also:
JSONConfig.getNumberFormat(Number)

addNumberFormat

public static void addNumberFormat(Class<? extends Number> numericClass,
                                   NumberFormat fmt)
Add a default number format for a particular type that extends Number. This will be applied to all new JSONConfig objects that are created after this in the same class loader. The format is cloned for thread safety.

Parameters:
numericClass - The class.
fmt - The number format.
See Also:
JSONConfig.addNumberFormat(Class, NumberFormat)

addNumberFormat

public static void addNumberFormat(Number numericType,
                                   NumberFormat fmt)
Add a default number format for a particular type that extends Number. This will be applied to all new JSONConfig objects that are created after this in the same class loader. The format is cloned for thread safety.

Parameters:
numericType - The object.
fmt - The number format.
See Also:
JSONConfig.addNumberFormat(Number, NumberFormat)

addNumberFormats

public static void addNumberFormats(Map<Class<? extends Number>,NumberFormat> numFmtMap)
Add a map of number formats to the current map of number formats. The formats are cloned for thread safety.

Parameters:
numFmtMap - The input map.
Since:
1.4
See Also:
JSONConfig.addNumberFormats(Map)

removeNumberFormat

public static void removeNumberFormat(Class<? extends Number> numericClass)
Remove the requested class from the default number formats.

Parameters:
numericClass - The class.
See Also:
JSONConfig.removeNumberFormat(Class)

removeNumberFormat

public static void removeNumberFormat(Number num)
Remove the requested class from the default number formats.

Parameters:
num - An object that implements Number.
See Also:
JSONConfig.removeNumberFormat(Number)

clearNumberFormats

public void clearNumberFormats()
Clear any default number formats.

Accessible via MBean server.

Specified by:
clearNumberFormats in interface JSONConfigDefaultsMBean
Since:
1.4
See Also:
JSONConfig.clearNumberFormats()

setDateGenFormat

public DateFormat setDateGenFormat(String fmtStr)
Set the date format used for date string generation when encodeDatesAsStrings or encodeDatesAsObjects is true.

Accessible via MBean server.

Specified by:
setDateGenFormat in interface JSONConfigDefaultsMBean
Parameters:
fmtStr - passed to the constructor for SimpleDateFormat.SimpleDateFormat(String,Locale) using the result of getLocale().
Returns:
the format that is created so that it can be modified.
Since:
1.4
See Also:
JSONConfig.setDateGenFormat(String)

setDateGenFormat

public static void setDateGenFormat(DateFormat fmt)
Set the date string generation format used when encodeDatesAsStrings or encodeDatesAsObjects is true.

Parameters:
fmt - the dateFormat to set
Since:
1.4
See Also:
JSONConfig.setDateGenFormat(DateFormat)

clearDateGenFormat

public void clearDateGenFormat()
Clear date generation format. This means that if any special date generation handling is enabled, then it will use the default ISO 8601 format.

Accessible via MBean server.

Specified by:
clearDateGenFormat in interface JSONConfigDefaultsMBean
Since:
1.4
See Also:
JSONConfig.clearDateGenFormat()

addDateParseFormat

public DateFormat addDateParseFormat(String fmtStr)
Add a date parsing format to the list of date parsing formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true.

Accessible via MBean server.

Specified by:
addDateParseFormat in interface JSONConfigDefaultsMBean
Parameters:
fmtStr - Passed to SimpleDateFormat.SimpleDateFormat(String,Locale) using the result of getLocale().
Returns:
The format that gets created so that it can be modified.
Since:
1.4
See Also:
JSONConfig.addDateParseFormat(String)

addDateParseFormat

public static void addDateParseFormat(DateFormat fmt)
Add a date parsing format to the list of parsing formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true. When parsing date strings, they will be used in the same order that they were added.

Parameters:
fmt - A date parsing format.
Since:
1.4
See Also:
JSONConfig.addDateParseFormat(DateFormat)

addDateParseFormats

public static void addDateParseFormats(Collection<? extends DateFormat> fmts)
Add a collection of date parsing format to the list of date parsing formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true.

Parameters:
fmts - A collection of date parsing formats.
Since:
1.4
See Also:
JSONConfig.addDateParseFormats(Collection)

clearDateParseFormats

public void clearDateParseFormats()
Clear any date parse formats from the list of formats used by the parser when encodeDatesAsStrings or encodeDatesAsObjects is true.

Accessible via MBean server.

Specified by:
clearDateParseFormats in interface JSONConfigDefaultsMBean
See Also:
JSONConfig.clearDateParseFormats()

getIndentPadding

public static IndentPadding getIndentPadding()
Get the default indent padding object.

Returns:
the padding object.
Since:
1.7
See Also:
JSONConfig.getIndentPadding()

setIndentPadding

public static void setIndentPadding(IndentPadding indentPadding)
Set the padding object.

Parameters:
indentPadding - the default indent padding object.
Since:
1.7
See Also:
JSONConfig.setIndentPadding(IndentPadding)

getReflectionPrivacy

public int getReflectionPrivacy()
Get the reflection privacy level.

Accessible via MBean server.

Specified by:
getReflectionPrivacy in interface JSONConfigDefaultsMBean
Returns:
the reflection privacy level.
Since:
1.9
See Also:
ReflectUtil.PRIVATE, ReflectUtil.PACKAGE, ReflectUtil.PROTECTED, ReflectUtil.PUBLIC, JSONConfig.getReflectionPrivacy()

setReflectionPrivacy

public void setReflectionPrivacy(int dflt)
                          throws MBeanException
Set the privacy level for reflection. Default is ReflectUtil.PUBLIC.

Accessible via MBean server.

Specified by:
setReflectionPrivacy in interface JSONConfigDefaultsMBean
Parameters:
dflt - the level to set
Throws:
MBeanException - If the privacy level is not allowed.
Since:
1.9
See Also:
ReflectUtil.PRIVATE, ReflectUtil.PACKAGE, ReflectUtil.PROTECTED, ReflectUtil.PUBLIC, JSONConfig.setReflectionPrivacy(int)

isReflectClass

public static boolean isReflectClass(Object obj)
Return true if the given class is in the set of classes being automatically reflected.

Parameters:
obj - An object to check
Returns:
true if objects of the given type are reflected.
Since:
1.9
See Also:
JSONConfig.isReflectClass(Object)

getReflectedClass

public static JSONReflectedClass getReflectedClass(Object obj)
Get the JSONReflectedClass for the given object if it is stored. The main reason that you might want to use this is to modify the fields or aliases that are reflected in the class.

Parameters:
obj - The class to look up.
Returns:
the reflected class object or null if not found.
See Also:
JSONConfig.getReflectedClass(Object)

addReflectClassByName

public void addReflectClassByName(String className)
                           throws MBeanException
Add the given class to the set of classes to be reflected.

Accessible via MBean server.

This method primarily exists for JMX MBean use.

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.

Specified by:
addReflectClassByName in interface JSONConfigDefaultsMBean
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:
MBeanException - If there's a problem loading the class.
Since:
1.9
See Also:
JSONConfig.addReflectClassByName(String)

addReflectClass

public static void addReflectClass(Object obj)
Add the class of the given object to the set of classes that automatically get reflected. Note that default reflected classes can also be added via JNDI. If the object is an array, Iterable or Enumeration, then all objects in it will be added.

Parameters:
obj - The object whose class to add to the reflect list.
Since:
1.9
See Also:
JSONReflectedClass, JSONConfig.addReflectClass(Object)

addReflectClasses

public static void addReflectClasses(Collection<?> classes)
Add the classes of all of the given objects to the list of classes that automatically get reflected. Note that default reflected classes can also be added via JNDI.

Parameters:
classes - The objects to reflect.
Since:
1.9
See Also:
JSONReflectedClass, JSONConfig.addReflectClasses(Collection)

removeReflectClassByName

public void removeReflectClassByName(String className)
                              throws MBeanException
Remove the given class from the set of classes to be reflected.

Specified by:
removeReflectClassByName in interface JSONConfigDefaultsMBean
Parameters:
className - The name of the class suitable for (@link ClassLoader.loadClass(String)}.
Throws:
MBeanException - If there's a problem loading the class.
Since:
1.9

removeReflectClass

public static void removeReflectClass(Object obj)
Remove the given class from the list of automatically reflected classes. If the object is an array, Iterable or Enumeration, then all objects in it will be removed.

Parameters:
obj - An object of the type to be removed from the reflect list.
Since:
1.9
See Also:
JSONConfig.removeReflectClass(Object)

removeReflectClasses

public static void removeReflectClasses(Collection<?> classes)
Remove the classes of the given objects from the set of classes that automatically get reflected.

Parameters:
classes - The classes to remove.
Since:
1.9
See Also:
JSONConfig.removeReflectClasses(Collection)

clearReflectClasses

public void clearReflectClasses()
Clear all reflection classes, disabling all default automatic selective reflection.

Accessible via MBean server.

Specified by:
clearReflectClasses in interface JSONConfigDefaultsMBean
Since:
1.9
See Also:
JSONConfig.clearReflectClasses()

clearReflectionCache

public void clearReflectionCache()
Clear the reflection cache, if any.

Accessible via MBean server.

Specified by:
clearReflectionCache in interface JSONConfigDefaultsMBean
Since:
1.9

listReflectedClasses

public String listReflectedClasses()
Get a string with newline separated list of classes that get reflected.

Accessible via MBean server.

Specified by:
listReflectedClasses in interface JSONConfigDefaultsMBean
Returns:
A string with newline separated list of classes that get reflected.
Since:
1.9

getUnmatchedSurrogatePolicy

public int getUnmatchedSurrogatePolicy()
Get the default policy for unmatched surrogates.

Accessible via MBean server.

Specified by:
getUnmatchedSurrogatePolicy in interface JSONConfigDefaultsMBean
Returns:
the default policy for unmatched surrogates.
See Also:
JSONConfig.REPLACE, JSONConfig.DISCARD, JSONConfig.EXCEPTION, JSONConfig.ESCAPE, JSONConfig.PASS, JSONConfig.getUnmatchedSurrogatePolicy()

setUnmatchedSurrogatePolicy

public void setUnmatchedSurrogatePolicy(int dflt)
Tell JSONUtil what to do by default when it encounters unmatched surrogates in strings and identifiers. The permitted values are: Any other value will be ignored.

Accessible via MBean server.

Specified by:
setUnmatchedSurrogatePolicy in interface JSONConfigDefaultsMBean
Parameters:
dflt - the default unmatchedSurrogatePolicy to set
See Also:
JSONConfig.setUnmatchedSurrogatePolicy(int)

getUndefinedCodePointPolicy

public int getUndefinedCodePointPolicy()
Get the default policy for undefined code points.

Accessible via MBean server.

Specified by:
getUndefinedCodePointPolicy in interface JSONConfigDefaultsMBean
Returns:
the policy for undefined code points.
See Also:
JSONConfig.REPLACE, JSONConfig.DISCARD, JSONConfig.EXCEPTION, JSONConfig.ESCAPE, JSONConfig.PASS, JSONConfig.getUndefinedCodePointPolicy()

setUndefinedCodePointPolicy

public void setUndefinedCodePointPolicy(int dflt)
Tell JSONUtil what to do by default when it encounters undefined code points in strings and identifiers. The permitted values are: Any other value will be ignored.

Accessible via MBean server.

Specified by:
setUndefinedCodePointPolicy in interface JSONConfigDefaultsMBean
Parameters:
dflt - the default undefinedCodePointPolicy to set
See Also:
JSONConfig.setUndefinedCodePointPolicy(int)

setBadCharacterPolicy

public static void setBadCharacterPolicy(int badCharacterPolicy)
Convenience method to call both setUnmatchedSurrogatePolicy(int) and setUndefinedCodePointPolicy(int) using the same value.

Parameters:
badCharacterPolicy - the badCharacterPolicy to set
See Also:
JSONConfig.REPLACE, JSONConfig.DISCARD, JSONConfig.EXCEPTION, JSONConfig.ESCAPE, JSONConfig.PASS, JSONConfig.setBadCharacterPolicy(int)

isValidatePropertyNames

public boolean isValidatePropertyNames()
Get the default validate property names policy.

Accessible via MBean server.

Specified by:
isValidatePropertyNames in interface JSONConfigDefaultsMBean
Returns:
The default validate property names policy.
See Also:
JSONConfig.isValidatePropertyNames()

setValidatePropertyNames

public void setValidatePropertyNames(boolean dflt)
Set the default flag for validation of property names. This will affect all new JSONConfig objects created after this call within the same class loader.

Accessible via MBean server.

Specified by:
setValidatePropertyNames in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then property names will be validated by default.
See Also:
JSONConfig.setValidatePropertyNames(boolean)

isDetectDataStructureLoops

public boolean isDetectDataStructureLoops()
Get the default detect data structure loops policy. Accessible via MBean server.

Specified by:
isDetectDataStructureLoops in interface JSONConfigDefaultsMBean
Returns:
The default detect data structure loops policy.
See Also:
JSONConfig.isDetectDataStructureLoops()

setDetectDataStructureLoops

public void setDetectDataStructureLoops(boolean dflt)
Set the default flag for detecting data structure loops. If true, then if a loop in a data structure is found then a DataStructureLoopException will be thrown. This will affect all new JSONConfig objects created after this call within the same class loader.

Accessible via MBean server.

Specified by:
setDetectDataStructureLoops in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then the code will detect loops in data structures.
See Also:
JSONConfig.setDetectDataStructureLoops(boolean)

isEscapeBadIdentifierCodePoints

public boolean isEscapeBadIdentifierCodePoints()
Get the default escape bad identifier code points policy.

Accessible via MBean server.

Specified by:
isEscapeBadIdentifierCodePoints in interface JSONConfigDefaultsMBean
Returns:
The default escape bad identifier code points policy.
See Also:
JSONConfig.isEscapeBadIdentifierCodePoints()

setEscapeBadIdentifierCodePoints

public void setEscapeBadIdentifierCodePoints(boolean dflt)
If true, then any bad code points in identifiers will be escaped. Default is false.

Accessible via MBean server.

Specified by:
setEscapeBadIdentifierCodePoints in interface JSONConfigDefaultsMBean
Parameters:
dflt - if true, then any bad code points in identifiers will be escaped.
See Also:
JSONConfig.setEscapeBadIdentifierCodePoints(boolean)

isFullJSONIdentifierCodePoints

public boolean isFullJSONIdentifierCodePoints()
Get the full JSON identifier code points policy.

Accessible via MBean server.

Specified by:
isFullJSONIdentifierCodePoints in interface JSONConfigDefaultsMBean
Returns:
the fullJSONIdentifierCodePoints
See Also:
JSONConfig.isFullJSONIdentifierCodePoints()

setFullJSONIdentifierCodePoints

public void setFullJSONIdentifierCodePoints(boolean dflt)
If true, then the full set of identifier code points permitted by the JSON standard will be allowed instead of the more restrictive set permitted by the ECMAScript standard. Use of characters not permitted by the ECMAScript standard will cause an error if parsed by Javascript eval().

Accessible via MBean server.

Specified by:
setFullJSONIdentifierCodePoints in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then allow all code points permitted by the JSON standard in identifiers.
See Also:
JSONConfig.setFullJSONIdentifierCodePoints(boolean)

isFastStrings

public boolean isFastStrings()
Get the fastStrings policy.

Specified by:
isFastStrings in interface JSONConfigDefaultsMBean
Returns:
the fastStrings policy
See Also:
JSONConfig.isFastStrings()

setFastStrings

public void setFastStrings(boolean dflt)
If true, then string values will be copied to the output with no escaping or validation.

Only use this if you know that you have no unescaped characters in the range U+0000-U+001F or unescaped backslash or forward slash or double quote in your strings. If you want your JSON to be parsable by Javascript eval() then you also need to make sure that you don't have U+2028 (line separator) or U+2029 (paragraph separator).

That said, if you are encoding a lot of large strings, this can improve performance by eliminating the check for characters that need to be escaped.

Specified by:
setFastStrings in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then string values will be copied as is with no escaping or validation.
See Also:
JSONConfig.setFastStrings(boolean)

isEncodeNumericStringsAsNumbers

public boolean isEncodeNumericStringsAsNumbers()
Get the default encode numeric strings as numbers policy.

Accessible via MBean server.

Specified by:
isEncodeNumericStringsAsNumbers in interface JSONConfigDefaultsMBean
Returns:
The default encode numeric strings as numbers policy.
See Also:
JSONConfig.isEncodeNumericStringsAsNumbers()

setEncodeNumericStringsAsNumbers

public void setEncodeNumericStringsAsNumbers(boolean dflt)
Set the default flag for encoding of numeric strings as numbers.

Accessible via MBean server.

Specified by:
setEncodeNumericStringsAsNumbers in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then strings that look like valid JSON numbers will be encoded as numbers.
See Also:
JSONConfig.setEncodeNumericStringsAsNumbers(boolean)

isEscapeNonAscii

public boolean isEscapeNonAscii()
Get the default escape non-ASCII policy.

Accessible via MBean server.

Specified by:
isEscapeNonAscii in interface JSONConfigDefaultsMBean
Returns:
The default quote non-ASCII policy.
See Also:
JSONConfig.isEscapeNonAscii()

setEscapeNonAscii

public void setEscapeNonAscii(boolean dflt)
Set the default flag for forcing escaping of non-ASCII characters in strings and identifiers. If true, then escapeSurrogates will be forced to false. This will affect all new JSONConfig objects created after this call within the same class loader.

Accessible via MBean server.

Specified by:
setEscapeNonAscii in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then all non-ASCII will be Unicode escaped.
See Also:
JSONConfig.setEscapeNonAscii(boolean)

isEscapeSurrogates

public boolean isEscapeSurrogates()
Get the default escape surrogates policy.

Accessible via MBean server.

Specified by:
isEscapeSurrogates in interface JSONConfigDefaultsMBean
Returns:
the escape surrogates policy.
See Also:
JSONConfig.isEscapeSurrogates()

setEscapeSurrogates

public void setEscapeSurrogates(boolean dflt)
Set the default escapeSurrogates policy.

Accessible via MBean server.

Specified by:
setEscapeSurrogates in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then surrogates will be escaped in strings and identifiers and escapeNonAscii will be forced to false.
See Also:
JSONConfig.setEscapeSurrogates(boolean)

isUnEscapeWherePossible

public boolean isUnEscapeWherePossible()
Get the default unEscape policy.

Accessible via MBean server.

Specified by:
isUnEscapeWherePossible in interface JSONConfigDefaultsMBean
Returns:
the unEscape policy.
See Also:
JSONConfig.isUnEscapeWherePossible()

setUnEscapeWherePossible

public void setUnEscapeWherePossible(boolean dflt)
Set default flag for undoing inline escapes in strings.

Accessible via MBean server.

Specified by:
setUnEscapeWherePossible in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true then where possible, undo inline escapes in strings.
See Also:
JSONConfig.setUnEscapeWherePossible(boolean)

isPassThroughEscapes

public boolean isPassThroughEscapes()
Get the pass through escapes policy.

Accessible via MBean server.

Specified by:
isPassThroughEscapes in interface JSONConfigDefaultsMBean
Returns:
The pass through escapes policy.
See Also:
JSONConfig.isPassThroughEscapes()

setPassThroughEscapes

public void setPassThroughEscapes(boolean dflt)
If true, then escapes in strings will be passed through unchanged. If false, then the backslash that starts the escape will be escaped.

Accessible via MBean server.

Specified by:
setPassThroughEscapes in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then pass escapes through.
See Also:
JSONConfig.setPassThroughEscapes(boolean)

isEncodeDatesAsStrings

public boolean isEncodeDatesAsStrings()
Get the encode dates as strings policy.

Accessible via MBean server.

Specified by:
isEncodeDatesAsStrings in interface JSONConfigDefaultsMBean
Returns:
the encodeDatesAsStrings policy.
See Also:
JSONConfig.isEncodeDatesAsStrings()

setEncodeDatesAsStrings

public void setEncodeDatesAsStrings(boolean dflt)
Set the encodeDatesAsStrings policy. If you set this to true, then encodeDatesAsObjects will be set to false.

Accessible via MBean server.

Specified by:
setEncodeDatesAsStrings in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then Date objects will be encoded as ISO 8601 date strings or a custom date format if you have called setDateGenFormat(DateFormat).
See Also:
JSONConfig.setEncodeDatesAsStrings(boolean)

isReflectUnknownObjects

public boolean isReflectUnknownObjects()
Get the default reflection of unknown objects policy.

Accessible via MBean server.

Specified by:
isReflectUnknownObjects in interface JSONConfigDefaultsMBean
Returns:
the reflectUnknownObjects policy.
Since:
1.9
See Also:
JSONConfig.isReflectUnknownObjects()

setReflectUnknownObjects

public void setReflectUnknownObjects(boolean dflt)
Set the default unknown object reflection encoding policy. If true, then any time that an unknown object is encountered, this package will attempt to use reflection to encode it. Default is false. When false, then unknown objects will have their toString() method called.

Accessible via MBean server.

Specified by:
setReflectUnknownObjects in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then attempt to use reflection to encode objects which are otherwise unknown.
Since:
1.9
See Also:
addReflectClass(Object), addReflectClasses(Collection), addReflectClassByName(String), JSONConfig.setReflectUnknownObjects(boolean)

isPreciseNumbers

public boolean isPreciseNumbers()
Get the default preciseIntegers policy.

Specified by:
isPreciseNumbers in interface JSONConfigDefaultsMBean
Returns:
The preciseIntegers policy.
Since:
1.9
See Also:
JSONConfig.isPreciseNumbers()

setPreciseNumbers

public void setPreciseNumbers(boolean dflt)
If true then numbers which are not exactly representable by a 64 bit double precision floating point number will be quoted in the output. If false, then they will be unquoted, and precision in such will likely be lost in the interpreter.

Specified by:
setPreciseNumbers in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true then quote numbers that lose precision in 64-bit floating point.
Since:
1.9
See Also:
JSONConfig.setPreciseNumbers(boolean)

isSmallNumbers

public boolean isSmallNumbers()
Get the default smallNumbers policy.

Specified by:
isSmallNumbers in interface JSONConfigDefaultsMBean
Returns:
The smallNumbers policy.
Since:
1.9
See Also:
JSONConfig.isSmallNumbers()

setSmallNumbers

public void setSmallNumbers(boolean dflt)
If true then JSONParser will attempt to minimize the storage used for all numbers. Decimal numbers will be reduced to floats instead of doubles if it can done without losing precision. Integer numbers will be reduced from long to int or short or byte if they fit.

Specified by:
setSmallNumbers in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true then numbers will be made to use as little memory as possible.
Since:
1.9
See Also:
JSONConfig.setSmallNumbers(boolean)

isUsePrimitiveArrays

public boolean isUsePrimitiveArrays()
The default primitive arrays policy.

Specified by:
isUsePrimitiveArrays in interface JSONConfigDefaultsMBean
Returns:
the usePrimitiveArrays policy.
Since:
1.9
See Also:
JSONConfig.isUsePrimitiveArrays()

setUsePrimitiveArrays

public void setUsePrimitiveArrays(boolean dflt)
If true, then when JSONParser encounters a JSON array of non-null wrappers of primitives and those primitives are all compatible with each other, then instead of an ArrayList of wrappers for those primitives it will create an array of those primitives in order to save memory.

This works for booleans and numbers. It will also convert an array of single character strings into an array of chars. Arrays of numbers will attempt to use the least complex type that does not lose information. You could easily end up with an array of bytes if all of your numbers are integers in the range -128 to 127. This option is meant to save as much memory as possible.

Specified by:
setUsePrimitiveArrays in interface JSONConfigDefaultsMBean
Parameters:
dflt - if true, then the parser will create arrays of primitives as applicable.
Since:
1.9
See Also:
JSONConfig.setUsePrimitiveArrays(boolean)

isCacheReflectionData

public boolean isCacheReflectionData()
Get the default cacheReflectionData policy.

Specified by:
isCacheReflectionData in interface JSONConfigDefaultsMBean
Returns:
the cacheReflectionData policy.
Since:
1.9
See Also:
JSONConfig.isCacheReflectionData()

setCacheReflectionData

public void setCacheReflectionData(boolean dflt)
If true, then when an object is reflected its reflection data will be cached to improve performance on subsequent reflections of objects of its class.

Specified by:
setCacheReflectionData in interface JSONConfigDefaultsMBean
Parameters:
dflt - if true, then cache reflection data.
Since:
1.9
See Also:
JSONConfig.setCacheReflectionData(boolean)

isQuoteIdentifier

public boolean isQuoteIdentifier()
Get the default quote identifier policy.

Accessible via MBean server.

Specified by:
isQuoteIdentifier in interface JSONConfigDefaultsMBean
Returns:
The default quote identifier policy.
See Also:
JSONConfig.isQuoteIdentifier()

setQuoteIdentifier

public void setQuoteIdentifier(boolean dflt)
Set the default flag for forcing quotes on identifiers. This will affect all new JSONConfig objects created after this call within the same class loader.

Accessible via MBean server.

Specified by:
setQuoteIdentifier in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then all identifiers will be quoted.
See Also:
JSONConfig.setQuoteIdentifier(boolean)

isUseECMA6

public boolean isUseECMA6()
Get the default escape ECMAScript 6 code points policy.

Accessible via MBean server.

Specified by:
isUseECMA6 in interface JSONConfigDefaultsMBean
Returns:
The default escape ECMAScript 6 code points policy.
See Also:
JSONConfig.isUseECMA6()

setUseECMA6

public void setUseECMA6(boolean dflt)
If you set this to true, then by default when JSONUtil generates Unicode escapes, it will use ECMAScript 6 code point escapes if they are shorter than code unit escapes. This is not standard JSON and not yet widely supported by Javascript interpreters. It also allows identifiers to have letter numbers in addition to other letters. Default is false.

Accessible via MBean server.

Specified by:
setUseECMA6 in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, use EMCAScript 6 code point escapes and allow ECMAScript 6 identifier character set.
See Also:
JSONConfig.setUseECMA6(boolean)

isAllowReservedWordsInIdentifiers

public boolean isAllowReservedWordsInIdentifiers()
Get the default for allowing reserved words in identifiers.

Accessible via MBean server.

Specified by:
isAllowReservedWordsInIdentifiers in interface JSONConfigDefaultsMBean
Returns:
the reserverd words in identifiers policy.
See Also:
JSONConfig.isAllowReservedWordsInIdentifiers()

setAllowReservedWordsInIdentifiers

public void setAllowReservedWordsInIdentifiers(boolean dflt)
Set default flag for allowing reserved words in identifiers.

Accessible via MBean server.

Specified by:
setAllowReservedWordsInIdentifiers in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then reserved words will be allowed in identifiers.
See Also:
JSONConfig.setAllowReservedWordsInIdentifiers(boolean)

isEncodeDatesAsObjects

public boolean isEncodeDatesAsObjects()
Get the encode dates as objects policy.

Accessible via MBean server.

Specified by:
isEncodeDatesAsObjects in interface JSONConfigDefaultsMBean
Returns:
the encodeDatesAsObjects policy.
See Also:
JSONConfig.isEncodeDatesAsObjects()

setEncodeDatesAsObjects

public void setEncodeDatesAsObjects(boolean dflt)
If true, then Date objects will be encoded as Javascript dates, using new Date(dateString). If you set this to true, then encodeDatesAsStrings will be set to false.

Accessible via MBean server.

Specified by:
setEncodeDatesAsObjects in interface JSONConfigDefaultsMBean
Parameters:
dflt - If true, then Date objects will be encoded as Javascript dates.
See Also:
JSONConfig.setEncodeDatesAsObjects(boolean)


Copyright © 2016. All rights reserved.