Skip to content

JSON support is still not first class #1685

Open
@timothysu

Description

@timothysu

First class JSON support is confusing, as a subset of valid JSON is not supported due to the current implementation expecting the parsed result to either be a Hash or Array[Hash], while the output of JSON.parse could also be String, Integer, or mixed type arrays.

The initial thought would be to have JSON targeting all valid JSON, while being able to take modifiers to limit the possible valid outputs, like:

requires :foo, type: JSON # this could result in  `Hash`, `String`, `Integer`, or `Array`, accepting any of the inputs detailed in the two lines below
requires :bar, type: JSON[Hash] # this limits the output to `Hash`, so  "{\"a\":\"hash\"}" is valid while "1",  "\"foo\"", and "[1, 2]" would be invalid inputs
requires :baz, type: JSON[Hash, String] # this would accept "\"foo\"" and "{\"a\":\"hash\"}" as valid, while "1" and "[1, 2]" are invalid

In addition, when the API is given a type that is already what the JSON is parsed to (currently that would be Hash or Array[Hash]) it still attempts to coerce it despite being already valid. For example, "{\"a\":\"hash\"}" is currently valid while { a: 'hash' } is not despite the desired parsed representation being the same. A PR modifying that logic will be linked soon.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions