Class XMLSerializer
java.lang.Object
net.sf.json.xml.XMLSerializer
Utility class for transforming JSON to XML an back.
When transforming JSONObject and JSONArray instances to XML, this class will add hints for converting back to JSON.
Examples:
When transforming JSONObject and JSONArray instances to XML, this class will add hints for converting back to JSON.
Examples:
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
String xml = new XMLSerializer().write( json );
json
true
1
JSONArray json = JSONArray.fromObject("[1,2,3]");
String xml = new XMLSerializer().write( json );
1
2
3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringthe name for an JSONArray Elementprivate Stringthe name for an JSONArray's element Elementprivate static final String[]private String[]list of properties to be expanded from child to parentprivate booleanprivate booleanflag for if characters lower than ' ' should be escaped in texts.private booleanflag for if text with CDATA should keep the information in the value or notprivate booleanflag for performing auto-expansion of arrays ifprivate static final Stringprivate booleanflag for if array name should be kept in JSON dataprivate static final org.apache.commons.logging.Logprivate booleanflag to be tolerant for incomplete namespace prefixesprivate MapMap of namespaces per elementprivate Stringthe name for an JSONObject Elementprivate booleanflag for trimming namespace prefix from element nameprivate Stringthe name for the root Elementprivate MapMap of namespaces for root elementprivate booleanflag for skipping namespaces while readingprivate booleanflag for skipping whitespace elements while readingprivate booleanflag for sorting object properties by nameprivate booleanflag for trimming spaces from string valuesprivate booleanflag for type hints naming compatibilityprivate booleanflag for adding JSON types hints as attributes -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new XMLSerializer with default options.
objectName: 'o'arrayName: 'a'elementName: 'e'typeHinstEnabled: truetypeHinstCompatibility: truenamespaceLenient: falseexpandableProperties: []skipNamespaces: falseremoveNameSpacePrefixFromElement: falsetrimSpaces: falseexpandableProperties: []skipWhitespace: falseperformAutoExpansion: falsekeepCData: falseescapeLowerChars: falsekeepArrayName: false -
Method Summary
Modifier and TypeMethodDescriptionprivate StringaddJsonPrefix(String str) voidaddNamespace(String prefix, String uri) Adds a namespace declaration to the root element.voidaddNamespace(String prefix, String uri, String elementName) Adds a namespace declaration to an element.
If the elementName param is null or blank, the namespace declaration will be added to the root element.private voidaddNameSpaceToElement(nu.xom.Element element) private booleancanAutoExpand(JSONArray array) Only perform auto expansion if all children are objects.private booleancheckChildElements(nu.xom.Element element, boolean isTopLevel) voidRemoves all namespaces declarations (from root an elements).voidclearNamespaces(String elementName) Removes all namespace declarations from an element.
If the elementName param is null or blank, the declarations will be removed from the root element.Returns the name used for JSONArray.private StringgetClass(nu.xom.Element element) Returns the name used for JSONArray elements.String[]Returns a list of properties to be expanded from child to parent.Returns the name used for JSONArray.Returns the name used for the root element.private StringgetType(nu.xom.Element element) private Stringprivate booleanhasNamespaces(nu.xom.Element element) private booleanisArray(nu.xom.Element element, boolean isTopLevel) private booleanisCData(nu.xom.Element element) booleanprivate booleanisFunction(nu.xom.Element element) booleanReturns whether this serializer is tolerant to namespaces without URIs or not.private booleanisNullObject(nu.xom.Element element) private booleanisObject(nu.xom.Element element, boolean isTopLevel) booleanReturns whether this serializer will remove namespace prefix from elements or not.booleanReturns whether this serializer will skip adding namespace declarations to elements or not.booleanReturns whether this serializer will skip whitespace or not.booleanReturns whether this serializer will sort object properties by name or not.booleanReturns whether this serializer will trim leading and trealing whitespace from values or not.booleanReturns true if types hints will have a 'json_' prefix or not.booleanReturns true if JSON types will be included as attributes.private nu.xom.ElementnewElement(String name) private JSONprocessArrayElement(nu.xom.Element element, String defaultType) private ObjectprocessElement(nu.xom.Element element, String type) private nu.xom.ElementprocessJSONArray(JSONArray array, nu.xom.Element root, String[] expandableProperties) private nu.xom.ElementprocessJSONObject(JSONObject jsonObject, nu.xom.Element root, String[] expandableProperties, boolean isRoot) private nu.xom.ElementprocessJSONValue(Object value, nu.xom.Element root, nu.xom.Element target, String[] expandableProperties) private JSONprocessObjectElement(nu.xom.Element element, String defaultType) Creates a JSON value from a XML string.readFromFile(File file) Creates a JSON value from a File.readFromFile(String path) Creates a JSON value from a File.readFromStream(InputStream stream) Creates a JSON value from an input stream.voidremoveNamespace(String prefix) Removes a namespace from the root element.voidremoveNamespace(String prefix, String elementName) Removes a namespace from the root element.
If the elementName is null or blank, the namespace will be removed from the root element.private StringremoveNamespacePrefix(String name) voidsetArrayName(String arrayName) Sets the name used for JSONArray.
Default is 'a'.voidsetElementName(String elementName) Sets the name used for JSONArray elements.
Default is 'e'.voidsetEscapeLowerChars(boolean escape) Sets whether this serializer should escape characters lower than ' ' in texts.voidsetExpandableProperties(String[] expandableProperties) Sets the list of properties to be expanded from child to parent.voidsetForceTopLevelObject(boolean forceTopLevelObject) voidsetKeepArrayName(boolean keepName) Sets whether this serializer should keep the XML element being an array.voidsetKeepCData(boolean keepCData) Sets whether this serializer should keep the CDATA information in the value or not.voidsetNamespace(String prefix, String uri) Sets the namespace declaration to the root element.
Any previous values are discarded.voidsetNamespace(String prefix, String uri, String elementName) Adds a namespace declaration to an element.
Any previous values are discarded.voidsetNamespaceLenient(boolean namespaceLenient) Sets whether this serializer is tolerant to namespaces without URIs or not.voidsetObjectName(String objectName) Sets the name used for JSONObject.
Default is 'o'.private voidsetOrAccumulate(JSONObject jsonObject, String key, Object value) voidsetPerformAutoExpansion(boolean autoExpansion) Sets whether this serializer should perform automatic expansion of array elements or not.voidsetRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements) Sets if this serializer will remove namespace prefix from elements when reading.voidsetRootName(String rootName) Sets the name used for the root element.voidsetSkipNamespaces(boolean skipNamespaces) Sets if this serializer will skip adding namespace declarations to elements when reading.voidsetSkipWhitespace(boolean skipWhitespace) Sets if this serializer will skip whitespace when reading.voidsetSortPropertyNames(boolean sortPropertyNames) Returns whether this serializer will sort object properties by name or not.voidsetTrimSpaces(boolean trimSpaces) Sets if this serializer will trim leading and trealing whitespace from values when reading.voidsetTypeHintsCompatibility(boolean typeHintsCompatibility) Sets whether types hints will have a 'json_' prefix or not.voidsetTypeHintsEnabled(boolean typeHintsEnabled) Sets whether JSON types will be included as attributes.private voidprivate voidsetValue(JSONObject jsonObject, nu.xom.Element element, String defaultType) private ObjectsimplifyValue(JSONObject parent, Object json) private StringtrimSpaceFromValue(String value) Writes a JSON value into a XML string with UTF-8 encoding.Writes a JSON value into a XML string with an specific encoding.
If the encoding string is null it will use UTF-8.private StringwriteDocument(nu.xom.Document doc, String encoding)
-
Field Details
-
EMPTY_ARRAY
-
JSON_PREFIX
- See Also:
-
log
private static final org.apache.commons.logging.Log log -
arrayName
the name for an JSONArray Element -
elementName
the name for an JSONArray's element Element -
expandableProperties
list of properties to be expanded from child to parent -
forceTopLevelObject
private boolean forceTopLevelObject -
namespaceLenient
private boolean namespaceLenientflag to be tolerant for incomplete namespace prefixes -
namespacesPerElement
Map of namespaces per element -
objectName
the name for an JSONObject Element -
removeNamespacePrefixFromElements
private boolean removeNamespacePrefixFromElementsflag for trimming namespace prefix from element name -
rootName
the name for the root Element -
rootNamespace
Map of namespaces for root element -
skipNamespaces
private boolean skipNamespacesflag for skipping namespaces while reading -
skipWhitespace
private boolean skipWhitespaceflag for skipping whitespace elements while reading -
trimSpaces
private boolean trimSpacesflag for trimming spaces from string values -
typeHintsCompatibility
private boolean typeHintsCompatibilityflag for type hints naming compatibility -
typeHintsEnabled
private boolean typeHintsEnabledflag for adding JSON types hints as attributes -
isPerformAutoExpansion
private boolean isPerformAutoExpansionflag for performing auto-expansion of arrays if -
isKeepCData
private boolean isKeepCDataflag for if text with CDATA should keep the information in the value or not -
isEscapeLowerChars
private boolean isEscapeLowerCharsflag for if characters lower than ' ' should be escaped in texts. -
keepArrayName
private boolean keepArrayNameflag for if array name should be kept in JSON data -
sortPropertyNames
private boolean sortPropertyNamesflag for sorting object properties by name
-
-
Constructor Details
-
XMLSerializer
public XMLSerializer()Creates a new XMLSerializer with default options.
objectName: 'o'arrayName: 'a'elementName: 'e'typeHinstEnabled: truetypeHinstCompatibility: truenamespaceLenient: falseexpandableProperties: []skipNamespaces: falseremoveNameSpacePrefixFromElement: falsetrimSpaces: falseexpandableProperties: []skipWhitespace: falseperformAutoExpansion: falsekeepCData: falseescapeLowerChars: falsekeepArrayName: false
-
-
Method Details
-
isSortPropertyNames
public boolean isSortPropertyNames()Returns whether this serializer will sort object properties by name or not. -
setSortPropertyNames
public void setSortPropertyNames(boolean sortPropertyNames) Returns whether this serializer will sort object properties by name or not. -
addNamespace
-
addNamespace
Adds a namespace declaration to an element.
If the elementName param is null or blank, the namespace declaration will be added to the root element.- Parameters:
prefix- namespace prefixuri- namespace urielementName- name of target element
-
clearNamespaces
public void clearNamespaces()Removes all namespaces declarations (from root an elements). -
clearNamespaces
Removes all namespace declarations from an element.
If the elementName param is null or blank, the declarations will be removed from the root element.- Parameters:
elementName- name of target element
-
getArrayName
Returns the name used for JSONArray. -
setArrayName
Sets the name used for JSONArray.
Default is 'a'. -
getElementName
Returns the name used for JSONArray elements. -
setElementName
Sets the name used for JSONArray elements.
Default is 'e'. -
getExpandableProperties
Returns a list of properties to be expanded from child to parent. -
setExpandableProperties
Sets the list of properties to be expanded from child to parent. -
getObjectName
Returns the name used for JSONArray. -
setObjectName
Sets the name used for JSONObject.
Default is 'o'. -
getRootName
Returns the name used for the root element. -
setRootName
Sets the name used for the root element. -
isForceTopLevelObject
public boolean isForceTopLevelObject() -
setForceTopLevelObject
public void setForceTopLevelObject(boolean forceTopLevelObject) -
isNamespaceLenient
public boolean isNamespaceLenient()Returns whether this serializer is tolerant to namespaces without URIs or not. -
setNamespaceLenient
public void setNamespaceLenient(boolean namespaceLenient) Sets whether this serializer is tolerant to namespaces without URIs or not. -
isRemoveNamespacePrefixFromElements
public boolean isRemoveNamespacePrefixFromElements()Returns whether this serializer will remove namespace prefix from elements or not. -
setRemoveNamespacePrefixFromElements
public void setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements) Sets if this serializer will remove namespace prefix from elements when reading. -
isSkipNamespaces
public boolean isSkipNamespaces()Returns whether this serializer will skip adding namespace declarations to elements or not. -
setSkipNamespaces
public void setSkipNamespaces(boolean skipNamespaces) Sets if this serializer will skip adding namespace declarations to elements when reading. -
isSkipWhitespace
public boolean isSkipWhitespace()Returns whether this serializer will skip whitespace or not. -
setSkipWhitespace
public void setSkipWhitespace(boolean skipWhitespace) Sets if this serializer will skip whitespace when reading. -
isTrimSpaces
public boolean isTrimSpaces()Returns whether this serializer will trim leading and trealing whitespace from values or not. -
setTrimSpaces
public void setTrimSpaces(boolean trimSpaces) Sets if this serializer will trim leading and trealing whitespace from values when reading. -
isTypeHintsCompatibility
public boolean isTypeHintsCompatibility()Returns true if types hints will have a 'json_' prefix or not. -
setTypeHintsCompatibility
public void setTypeHintsCompatibility(boolean typeHintsCompatibility) Sets whether types hints will have a 'json_' prefix or not. -
isTypeHintsEnabled
public boolean isTypeHintsEnabled()Returns true if JSON types will be included as attributes. -
setTypeHintsEnabled
public void setTypeHintsEnabled(boolean typeHintsEnabled) Sets whether JSON types will be included as attributes. -
read
Creates a JSON value from a XML string.- Parameters:
xml- A well-formed xml document in a String- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
readFromFile
Creates a JSON value from a File.- Parameters:
file-- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
readFromFile
Creates a JSON value from a File.- Parameters:
path-- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
readFromStream
Creates a JSON value from an input stream.- Parameters:
stream-- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
removeNamespace
Removes a namespace from the root element.- Parameters:
prefix- namespace prefix
-
removeNamespace
-
setNamespace
-
setNamespace
Adds a namespace declaration to an element.
Any previous values are discarded. If the elementName param is null or blank, the namespace declaration will be added to the root element.- Parameters:
prefix- namespace prefixuri- namespace urielementName- name of target element
-
setPerformAutoExpansion
public void setPerformAutoExpansion(boolean autoExpansion) Sets whether this serializer should perform automatic expansion of array elements or not. -
setKeepCData
public void setKeepCData(boolean keepCData) Sets whether this serializer should keep the CDATA information in the value or not.- Parameters:
keepCData- True to keep CDATA, false to only use the text value.
-
setEscapeLowerChars
public void setEscapeLowerChars(boolean escape) Sets whether this serializer should escape characters lower than ' ' in texts.- Parameters:
escape- True to escape, false otherwise.
-
setKeepArrayName
public void setKeepArrayName(boolean keepName) Sets whether this serializer should keep the XML element being an array.- Parameters:
keepName- True to include the element name in the JSON object, false otherwise.
-
write
Writes a JSON value into a XML string with UTF-8 encoding.- Parameters:
json- The JSON value to transform- Returns:
- a String representation of a well-formed xml document.
- Throws:
JSONException- if the conversion from JSON to XML can't be made for I/O reasons.
-
write
Writes a JSON value into a XML string with an specific encoding.
If the encoding string is null it will use UTF-8.- Parameters:
json- The JSON value to transformencoding- The xml encoding to use- Returns:
- a String representation of a well-formed xml document.
- Throws:
JSONException- if the conversion from JSON to XML can't be made for I/O reasons or the encoding is not supported.
-
addJsonPrefix
-
addNameSpaceToElement
private void addNameSpaceToElement(nu.xom.Element element) -
checkChildElements
private boolean checkChildElements(nu.xom.Element element, boolean isTopLevel) -
getClass
-
getType
-
getType
-
hasNamespaces
private boolean hasNamespaces(nu.xom.Element element) -
isArray
private boolean isArray(nu.xom.Element element, boolean isTopLevel) -
isFunction
private boolean isFunction(nu.xom.Element element) -
isNullObject
private boolean isNullObject(nu.xom.Element element) -
isObject
private boolean isObject(nu.xom.Element element, boolean isTopLevel) -
newElement
-
processArrayElement
-
processElement
-
processJSONArray
-
processJSONObject
private nu.xom.Element processJSONObject(JSONObject jsonObject, nu.xom.Element root, String[] expandableProperties, boolean isRoot) -
canAutoExpand
Only perform auto expansion if all children are objects.- Parameters:
array- The array to check- Returns:
- True if all children are objects, false otherwise.
-
processJSONValue
-
processObjectElement
-
removeNamespacePrefix
-
setOrAccumulate
-
setValue
-
setValue
-
isCData
private boolean isCData(nu.xom.Element element) -
simplifyValue
-
trimSpaceFromValue
-
writeDocument
-