Class XMLSerializer


  • public class XMLSerializer
    extends java.lang.Object
    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 );
     <o class="object">
     <name type="string">json</name>
     <bool type="boolean">true</bool>
     <int type="number">1</int>
     </o>
     
     JSONArray json = JSONArray.fromObject("[1,2,3]");
     String xml = new XMLSerializer().write( json );
     <a class="array">
     <e type="number">1</e>
     <e type="number">2</e>
     <e type="number">3</e>
     </a>
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String arrayName
      the name for an JSONArray Element
      private java.lang.String elementName
      the name for an JSONArray's element Element
      private static java.lang.String[] EMPTY_ARRAY  
      private boolean escapeLowerChars
      flag for if characters lower than ' ' should be escaped in texts.
      private java.lang.String[] expandableProperties
      list of properties to be expanded from child to parent
      private java.util.Collection<java.lang.String> forcedArrayElements
      set of element names that forces its children elements to be in an Array
      private boolean forceTopLevelObject  
      private static java.lang.String JSON_PREFIX  
      private boolean keepArrayName
      flag for if array name should be kept in JSON data
      private boolean keepCData
      flag for if text with CDATA should keep the information in the value or not
      private static org.slf4j.Logger LOG  
      private java.util.Map<java.lang.String,​java.lang.String> mappedPropertyNames
      Map JSON property names to XML elements
      private boolean namespaceLenient
      flag to be tolerant for incomplete namespace prefixes
      private java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> namespacesPerElement
      Map of namespaces per element
      private java.lang.String objectName
      the name for an JSONObject Element
      private boolean parseJsonLiterals
      should JSON literals be parsed or not
      private boolean performAutoExpansion
      flag for performing auto-expansion of arrays if
      private boolean removeNamespacePrefixFromElements
      flag for trimming namespace prefix from element name
      private java.lang.String rootName
      the name for the root Element
      private java.util.Map<java.lang.String,​java.lang.String> rootNamespace
      Map of namespaces for root element
      private boolean skipNamespaces
      flag for skipping namespaces while reading
      private boolean skipWhitespace
      flag for skipping whitespace elements while reading
      private boolean sortPropertyNames
      flag for sorting object properties by name
      private boolean trimSpaces
      flag for trimming spaces from string values
      private boolean typeHintsCompatibility
      flag for type hints naming compatibility
      private boolean typeHintsEnabled
      flag for adding JSON types hints as attributes
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLSerializer()
      Creates a new XMLSerializer with default options.
      objectName: 'o' arrayName: 'a' elementName: 'e' typeHintsEnabled: true typeHintsCompatibility: true namespaceLenient: false expandableProperties: [] skipNamespaces: false removeNameSpacePrefixFromElement: false trimSpaces: false expandableProperties: [] skipWhitespace: false performAutoExpansion: false keepCData: false escapeLowerChars: false keepArrayName: false forcedArrayElements: [] parseJsonLiterals: true sortPropertyNames: false
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.String addJsonPrefix​(java.lang.String str)  
      void addMappedPropertyName​(java.lang.String json, java.lang.String xml)
      Add a mapped JSON property name to XML element.
      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.
      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 clearNamespaces()
      Removes all namespaces declarations (from root an elements).
      void clearNamespaces​(java.lang.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.
      java.lang.String getArrayName()
      Returns the name used for JSONArray.
      private java.lang.String getClass​(nu.xom.Element element)  
      java.lang.String getElementName()
      Returns the name used for JSONArray elements.
      java.util.Map<java.lang.String,​java.lang.String> getElementNamespace​(java.lang.String elementName)
      Returns a read-only view of the particular element name space if found.
      java.lang.String[] getExpandableProperties()
      Returns a list of properties to be expanded from child to parent.
      java.util.Collection<java.lang.String> getForcedArrayElements()
      Returns the set of XML elements that force their children to be treated as array elements.
      java.util.Map<java.lang.String,​java.lang.String> getMappedPropertyNames()
      Returns mappings between JSON properties to XML elements.
      java.lang.String getObjectName()
      Returns the name used for JSONArray.
      java.lang.String getRootName()
      Returns the name used for the root element.
      java.util.Map<java.lang.String,​java.lang.String> getRootNamespace()
      Returns a read-only view of the root name space.
      private java.lang.String getType​(nu.xom.Element element)  
      private java.lang.String getType​(nu.xom.Element element, java.lang.String defaultType)  
      private boolean hasNamespaces​(nu.xom.Element element)  
      private boolean isArray​(nu.xom.Element element, boolean isTopLevel)  
      private boolean isCData​(nu.xom.Element element)  
      boolean isForceTopLevelObject()  
      private boolean isFunction​(nu.xom.Element element)  
      boolean isNamespaceLenient()
      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 isParseJsonLiterals()
      Returns whether JSON literals are parsed as JSON or not.
      boolean isRemoveNamespacePrefixFromElements()
      Returns whether this serializer will remove namespace prefix from elements or not.
      boolean isSkipNamespaces()
      Returns whether this serializer will skip adding namespace declarations to elements or not.
      boolean isSkipWhitespace()
      Returns whether this serializer will skip whitespace or not.
      boolean isSortPropertyNames()
      Returns whether this serializer will sort object properties by name or not.
      boolean isTrimSpaces()
      Returns whether this serializer will trim leading and trailing 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.
      private nu.xom.Element newElement​(java.lang.String name)  
      private JSON processArrayElement​(nu.xom.Element element, java.lang.String defaultType)  
      private java.lang.Object processElement​(nu.xom.Element element, java.lang.String type)  
      private nu.xom.Element processJSONArray​(JSONArray array, nu.xom.Element root, java.lang.String[] expandableProperties)  
      private nu.xom.Element processJSONObject​(JSONObject jsonObject, nu.xom.Element root, java.lang.String[] expandableProperties, boolean isRoot)  
      private nu.xom.Element processJSONValue​(java.lang.Object value, nu.xom.Element root, nu.xom.Element target, java.lang.String[] expandableProperties)  
      private JSON processObjectElement​(nu.xom.Element element, java.lang.String defaultType)  
      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.
      If the elementName is null or blank, the namespace will be removed from the root element.
      private java.lang.String removeNamespacePrefix​(java.lang.String name)  
      void setArrayName​(java.lang.String arrayName)
      Sets the name used for JSONArray.
      Default is 'a'.
      void setElementName​(java.lang.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​(java.lang.String[] expandableProperties)
      Sets the list of properties to be expanded from child to parent.
      void setForcedArrayElements​(java.util.Collection<java.lang.String> forcedArrayElements)
      Defines the set of XML elements that force their children to be treated as array elements.
      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 setMappedPropertyNames​(java.util.Map<java.lang.String,​java.lang.String> mappedPropertyNames)
      Sets mappings between JSON properties to XML elements.
      void setNamespace​(java.lang.String prefix, java.lang.String uri)
      Sets the namespace declaration to the root element.
      Any previous values are discarded.
      void setNamespace​(java.lang.String prefix, java.lang.String uri, java.lang.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​(java.lang.String objectName)
      Sets the name used for JSONObject.
      Default is 'o'.
      private void setOrAccumulate​(JSONObject jsonObject, java.lang.String key, java.lang.Object value)  
      void setParseJsonLiterals​(boolean parseJsonLiterals)
      Sets whether JSON literals are parsed as JSON or not.
      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​(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 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 trailing 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 setValue​(JSONArray jsonArray, nu.xom.Element element, java.lang.String defaultType)  
      private void setValue​(JSONObject jsonObject, nu.xom.Element element, java.lang.String defaultType)  
      private java.lang.Object simplifyValue​(JSONObject parent, java.lang.Object json)  
      private java.lang.String trimSpaceFromValue​(java.lang.String value)  
      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.
      If the encoding string is null it will use UTF-8.
      private java.lang.String writeDocument​(nu.xom.Document doc, java.lang.String encoding)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_ARRAY

        private static final java.lang.String[] EMPTY_ARRAY
      • LOG

        private static final org.slf4j.Logger LOG
      • arrayName

        private java.lang.String arrayName
        the name for an JSONArray Element
      • elementName

        private java.lang.String elementName
        the name for an JSONArray's element Element
      • expandableProperties

        private java.lang.String[] expandableProperties
        list of properties to be expanded from child to parent
      • forceTopLevelObject

        private boolean forceTopLevelObject
      • namespaceLenient

        private boolean namespaceLenient
        flag to be tolerant for incomplete namespace prefixes
      • namespacesPerElement

        private final java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> namespacesPerElement
        Map of namespaces per element
      • objectName

        private java.lang.String objectName
        the name for an JSONObject Element
      • removeNamespacePrefixFromElements

        private boolean removeNamespacePrefixFromElements
        flag for trimming namespace prefix from element name
      • rootName

        private java.lang.String rootName
        the name for the root Element
      • rootNamespace

        private final java.util.Map<java.lang.String,​java.lang.String> rootNamespace
        Map of namespaces for root element
      • skipNamespaces

        private boolean skipNamespaces
        flag for skipping namespaces while reading
      • skipWhitespace

        private boolean skipWhitespace
        flag for skipping whitespace elements while reading
      • trimSpaces

        private boolean trimSpaces
        flag for trimming spaces from string values
      • typeHintsCompatibility

        private boolean typeHintsCompatibility
        flag for type hints naming compatibility
      • typeHintsEnabled

        private boolean typeHintsEnabled
        flag for adding JSON types hints as attributes
      • performAutoExpansion

        private boolean performAutoExpansion
        flag for performing auto-expansion of arrays if
      • keepCData

        private boolean keepCData
        flag for if text with CDATA should keep the information in the value or not
      • escapeLowerChars

        private boolean escapeLowerChars
        flag for if characters lower than ' ' should be escaped in texts.
      • keepArrayName

        private boolean keepArrayName
        flag for if array name should be kept in JSON data
      • sortPropertyNames

        private boolean sortPropertyNames
        flag for sorting object properties by name
      • forcedArrayElements

        private final java.util.Collection<java.lang.String> forcedArrayElements
        set of element names that forces its children elements to be in an Array
      • parseJsonLiterals

        private boolean parseJsonLiterals
        should JSON literals be parsed or not
      • mappedPropertyNames

        private final java.util.Map<java.lang.String,​java.lang.String> mappedPropertyNames
        Map JSON property names to XML elements
    • Constructor Detail

      • XMLSerializer

        public XMLSerializer()
        Creates a new XMLSerializer with default options.
        • objectName: 'o'
        • arrayName: 'a'
        • elementName: 'e'
        • typeHintsEnabled: true
        • typeHintsCompatibility: true
        • namespaceLenient: false
        • expandableProperties: []
        • skipNamespaces: false
        • removeNameSpacePrefixFromElement: false
        • trimSpaces: false
        • expandableProperties: []
        • skipWhitespace: false
        • performAutoExpansion: false
        • keepCData: false
        • escapeLowerChars: false
        • keepArrayName: false
        • forcedArrayElements: []
        • parseJsonLiterals: true
        • sortPropertyNames: false
    • Method Detail

      • getMappedPropertyNames

        public java.util.Map<java.lang.String,​java.lang.String> getMappedPropertyNames()
        Returns mappings between JSON properties to XML elements.
      • setMappedPropertyNames

        public void setMappedPropertyNames​(java.util.Map<java.lang.String,​java.lang.String> mappedPropertyNames)
        Sets mappings between JSON properties to XML elements.
      • addMappedPropertyName

        public void addMappedPropertyName​(java.lang.String json,
                                          java.lang.String xml)
        Add a mapped JSON property name to XML element.
      • isParseJsonLiterals

        public boolean isParseJsonLiterals()
        Returns whether JSON literals are parsed as JSON or not.
      • setParseJsonLiterals

        public void setParseJsonLiterals​(boolean parseJsonLiterals)
        Sets whether JSON literals are parsed as JSON or not.
      • 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

        public void addNamespace​(java.lang.String prefix,
                                 java.lang.String uri)
        Adds a namespace declaration to the root element.
        Parameters:
        prefix - namespace prefix
        uri - namespace uri
      • addNamespace

        public void addNamespace​(java.lang.String prefix,
                                 java.lang.String uri,
                                 java.lang.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.
        Parameters:
        prefix - namespace prefix
        uri - namespace uri
        elementName - name of target element
      • getRootNamespace

        public java.util.Map<java.lang.String,​java.lang.String> getRootNamespace()
        Returns a read-only view of the root name space.
      • getElementNamespace

        public java.util.Map<java.lang.String,​java.lang.String> getElementNamespace​(java.lang.String elementName)
        Returns a read-only view of the particular element name space if found.
      • clearNamespaces

        public void clearNamespaces()
        Removes all namespaces declarations (from root an elements).
      • clearNamespaces

        public void clearNamespaces​(java.lang.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.
        Parameters:
        elementName - name of target element
      • getArrayName

        public java.lang.String getArrayName()
        Returns the name used for JSONArray.
      • setArrayName

        public void setArrayName​(java.lang.String arrayName)
        Sets the name used for JSONArray.
        Default is 'a'.
      • getElementName

        public java.lang.String getElementName()
        Returns the name used for JSONArray elements.
      • setElementName

        public void setElementName​(java.lang.String elementName)
        Sets the name used for JSONArray elements.
        Default is 'e'.
      • getExpandableProperties

        public java.lang.String[] getExpandableProperties()
        Returns a list of properties to be expanded from child to parent.
      • setExpandableProperties

        public void setExpandableProperties​(java.lang.String[] expandableProperties)
        Sets the list of properties to be expanded from child to parent.
      • getObjectName

        public java.lang.String getObjectName()
        Returns the name used for JSONArray.
      • setObjectName

        public void setObjectName​(java.lang.String objectName)
        Sets the name used for JSONObject.
        Default is 'o'.
      • getRootName

        public java.lang.String getRootName()
        Returns the name used for the root element.
      • setRootName

        public void setRootName​(java.lang.String rootName)
        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 trailing whitespace from values or not.
      • setTrimSpaces

        public void setTrimSpaces​(boolean trimSpaces)
        Sets if this serializer will trim leading and trailing 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.
      • getForcedArrayElements

        public java.util.Collection<java.lang.String> getForcedArrayElements()
        Returns the set of XML elements that force their children to be treated as array elements.
      • setForcedArrayElements

        public void setForcedArrayElements​(java.util.Collection<java.lang.String> forcedArrayElements)
        Defines the set of XML elements that force their children to be treated as array elements.
      • read

        public JSON read​(java.lang.String xml)
        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

        public JSON readFromFile​(java.io.File file)
        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

        public JSON readFromFile​(java.lang.String path)
        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

        public JSON readFromStream​(java.io.InputStream stream)
        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

        public void removeNamespace​(java.lang.String prefix)
        Removes a namespace from the root element.
        Parameters:
        prefix - namespace prefix
      • removeNamespace

        public void removeNamespace​(java.lang.String prefix,
                                    java.lang.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.
        Parameters:
        prefix - namespace prefix
        elementName - name of target element
      • setNamespace

        public void setNamespace​(java.lang.String prefix,
                                 java.lang.String uri)
        Sets the namespace declaration to the root element.
        Any previous values are discarded.
        Parameters:
        prefix - namespace prefix
        uri - namespace uri
      • setNamespace

        public void setNamespace​(java.lang.String prefix,
                                 java.lang.String uri,
                                 java.lang.String elementName)
        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 prefix
        uri - namespace uri
        elementName - 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

        public java.lang.String write​(JSON json)
        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

        public java.lang.String write​(JSON json,
                                      java.lang.String encoding)
        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 transform
        encoding - 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

        private java.lang.String addJsonPrefix​(java.lang.String str)
      • addNameSpaceToElement

        private void addNameSpaceToElement​(nu.xom.Element element)
      • checkChildElements

        private boolean checkChildElements​(nu.xom.Element element,
                                           boolean isTopLevel)
      • getClass

        private java.lang.String getClass​(nu.xom.Element element)
      • getType

        private java.lang.String getType​(nu.xom.Element element)
      • getType

        private java.lang.String getType​(nu.xom.Element element,
                                         java.lang.String defaultType)
      • 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

        private nu.xom.Element newElement​(java.lang.String name)
      • processArrayElement

        private JSON processArrayElement​(nu.xom.Element element,
                                         java.lang.String defaultType)
      • processElement

        private java.lang.Object processElement​(nu.xom.Element element,
                                                java.lang.String type)
      • processJSONArray

        private nu.xom.Element processJSONArray​(JSONArray array,
                                                nu.xom.Element root,
                                                java.lang.String[] expandableProperties)
      • processJSONObject

        private nu.xom.Element processJSONObject​(JSONObject jsonObject,
                                                 nu.xom.Element root,
                                                 java.lang.String[] expandableProperties,
                                                 boolean isRoot)
      • canAutoExpand

        private boolean canAutoExpand​(JSONArray array)
        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

        private nu.xom.Element processJSONValue​(java.lang.Object value,
                                                nu.xom.Element root,
                                                nu.xom.Element target,
                                                java.lang.String[] expandableProperties)
      • processObjectElement

        private JSON processObjectElement​(nu.xom.Element element,
                                          java.lang.String defaultType)
      • removeNamespacePrefix

        private java.lang.String removeNamespacePrefix​(java.lang.String name)
      • setOrAccumulate

        private void setOrAccumulate​(JSONObject jsonObject,
                                     java.lang.String key,
                                     java.lang.Object value)
      • setValue

        private void setValue​(JSONArray jsonArray,
                              nu.xom.Element element,
                              java.lang.String defaultType)
      • setValue

        private void setValue​(JSONObject jsonObject,
                              nu.xom.Element element,
                              java.lang.String defaultType)
      • isCData

        private boolean isCData​(nu.xom.Element element)
      • simplifyValue

        private java.lang.Object simplifyValue​(JSONObject parent,
                                               java.lang.Object json)
      • trimSpaceFromValue

        private java.lang.String trimSpaceFromValue​(java.lang.String value)
      • writeDocument

        private java.lang.String writeDocument​(nu.xom.Document doc,
                                               java.lang.String encoding)