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 affects 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 the default values of these flags 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", in
$CATALINA_BASE/conf/Catalina/host/MyApp.xml in order to
disable property name validation:
<Context path="/MyApp">
<Environment name="org/kopitubruk/util/json/validatePropertyNames" type="java.lang.Boolean" value="false" override="false" />
</Context>
These are the names and their normal defaults if you don't change them. See the setters for these for descriptions of what they do.
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 get passed to
Locale.forLanguageTag(String).
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.JSONConfig. You can disable MBean registration by setting a boolean variable named registerMBean to false in the environment as shown above for the flags.
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.
JSONConfig,
Serialized Form| Modifier and Type | Method and Description |
|---|---|
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 |
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.
|
static JSONConfigDefaults |
getInstance()
Return the JSONConfigDefaults singleton instance.
|
static Locale |
getLocale()
Set a default locale for new JSONConfig objects to use.
|
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.
|
boolean |
isAllowReservedWordsInIdentifiers()
Get the default for allowing reserved words in identifiers.
|
boolean |
isDetectDataStructureLoops()
Get the default detect data structure loops 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 quote non-ASCII policy.
|
boolean |
isEscapeSurrogates()
The default escape surrogates policy.
|
boolean |
isQuoteIdentifier()
Get the default quote identifier policy.
|
boolean |
isUnEscapeWherePossible()
The default unEscape policy.
|
boolean |
isUseECMA6()
Get the default escape ECMAScript 6 code points policy.
|
boolean |
isValidatePropertyNames()
Get the default validate property names policy.
|
static void |
removeNumberFormat(Class<? extends Number> numericClass)
Remove the requested class from the default number formats.
|
void |
setAllowReservedWordsInIdentifiers(boolean dflt)
Set default flag for allowing reserved words in identifiers.
|
void |
setCodeDefaults()
Reset all defaults to their original unmodified values.
|
void |
setDetectDataStructureLoops(boolean dflt)
Set the default flag for forcing quotes on identifiers.
|
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.
|
void |
setEscapeSurrogates(boolean dflt)
If true, then surrogates will be escaped.
|
static void |
setLocale(Locale locale)
Set a default locale for new JSONConfig objects to use.
|
void |
setQuoteIdentifier(boolean dflt)
Set the default flag for forcing quotes on identifiers.
|
void |
setUnEscapeWherePossible(boolean dflt)
Set default flag for undoing inline escapes in strings.
|
void |
setUseECMA6(boolean dflt)
Set the default flag for using ECMAScript 6 code points to encode
Unicode escapes.
|
void |
setValidatePropertyNames(boolean dflt)
Set the default flag for validation of property names.
|
public static JSONConfigDefaults getInstance()
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 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.
public static Locale getLocale()
public static void setLocale(Locale locale)
locale - the default locale.public static void addNumberFormat(Class<? extends Number> numericClass, NumberFormat fmt)
numericClass - The class.fmt - The number format.public static void addNumberFormat(Number numericType, NumberFormat fmt)
numericType - The object.fmt - The number format.public static NumberFormat getNumberFormat(Class<? extends Number> numericClass)
numericClass - A class.public static NumberFormat getNumberFormat(Number num)
num - An object that implements Number.public static void removeNumberFormat(Class<? extends Number> numericClass)
numericClass - The class.public void setCodeDefaults()
setCodeDefaults in interface JSONConfigDefaultsMBeanpublic void clearNumberFormats()
clearNumberFormats in interface JSONConfigDefaultsMBeanpublic boolean isValidatePropertyNames()
isValidatePropertyNames in interface JSONConfigDefaultsMBeanpublic void setValidatePropertyNames(boolean dflt)
setValidatePropertyNames in interface JSONConfigDefaultsMBeandflt - If true, then property names will be validated by default.public boolean isDetectDataStructureLoops()
isDetectDataStructureLoops in interface JSONConfigDefaultsMBeanpublic void setDetectDataStructureLoops(boolean dflt)
setDetectDataStructureLoops in interface JSONConfigDefaultsMBeandflt - If true, then all identifiers will be quoted.public boolean isEscapeBadIdentifierCodePoints()
isEscapeBadIdentifierCodePoints in interface JSONConfigDefaultsMBeanpublic void setEscapeBadIdentifierCodePoints(boolean dflt)
setEscapeBadIdentifierCodePoints in interface JSONConfigDefaultsMBeandflt - the escapeBadIdentifierCodePoints to setpublic boolean isEncodeNumericStringsAsNumbers()
isEncodeNumericStringsAsNumbers in interface JSONConfigDefaultsMBeanpublic void setEncodeNumericStringsAsNumbers(boolean dflt)
setEncodeNumericStringsAsNumbers in interface JSONConfigDefaultsMBeandflt - If true, then strings that look like numbers will be encoded
as numbers.public boolean isEscapeNonAscii()
isEscapeNonAscii in interface JSONConfigDefaultsMBeanpublic void setEscapeNonAscii(boolean dflt)
setEscapeNonAscii in interface JSONConfigDefaultsMBeandflt - If true, then all non-ASCII will be Unicode escaped.public boolean isUnEscapeWherePossible()
isUnEscapeWherePossible in interface JSONConfigDefaultsMBeanpublic void setUnEscapeWherePossible(boolean dflt)
setUnEscapeWherePossible in interface JSONConfigDefaultsMBeandflt - If true then where possible, undo inline escapes in strings.public boolean isEscapeSurrogates()
isEscapeSurrogates in interface JSONConfigDefaultsMBeanpublic void setEscapeSurrogates(boolean dflt)
setEscapeSurrogates in interface JSONConfigDefaultsMBeandflt - the defaultEscapeSurrogates to setpublic boolean isQuoteIdentifier()
isQuoteIdentifier in interface JSONConfigDefaultsMBeanpublic void setQuoteIdentifier(boolean dflt)
setQuoteIdentifier in interface JSONConfigDefaultsMBeandflt - If true, then all identifiers will be quoted.public boolean isUseECMA6()
isUseECMA6 in interface JSONConfigDefaultsMBeanpublic void setUseECMA6(boolean dflt)
setUseECMA6 in interface JSONConfigDefaultsMBeandflt - if true then ECMAScript 6 code points
will be used to encode Unicode escapes as needed.public boolean isAllowReservedWordsInIdentifiers()
isAllowReservedWordsInIdentifiers in interface JSONConfigDefaultsMBeanpublic void setAllowReservedWordsInIdentifiers(boolean dflt)
setAllowReservedWordsInIdentifiers in interface JSONConfigDefaultsMBeandflt - the defaultAllowReservedWordsInIdentifiers to setCopyright © 2015. All rights reserved.