wivuu / Wivuu.JsonPolymorphism

Adds System.Text.Json serializer support for polymorphism with a discriminator attribute
MIT License
25 stars 3 forks source link

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

Open onionhammer opened 1 year ago

onionhammer commented 1 year ago

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
        }