XML and JSON conversion library for Go.
- 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.
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>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 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>- GoDoc: https://pkg.go.dev/github.com/aileron-projects/go-xmljson
- Examples:
- example_test.go
- XML to JSON: examples/xml-to-json/
- JSON to XML: examples/json-to-xml/
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