Skip to content

UniValue should only parse the first JSON construct of a line #6558

@domob1812

Description

@domob1812

The current parser UniValue::read continues until it gets to the end of the parsed string (JTOK_NONE) or until an error occurs while lexing (JTOK_ERR). If the string contains, for instance, multiple top-level objects or arrays, they are all parsed and only the last is saved in the UniValue object. On the other hand, parsing fails if a valid object is followed by garbage. (While parsing does not fail, surprisingly, if the string starts with a JTOK_ERR.)

I think this behaviour is highly confusing and should be changed. I'm willing to work on this myself, but would like to discuss it before starting. My suggestion is the following:

Reading from a string should only parse until a full JSON construct is found (object or array as now, or a single number/string/keyword). Afterwards, parsing should stop and discard any trailing parts of the string completely. I want to add also the option to return (in an optional output argument) the position where parsing stopped, so that the caller may get everything in there or check that no unparsed content remains; I do not think that Bitcoin currently needs this functionality, but it is almost "for free" and may be useful in the future.

Does this sound like a good idea, or is there a problem with my plan that I missed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions