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:
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
Constructor and description |
---|
JsonXmlSerializer
() Creates a new XMLSerializer with default options. |
Type Params | Return Type | Name and description |
---|---|---|
|
void |
addNamespace(java.lang.String prefix, java.lang.String uri) Adds a namespace declaration to the root element. |
|
void |
addNamespace(java.lang.String prefix, java.lang.String uri, java.lang.String elementName) Adds a namespace declaration to an element. |
|
void |
clearNamespaces() Removes all namespaces declarations (from root an elements). |
|
void |
clearNamespaces(java.lang.String elementName) Removes all namespace declarations from an element. |
|
java.lang.String |
getArrayName() Returns the name used for JSONArray. |
|
java.lang.String |
getElementName() Returns the name used for JSONArray elements. |
|
java.lang.String[] |
getExpandableProperties() Returns a list of properties to be expanded from child to parent. |
|
java.lang.String |
getObjectName() Returns the name used for JSONArray. |
|
java.lang.String |
getRootName() Returns the name used for the root element. |
|
boolean |
isForceTopLevelObject() |
|
boolean |
isNamespaceLenient() Returns wether this serializer is tolerant to namespaces without URIs or not. |
|
boolean |
isRemoveNamespacePrefixFromElements() Returns wether this serializer will remove namespace prefix from elements or not. |
|
boolean |
isSkipNamespaces() Returns wether this serializer will skip adding namespace declarations to elements or not. |
|
boolean |
isSkipWhitespace() Returns wether this serializer will skip whitespace or not. |
|
boolean |
isTrimSpaces() Returns wether this serializer will trim leading and trealing whitespace from values or not. |
|
boolean |
isTypeHintsCompatibility() Returns true if types hints will have a 'json_' prefix or not. |
|
boolean |
isTypeHintsEnabled() Returns true if JSON types will be included as attributes. |
|
JSON |
read(java.lang.String xml) Creates a JSON value from a XML string. |
|
JSON |
readFromFile(java.io.File file) Creates a JSON value from a File. |
|
JSON |
readFromFile(java.lang.String path) Creates a JSON value from a File. |
|
JSON |
readFromStream(java.io.InputStream stream) Creates a JSON value from an input stream. |
|
void |
removeNamespace(java.lang.String prefix) Removes a namespace from the root element. |
|
void |
removeNamespace(java.lang.String prefix, java.lang.String elementName) Removes a namespace from the root element. |
|
void |
setArrayName(java.lang.String arrayName) Sets the name used for JSONArray. |
|
void |
setElementName(java.lang.String elementName) Sets the name used for JSONArray elements. |
|
void |
setExpandableProperties(java.lang.String[] expandableProperties) Sets the list of properties to be expanded from child to parent. |
|
void |
setForceTopLevelObject(boolean forceTopLevelObject) |
|
void |
setNamespace(java.lang.String prefix, java.lang.String uri) Sets the namespace declaration to the root element. |
|
void |
setNamespace(java.lang.String prefix, java.lang.String uri, java.lang.String elementName) Adds a namespace declaration to an element. |
|
void |
setNamespaceLenient(boolean namespaceLenient) Sets wether this serializer is tolerant to namespaces without URIs or not. |
|
void |
setObjectName(java.lang.String objectName) Sets the name used for JSONObject. |
|
void |
setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements) Sets if this serializer will remove namespace prefix from elements when reading. |
|
void |
setRootName(java.lang.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 |
setTrimSpaces(boolean trimSpaces) Sets if this serializer will trim leading and trealing whitespace from values when reading. |
|
void |
setTypeHintsCompatibility(boolean typeHintsCompatibility) Sets wether types hints will have a 'json_' prefix or not. |
|
void |
setTypeHintsEnabled(boolean typeHintsEnabled) Sets wether JSON types will be included as attributes. |
|
java.lang.String |
write(JSON json) Writes a JSON value into a XML string with UTF-8 encoding. |
|
java.lang.String |
write(JSON json, java.lang.String encoding) Writes a JSON value into a XML string with an specific encoding. |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Creates a new XMLSerializer with default options.
objectName
: 'o'arrayName
: 'a'elementName
: 'e'typeHinstEnabled
: truetypeHinstCompatibility
: truenamespaceLenient
: falseexpandableProperties
: []skipNamespaces
: falseremoveNameSpacePrefixFromElement
: falsetrimSpaces
: falseAdds a namespace declaration to the root element.
prefix
- namespace prefixuri
- namespace uri 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.
prefix
- namespace prefixuri
- namespace urielementName
- name of target elementRemoves all namespaces declarations (from root an elements).
Removes all namespace declarations from an element.
If the elementName param is null or blank, the declarations will be
removed from the root element.
elementName
- name of target elementReturns the name used for JSONArray.
Returns the name used for JSONArray elements.
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.
Returns wether this serializer is tolerant to namespaces without URIs or not.
Returns wether this serializer will remove namespace prefix from elements or not.
Returns wether this serializer will skip adding namespace declarations to elements or not.
Returns wether this serializer will skip whitespace or not.
Returns wether this serializer will trim leading and trealing whitespace from values or not.
Returns true if types hints will have a 'json_' prefix or not.
Returns true if JSON types will be included as attributes.
Creates a JSON value from a XML string.
xml
- A well-formed xml document in a StringCreates a JSON value from a File.
Creates a JSON value from a File.
Creates a JSON value from an input stream.
Removes a namespace from the root element.
prefix
- namespace prefix Removes a namespace from the root element.
If the elementName is null or blank, the namespace will be removed from
the root element.
prefix
- namespace prefixelementName
- name of target element Sets the name used for JSONArray.
Default is 'a'.
Sets the name used for JSONArray elements.
Default is 'e'.
Sets the list of properties to be expanded from child to parent.
Sets the namespace declaration to the root element.
Any previous values are discarded.
prefix
- namespace prefixuri
- namespace uri 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.
prefix
- namespace prefixuri
- namespace urielementName
- name of target elementSets wether this serializer is tolerant to namespaces without URIs or not.
Sets the name used for JSONObject.
Default is 'o'.
Sets if this serializer will remove namespace prefix from elements when reading.
Sets the name used for the root element.
Sets if this serializer will skip adding namespace declarations to elements when reading.
Sets if this serializer will skip whitespace when reading.
Sets if this serializer will trim leading and trealing whitespace from values when reading.
Sets wether types hints will have a 'json_' prefix or not.
Sets wether JSON types will be included as attributes.
Writes a JSON value into a XML string with UTF-8 encoding.
json
- The JSON value to transform Writes a JSON value into a XML string with an specific encoding.
If the encoding string is null it will use UTF-8.
json
- The JSON value to transformencoding
- The xml encoding to use