buglabs / node-xml2json

Converts XML to JSON using node-expat
810 stars 209 forks source link

What is the meaning of $t in reversible parsing? #196

Open silversonicaxel opened 4 years ago

silversonicaxel commented 4 years ago

I'm using this parser command parser.toJson(XMLContent, { reversible: true } and it is not yet totally clear to me why from this xml:

const xmlData = `<rss><title>example</title></rss>`

I reach this json:

const jsonData = { rss: { title: {"$t":"example"}}}

and not this json:

const jsonData = { rss: { title: "example"}}

Can someone explain me why, because documentation is lacking here, or at least, I was not able to understand the reason checking source code here

- reversible: If true, the parser generates a reversible JSON, mainly
              characterized by the presence of the property $t.

at line https://github.com/buglabs/node-xml2json/blob/98272646816c5f5747970ee6906e206c1fdd6082/lib/xml2json.js#L128

thanks

c4milo commented 4 years ago

It means text. When serializing it back to XML, it knows the value needs to be an element instead of an attribute.

silversonicaxel commented 4 years ago

I guess it is not possible to remove it, right? Just checking, because the tree changes a according to the parameter reversible set to

false

- rss
  - title -> example

or

true

- rss
  - title
    - $t -> example

is there a way to have this documented maybe in README, including all small changes of the output JSON and make it crystal clear how the result is formatted?