The use case is to generate (and parse) the following XML and JSON not creating separate structures for every of them.
XML
<xxx xmlns="http://example.org/ns">
<data type="plaintext">Hello</data>
<field1>Something1</field1>
<field2>Something2</field2>
...
</xxx>
JSON
{
"data": "Hello",
"data_type": "plaintext",
"field1": "Something1",
"field2": "Something2"
...
}
Possible solution would be:
type Xxx struct {
XMLName xml.Name `xml:"http://example.org/ns xxx" json:"-"`
// **If only "inline" attribute had existed**
Data Data `xml:"data" json:",inline"`
// There are a lot of other fields here
Field1 string `xml:"field1" json:"field1"`
Field2 string `xml:"field2" json:"field2"`
...
}
type Data struct {
Value string `xml:",chardata" json:"data"`
Type string `xml:"type,attr" data_type:"data"`
}
However, right now it produces the following JSON:
{"Data": {"data": "Hello", "type": "plaintext"}, "field1": "Something1", "field2": "Something2"}
which is not what I need. So, is there any other way to solve the problem not using separate structures for xml and json?