Documentation
¶
Overview ¶
j2x package - mirror of x2j package
Marshal XML docs from arbitrary JSON and map[string]interface{} values.
Copyright 2013 Charles Banning. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file
Marshal dynamic / arbitrary XML docs from arbitrary JSON string and map[string]interface{} values.
NOTICE: 01-Jun-2014, package deprecated. Please see http://github.com/clbanning/mxj/j2x.
Compliments the x2j package functions.
Uses x2j conventions:
- Keys that begin with a hyphen, '-', are treated as attributes.
- The "#text" key is treated as the value for a simple element.
Map values that are not standard JSON types - can be a structure, etc. - are marshal'd using xml.Marshal().
However, attribute keys are restricted to string, numeric, or boolean types.
If the map[string]interface{} has a single key, it is used as the XML root tag. If it doesn't have
a single key, then a root tag - rootTag - must be provided or the default root tag value is used.
EMPTY ELEMENT ENCODING
Empty (nil) elements or elements with only attributes are encoded as "<tag .../>". The standard library
encoding/xml package encodes them as "<tag ...></tag>". If you're marshaling a map with structure values
and want a consistent syntax, use the xml_marshal hack of the standard library that conforms encoding/xml
to the j2x convention.
Deprecated: Use github.com/clbanning/mxj
Index ¶
- Constants
- func JsonReaderToMap(rdr io.Reader) (map[string]interface{}, *[]byte, error)
- func JsonReaderToStruct(rdr io.Reader, structPtr interface{}) (*[]byte, error)
- func JsonReaderToXml(rdr io.Reader, rootTag ...string) ([]byte, *[]byte, error)
- func JsonReaderToXmlWriter(rdr io.Reader, wtr io.Writer, rootTag ...string) (*[]byte, *[]byte, error)
- func JsonToXml(jsonString []byte, rootTag ...string) ([]byte, error)
- func JsonToXmlIndent(jsonString []byte, prefix, indent string, rootTag ...string) ([]byte, error)
- func JsonToXmlWriter(b []byte, wtr io.Writer) (*[]byte, error)
- func MapToXml(m map[string]interface{}, rootTag ...string) ([]byte, error)
- func MapToXmlIndent(m map[string]interface{}, prefix, indent string, rootTag ...string) ([]byte, error)
- func MapToXmlWriter(m map[string]interface{}, wtr io.Writer) (*[]byte, error)
- func Marshal(v interface{}, rootTag ...string) ([]byte, error)
- func MarshalIndent(v interface{}, prefix, indent string, rootTag ...string) ([]byte, error)
- func UseGoXmlEmptyElemSyntax()
- func UseJ2xEmptyElemSyntax()
Constants ¶
const (
DefaultRootTag = "doc"
)
Variables ¶
This section is empty.
Functions ¶
func JsonReaderToMap ¶
JsonReaderToMap wraps json.Unmarshal() with an io.Reader. Repeated calls will bulk process the stream of anonymous JSON strings. The function returns: map[string]interface{}, pointer to source JSON value, error.
func JsonReaderToStruct ¶
JsonReaderToStruct - wraps json.Unmarshal to load instances of a structure. The function returns: pointer to source JSON value, error - structPtr holds the data.
func JsonReaderToXml ¶
JsonReaderToXml implements JsonToXml() by wrapping MapToXml() with an io.Reader. Repeated calls will bulk process the stream of anonymous JSON strings. The function returns: XML string, pointer to source JSON value, error.
func JsonReaderToXmlWriter ¶
func JsonReaderToXmlWriter(rdr io.Reader, wtr io.Writer, rootTag ...string) (*[]byte, *[]byte, error)
Decodes next value from a JSON io.Reader and writes it using io.Writer Returns: pointer to JSON, pointer to encoded XML, error.
func JsonToXml ¶
Encode JSON value as XML. The inverse of x2j.DocToJson().
See MapToXml() for encoding rules.
func JsonToXmlWriter ¶
JsonToXmlWriter decodes JSON string and writes it using io.Writer Returns pointer to encoded XML, error.
func MapToXml ¶
Encode a map[string]interface{} variable as XML. The inverse of x2j.DocToMap(). The following rules apply.
- The key label "#text" is treated as the value for a simple element with attributes.
- Map keys that begin with a hyphen, '-', are interpreted as attributes. It is an error if the attribute doesn't have a []byte, string, number, or boolean value.
- Map value type encoding: > string, bool, float64, int, int32, int64, float32: per "%v" formating > []bool, []uint8: by casting to string > structures, etc.: handed to xml.Marshal() - if there is an error, the element value is "UNKNOWN"
- Elements with only attribute values or are null are terminated using "/>".
- If len(m) == 1 and no rootTag is provided, then the map key is used as the root tag. Thus, `{ "key":"value" }` encodes as `<key>value</key>`.
func MapToXmlIndent ¶
func MapToXmlIndent(m map[string]interface{}, prefix, indent string, rootTag ...string) ([]byte, error)
Encode a map[string]interface{} variable as a pretty XML string. See MapToXml().
func MapToXmlWriter ¶
MapToXmlWriter encodes the map as XML and writes in on the io.Writer. The function returns: pointer to encoded XML, error.
func Marshal ¶
Extends xml.Marshal() to handle JSON and map[string]interface{} types.
This is the inverse of x2j.Unmarshal().
Strings are interpreted as JSON strings; use xml.Marshal() to marshal
a string as "<string>...</string>" - the standard package handling.
Follows xml.Marshal handling of types except for string and map[string]interface{}
values. For more generalized marshal'ing use MapToXml().
See MapToXml() for encoding rules.
func MarshalIndent ¶
Extends xml.MarshalIndent() to handle JSON and map[string]interface{} types. See Marshal().
func UseGoXmlEmptyElemSyntax ¶
func UseGoXmlEmptyElemSyntax()
UseGoXmlEmptyElemSyntax() - <tag ...></tag> rather than <tag .../>
func UseJ2xEmptyElemSyntax ¶
func UseJ2xEmptyElemSyntax()
UseJ2xEmptyElemSyntax() - <tag .../> rather than <tag ...></tag>
Types ¶
This section is empty.