neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.35k stars 266 forks source link

Fix compilation error from generated code with readonly member and DataMemberAttribute #251

Open nullbus opened 2 years ago

nullbus commented 2 years ago

Hello, I had an issue with UniversalCodeGenerator.

Assume I have a struct like this:

[DataContract]
public struct Foo
{
    [DataMember(Name = "bar")]
    public readonly int Bar;

    public Foo(int Bar) => this.Bar = Bar;
}

The generated FooFormatter.Deserialize code from this struct is:

public global::Foo Deserialize(ref JsonReader reader, global::Utf8Json.IJsonFormatterResolver formatterResolver)
{
    if (reader.ReadIsNull())
    {
        throw new InvalidOperationException("typecode is null, struct not supported");
    }

    var __Bar__ = default(int);
    var __Bar__b__ = false;

    var ____count = 0;
    reader.ReadIsBeginObjectWithVerify();
    while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref ____count))
    {
        var stringKey = reader.ReadPropertyNameSegmentRaw();
        int key;
        if (!____keyMapping.TryGetValueSafe(stringKey, out key))
        {
            reader.ReadNextBlock();
            goto NEXT_LOOP;
        }

        switch (key)
        {
            case 0:
                __Bar__ = reader.ReadInt32();
                __Bar__b__ = true;
                break;
            default:
                reader.ReadNextBlock();
                break;
        }

        NEXT_LOOP:
        continue;
    }

    var ____result = new global::Foo(__bar__); // <<<<<<<<<<< this line

    return ____result;
}

That's because ObjectSerializationInfo.GetConstructorString works different like other template code does.

This PR fixes the issue.