Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Release Reference DeepWiki Test

Insights Insights

go-xmljson

XML and JSON conversion library for Go.

Features

  • Customizable XML to JSON conversion/transformation.
  • Customizable JSON to XML conversion/transformation.
  • Conventional transformation methods support.
    • BadgerFish
    • RayFish
    • Simple (Simplified conversion rulesets)
  • Applicable for SOAP/REST, REST/SOAP transformation.
  • Fast.
  • Zero dependency.

Usages

BadgerFish convention

XML/JSON conversion with BadgerFish convention.

Create a converter instance:

// With default encode-decoder.
c := xmljson.Converter{
  EncodeDecoder: xmljson.NewBadgerFish(),
}

// With custom encode-decoder.
c := xmljson.Converter{
  EncodeDecoder: &xmljson.&adgerFish{
    TextKey:      "$",
    AttrPrefix:   "@",
    NamespaceSep: ":",
    TrimSpace:    true,
  },
}

Convert from XML to JSON:

b, err := c.XMLtoJSON([]byte(`<alice charlie="david">bob</alice>`))
fmt.Println(string(b))
// {"alice":{"$":"bob","@charlie":"david"}}

Convert from JSON to XML:

b, err := c.JSONtoXML([]byte(`{"alice":{"$":"bob","@charlie":"david"}}`))
fmt.Println(string(b))
// <alice charlie="david">bob</alice>

Rayfish convention

XML/JSON conversion with Rayfish convention.

Create a converter instance:

// With default encode-decoder.
c := xmljson.Converter{
  EncodeDecoder: xmljson.NewRayFish(),
}

// With custom encode-decoder.
c := xmljson.Converter{
  EncodeDecoder: &xmljson.RayFish{
    NameKey:      "#name",
    TextKey:      "#text",
    ChildrenKey:  "#children",
    AttrPrefix:   "@",
    NamespaceSep: ":",
    TrimSpace:    true,
  },
}

Convert from XML to JSON:

b, err := c.XMLtoJSON([]byte(`<alice charlie="david">bob</alice>`))
fmt.Println(string(b))
// {"#children":[{"#children":[],"#name":"@charlie","#text":"david"}],"#name":"alice","#text":"bob"}

Convert from JSON to XML:

b, err := c.JSONtoXML([]byte(`
{
  "#name": "alice",
  "#text": "bob",
  "#children": [
    {
      "#name": "@charlie",
      "#text": "david",
      "#children": []
    }
  ]
}
`))

fmt.Println(string(b))
// <alice charlie="david">bob</alice>

Simple converter

Simple converts XML/JSON with simpler rules than BadgerFish or Rayfish.

Create a converter instance:

// With default encode-decoder.
c := xmljson.Converter{
  EncodeDecoder: xmljson.NewSimple(),
}

// With custom encode-decoder.
c := xmljson.Converter{
  EncodeDecoder: &xmljson.Simple{
    TextKey:      "$",
    AttrPrefix:   "@",
    NamespaceSep: ":",
    TrimSpace:    true,
    PreferShort:  true,
  },
}

Convert from XML to JSON:

b, err := c.XMLtoJSON([]byte(`<alice charlie="david">bob</alice>`))
fmt.Println(string(b))
// {"alice":{"$":"bob","@charlie":"david"}}

Convert from JSON to XML:

b, err := c.JSONtoXML([]byte(`{"alice":{"$":"bob","@charlie":"david"}}`))
fmt.Println(string(b))
// <alice charlie="david">bob</alice>

Docs & Examples

Benchmarks

XML to JSON, JSON to XML benchmark results. testdata/xml/00_wiki.xml and corresponding json data are used.

See benchmark_test.go.

BenchmarkSimple_XMLtoJSON-8        8808   170946 ns/op   45126 B/op    711 allocs/op
BenchmarkSimple_JSONtoXML-8        5277   200499 ns/op   51323 B/op    641 allocs/op
BenchmarkRayFish_XMLtoJSON-8       7983   305133 ns/op   60770 B/op   1470 allocs/op
BenchmarkRayFish_JSONtoXML-8       5449   224865 ns/op   92628 B/op   1096 allocs/op
BenchmarkBadgerFish_XMLtoJSON-8    9214   132736 ns/op   56156 B/op    814 allocs/op
BenchmarkBadgerFish_JSONtoXML-8   10000   114447 ns/op   51803 B/op    650 allocs/op

References

Releases

Contributors

Languages