JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
80 stars 16 forks source link

How to parse a dynamic JSON (without knowing its type)? #49

Closed hmassareli closed 10 months ago

hmassareli commented 1 year ago

I dont know if its possible, but I am trying to parse a JSON file that is unknown at the time I am trying to parse it, for example:

first JSON (possible entry) { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] } } } } } }

second JSON with an array of objects (possible entry also)

[ { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] } } } } } } ]

JairusSW commented 1 year ago

It can be done using https://github.com/MaxGraey/as-variant

mattjohnsonpint commented 1 year ago

How? JSON.parse<Variant>(s) doesn't work, and even if it did, how would I then access parts of the graph from there? Even putting Variant within another class doesn't seem to work, because it's not tagged with @json.

An example would be useful. Thanks.

mattjohnsonpint commented 1 year ago

This use case seems more like the example in the other JSON library for AssemblyScript: https://github.com/near/assemblyscript-json#parsing-json

But I really don't want to have two separate JSON libraries in a single project. 😅