Package net.sf.json.xml
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 class
private class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
the name for an JSONArray Elementprivate String
the name for an JSONArray's element Elementprivate static final String[]
private String[]
list of properties to be expanded from child to parentprivate boolean
private boolean
flag for if characters lower than ' ' should be escaped in texts.private boolean
flag for if text with CDATA should keep the information in the value or notprivate boolean
flag for performing auto-expansion of arrays ifprivate static final String
private boolean
flag for if array name should be kept in JSON dataprivate static final org.apache.commons.logging.Log
private boolean
flag to be tolerant for incomplete namespace prefixesprivate Map
Map of namespaces per elementprivate String
the name for an JSONObject Elementprivate boolean
flag for trimming namespace prefix from element nameprivate String
the name for the root Elementprivate Map
Map of namespaces for root elementprivate boolean
flag for skipping namespaces while readingprivate boolean
flag for skipping whitespace elements while readingprivate boolean
flag for sorting object properties by nameprivate boolean
flag for trimming spaces from string valuesprivate boolean
flag for type hints naming compatibilityprivate boolean
flag 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 String
addJsonPrefix
(String str) void
addNamespace
(String prefix, String uri) Adds a namespace declaration to the root element.void
addNamespace
(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 void
addNameSpaceToElement
(nu.xom.Element element) private boolean
canAutoExpand
(JSONArray array) Only perform auto expansion if all children are objects.private boolean
checkChildElements
(nu.xom.Element element, boolean isTopLevel) void
Removes all namespaces declarations (from root an elements).void
clearNamespaces
(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 String
getClass
(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 String
getType
(nu.xom.Element element) private String
private boolean
hasNamespaces
(nu.xom.Element element) private boolean
isArray
(nu.xom.Element element, boolean isTopLevel) private boolean
isCData
(nu.xom.Element element) boolean
private boolean
isFunction
(nu.xom.Element element) boolean
Returns whether this serializer is tolerant to namespaces without URIs or not.private boolean
isNullObject
(nu.xom.Element element) private boolean
isObject
(nu.xom.Element element, boolean isTopLevel) boolean
Returns whether this serializer will remove namespace prefix from elements or not.boolean
Returns whether this serializer will skip adding namespace declarations to elements or not.boolean
Returns whether this serializer will skip whitespace or not.boolean
Returns whether this serializer will sort object properties by name or not.boolean
Returns whether this serializer will trim leading and trealing whitespace from values or not.boolean
Returns true if types hints will have a 'json_' prefix or not.boolean
Returns true if JSON types will be included as attributes.private nu.xom.Element
newElement
(String name) private JSON
processArrayElement
(nu.xom.Element element, String defaultType) private Object
processElement
(nu.xom.Element element, String type) private nu.xom.Element
processJSONArray
(JSONArray array, nu.xom.Element root, String[] expandableProperties) private nu.xom.Element
processJSONObject
(JSONObject jsonObject, nu.xom.Element root, String[] expandableProperties, boolean isRoot) private nu.xom.Element
processJSONValue
(Object value, nu.xom.Element root, nu.xom.Element target, String[] expandableProperties) private JSON
processObjectElement
(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.void
removeNamespace
(String prefix) Removes a namespace from the root element.void
removeNamespace
(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 String
removeNamespacePrefix
(String name) void
setArrayName
(String arrayName) Sets the name used for JSONArray.
Default is 'a'.void
setElementName
(String elementName) Sets the name used for JSONArray elements.
Default is 'e'.void
setEscapeLowerChars
(boolean escape) Sets whether this serializer should escape characters lower than ' ' in texts.void
setExpandableProperties
(String[] expandableProperties) Sets the list of properties to be expanded from child to parent.void
setForceTopLevelObject
(boolean forceTopLevelObject) void
setKeepArrayName
(boolean keepName) Sets whether this serializer should keep the XML element being an array.void
setKeepCData
(boolean keepCData) Sets whether this serializer should keep the CDATA information in the value or not.void
setNamespace
(String prefix, String uri) Sets the namespace declaration to the root element.
Any previous values are discarded.void
setNamespace
(String prefix, String uri, String elementName) Adds a namespace declaration to an element.
Any previous values are discarded.void
setNamespaceLenient
(boolean namespaceLenient) Sets whether this serializer is tolerant to namespaces without URIs or not.void
setObjectName
(String objectName) Sets the name used for JSONObject.
Default is 'o'.private void
setOrAccumulate
(JSONObject jsonObject, String key, Object value) void
setPerformAutoExpansion
(boolean autoExpansion) Sets whether this serializer should perform automatic expansion of array elements or not.void
setRemoveNamespacePrefixFromElements
(boolean removeNamespacePrefixFromElements) Sets if this serializer will remove namespace prefix from elements when reading.void
setRootName
(String rootName) Sets the name used for the root element.void
setSkipNamespaces
(boolean skipNamespaces) Sets if this serializer will skip adding namespace declarations to elements when reading.void
setSkipWhitespace
(boolean skipWhitespace) Sets if this serializer will skip whitespace when reading.void
setSortPropertyNames
(boolean sortPropertyNames) Returns whether this serializer will sort object properties by name or not.void
setTrimSpaces
(boolean trimSpaces) Sets if this serializer will trim leading and trealing whitespace from values when reading.void
setTypeHintsCompatibility
(boolean typeHintsCompatibility) Sets whether types hints will have a 'json_' prefix or not.void
setTypeHintsEnabled
(boolean typeHintsEnabled) Sets whether JSON types will be included as attributes.private void
private void
setValue
(JSONObject jsonObject, nu.xom.Element element, String defaultType) private Object
simplifyValue
(JSONObject parent, Object json) private String
trimSpaceFromValue
(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 String
writeDocument
(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
Adds a namespace declaration to the root element.- Parameters:
prefix
- namespace prefixuri
- namespace uri
-
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
Removes a namespace from the root element.
If the elementName is null or blank, the namespace will be removed from the root element.- Parameters:
prefix
- namespace prefixelementName
- name of target element
-
setNamespace
Sets the namespace declaration to the root element.
Any previous values are discarded.- Parameters:
prefix
- namespace prefixuri
- namespace uri
-
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
-