Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonElement with Undefined kind #42249

Closed
DsAekb opened this issue Sep 15, 2020 · 2 comments
Closed

JsonElement with Undefined kind #42249

DsAekb opened this issue Sep 15, 2020 · 2 comments
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@DsAekb
Copy link

DsAekb commented Sep 15, 2020

Here it is said that JsonElement.WriteTo throws the InvalidOperationException when the ValueKind is Undefined.

My question is why it does that? How is Undefined value not valid for the serialization/deserialization?

This behaviour doesn't allow to use Undefined together with System.Text.Json even though it is declared there as a valid value.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Sep 15, 2020
@bartonjs bartonjs added question Answer questions and provide assistance, not an issue with source code or documentation. and removed untriaged New issue has not been triaged by the area owner labels Sep 15, 2020
@bartonjs bartonjs added this to the Future milestone Sep 15, 2020
@bartonjs
Copy link
Member

A JsonElement with the Undefined ValueKind represents effectively uninitialized data (the only way to get one should be default(JsonElement)), and is logically equivalent to the value in ECMAScript when reading an unset property (var data = obj["some property no one set"];). There's no representation of that concept in JSON, because if anything gets written down the value is no longer Undefined, it's whatever defined value it had. Writing it as the empty string would possibly work, but would result in an invalid JSON document.

Utf8JsonReader has a similar Undefined concept because it's also a struct, and needs to describe the state that it's in if it gets created with default(Utf8JsonReader). Neither of these values should be encountered in normal processing.

Is there somewhere that you're encountering Unknown elements on a regular basis?

@DsAekb
Copy link
Author

DsAekb commented Sep 16, 2020

Hey @bartonjs, thanks for your reply. Indeed undefined is not a valid value for JSON unlike in JavaScript, my bad.

@DsAekb DsAekb closed this as completed Sep 16, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

3 participants