cpp-hocon icon indicating copy to clipboard operation
cpp-hocon copied to clipboard

Array at root?

Open tamboril opened this issue 9 years ago • 4 comments

If the JSON I want is an array at the top level, how do I parse that from a HOCON string?

I get an exception when parsing "[ { op: replace } ]" string: has type list rather than object at file root

tamboril avatar Dec 20 '16 15:12 tamboril

JSON specifically does not allow you to parse something that does not have curly braces at root. Reading through the docs, it looks like HOCON allows you to omit the curly braces, but it still assumes that the thing at root can be converted to an object, which a list cannot. You could try adding curly braces around it (not sure if that's an option). I'd need to investigate more to come up with other solutions, but the code that is erroring is here: https://github.com/puppetlabs/cpp-hocon/blob/master/lib/src/parseable.cc#L124. That function is the last thing called before the config object gets returned, so it attempts to coerce everything to an object at the top level.

Magisus avatar Dec 20 '16 18:12 Magisus

After double-checking RFC 4627, I'm pretty sure an array at the top is allowed. It can be either an object or an array.

Works with nlohmann's library and on jsonlint.com: [ {"a": 1} ]

tamboril avatar Dec 20 '16 18:12 tamboril

Yeah you're right, I found the place that specifies that now... I'll have to play around with it more to see if we ever tested that functionality, and if not, where our code differs from the code that supports that.

Magisus avatar Dec 20 '16 18:12 Magisus

https://github.com/lightbend/config/blob/master/HOCON.md#include-semantics-merging specifically talks about this behavior of both HOCON and JSON, and how included files differ.

MikaelSmith avatar Feb 01 '19 17:02 MikaelSmith