8000 Structs can't parse json with null values · Issue #7257 · winglang/wing · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Structs can't parse json with null values #7257
Closed
@ofiesh

Description

@ofiesh

Use Case

When a json string has a null value, parsing a struct with optional fields fails.

I would expect { "foo": null, "bar": "bar" } to parse into the struct:

Foo {
  foo: nil
  bar: "bar"
}

Example of failure:

let json = "\{\"foo\": null, \"bar\": \"bar\"}";
struct Foo {
  foo: str?;
  bar: str?;
}
Foo.parseJson(json);
Error: unable to parse Foo:
- Foo/foo must be string
  --> wing/main.w:6:1
  |   foo: str?;
  | }
  | 
6 | Foo.parseJson(json);
  | ^
at /app/wing/main.w:6:1

I get why this is happening in wing, but null keys in json are pretty common and this makes parsing directly to structs pretty unsafe.

Proposed Solution

If the field is optional, it should be set to nil.

i.e. {"foo": null} should give parse into the struct when foo is optional:

Foo {
  foo: nil
}

Implementation Notes

No response

Component

No response

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.
  • If this issue is labeled needs-discussion, it means the spec has not been finalized yet. Please reach out on the #dev channel in the Wing Discord.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0