Skip to content
This repository was archived by the owner on May 16, 2022. It is now read-only.

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

Open
nullbus wants to merge 1 commit intoneuecc:masterfrom
nullbus:fix-generated-construct-parameter
Open

Fix compilation error from generated code with readonly member and DataMemberAttribute#251
nullbus wants to merge 1 commit intoneuecc:masterfrom
nullbus:fix-generated-construct-parameter

Conversation

@nullbus
Copy link
Copy Markdown

@nullbus nullbus commented Jul 23, 2021

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.

@nullbus nullbus changed the title Fix compilation error from generated code with readonly member Fix compilation error from generated code with readonly member and DataMemberAttribute Jul 23, 2021
AMPing added a commit to AMPing/ZCS.Utf8Json that referenced this pull request Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant