j2x

package module
v0.0.0-...-ece330a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2016 License: BSD-3-Clause Imports: 5 Imported by: 0

README

j2x.go - Encode arbitrary JSON strings or map[string]interface{} values as XML.

NOTICE:

AS OF 3-FEB-2014 THE j2x PACKAGE IS NO LONGER SUPPORTED.

IT HAS BEEN DEPRECATED IN FAVOR OF mxj/j2x: https://github.com/clbanning/mxj/tree/master/j2x.

ANNOUNCEMENTS

01/23/14

NOTICE: FUNCTIONS HAVE BEEN RENAMED AND ARG/RETURN TYPES CHANGED. NOT BACKWARDS COMPATIBLE!

Added io.Writer and io.ReadWriter functions.  With the JsonReaderToXmlWriter() function
you can open a file/stream of JSON messages and write them out to a file/stream as XML.

01/22/14

Enable Go XML package encoding of empty elements - <tag ...></tag> instead of <tag .../>.

01/16/14

Included our io.Reader wrapper for bulk processing JSON strings to map[string]interface{}
or XML string values. See: JsonReaderToMap() and JsonReaderToDoc().

12/18/13

xml_marshal folder contains modified pkg/encoding/xml/marshal.go file that allows 
xml.Marshal() and xml.MarshalIndent() to use the j2x empty element encoding convention.

MOTIVATION

As part of our message hub project we want to make XML documents first class members
and had written the x2j package (http://godoc.org/github.com/clbanning/x2j) to parse
and filter XML encoded messages.  This package allows filtered messages to be delivered
as XML docs even if they are published to the hub as JSON encoded messages.

USAGE

The package is fairly well self-documented. (http://godoc.org/github.com/clbanning/j2x)  

XML ENCODING CONVENTIONS

   - JSON and map[string]interface{} keys that are prefixed with a hyphen, '-',
     are treated as attributes.
   - The value for the key '#text' is treated as the value for a simple element.
   - map[string]interface{} member values that are not standard JSON types - numbers,
	  character strings, boolean values, lists and JSON strings - are marshal'd using
	  xml.Marshal.  Thus, values can be complex structures, etc.

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

View Source
const (
	DefaultRootTag = "doc"
)

Variables

This section is empty.

Functions

func JsonReaderToMap

func JsonReaderToMap(rdr io.Reader) (map[string]interface{}, *[]byte, error)

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

func JsonReaderToStruct(rdr io.Reader, structPtr interface{}) (*[]byte, error)

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

func JsonReaderToXml(rdr io.Reader, rootTag ...string) ([]byte, *[]byte, error)

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

func JsonToXml(jsonString []byte, rootTag ...string) ([]byte, error)

Encode JSON value as XML. The inverse of x2j.DocToJson().

See MapToXml() for encoding rules.

func JsonToXmlIndent

func JsonToXmlIndent(jsonString []byte, prefix, indent string, rootTag ...string) ([]byte, error)

Encode a JSON string as pretty XML string.

See JsonToXml().

func JsonToXmlWriter

func JsonToXmlWriter(b []byte, wtr io.Writer) (*[]byte, error)

JsonToXmlWriter decodes JSON string and writes it using io.Writer Returns pointer to encoded XML, error.

func MapToXml

func MapToXml(m map[string]interface{}, rootTag ...string) ([]byte, error)

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

func MapToXmlWriter(m map[string]interface{}, wtr io.Writer) (*[]byte, error)

MapToXmlWriter encodes the map as XML and writes in on the io.Writer. The function returns: pointer to encoded XML, error.

func Marshal

func Marshal(v interface{}, rootTag ...string) ([]byte, error)

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

func MarshalIndent(v interface{}, prefix, indent string, rootTag ...string) ([]byte, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL