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

Peak to see if 'kind' is at top of object, if so deserialize as that type #14

Open
onionhammer opened this issue May 13, 2023 · 0 comments

Comments

@onionhammer
Copy link
Contributor

onionhammer commented May 13, 2023

PoC:

        var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));

        // Copy the reader struct by value to avoid advancing the `reader`
        var peakCopy = reader;
        Assert.True(peakCopy.Read());

        // Start reading the object
        Assert.Equal(JsonTokenType.StartObject, peakCopy.TokenType);
        Assert.True(peakCopy.Read());

        Assert.Equal(JsonTokenType.PropertyName, peakCopy.TokenType);

        // Peak the property name
        var propertyName = peakCopy.GetString();
        if (propertyName == "$type")
        {
            // Assign the input ref to the peak
            reader = peakCopy;

            // Read the type value
            Assert.True(reader.Read());
            Assert.Equal(JsonTokenType.String, reader.TokenType);
            var typeName = reader.GetString();

            // Read the remaining properties for the descendent type here...
        }
        else if (propertyName != null)
        {
            // Read the remaining properties for the descendent type here...
            var element = JsonElement.ParseValue(ref reader);

            // Get the $type property
            if (!element.TryGetProperty("$type", out var typeName))
            {
                throw new JsonException("Missing $type property.");
            }

            // Deserialize the `element` as the desired type
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant