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

ApiView feedback: custom code serialization abstraction #48

Conversation

trrwilson
Copy link
Collaborator

This does the standalone changes previous incorporated into #42 that address the ApiView feedback of extreme repetition in the needed custom serialization code.

It establishes a set of templated static helpers along with a common pattern of SerializeFoo/DeserializeFoo that are then passed as delegates into those helpers. This allows full flexibility with derived type partial (de)serialization relative to instance-only methods.

Together, this makes the common impl pattern fairly minimal:

public partial class Foo : IJsonModel<Foo>
{
    Foo IJsonModel<Foo>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
        => ModelSerializationHelpers.DeserializeNewInstance(this, DeserializeFoo, ref reader, options);

    Foo IPersistableModel<Foo>.Create(BinaryData data, ModelReaderWriterOptions options)
        => ModelSerializationHelpers.DeserializeNewInstance(this, DeserializeFoo, data, options);

    void IJsonModel<Foo>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        => ModelSerializationHelpers.SerializeInstance(this, SerializeFoo, writer, options);

    BinaryData IPersistableModel<Foo>.Write(ModelReaderWriterOptions options)
        => ModelSerializationHelpers.SerializeInstance(this, options);

    string IPersistableModel<Foo>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";

    internal static Foo DeserializeFoo(JsonElement element, ModelReaderWriterOptions options = default)
    {
        // Deserialize from element here, can easily call derived type methods as needed
    }

    internal static void SerializeFoo(Foo foo, Utf8JsonWriter writer, ModelReaderWriterOptions options)
    {
        // Serialize "foo" here, can invoke overriden derived type serializers like "WriteDerived" as needed
    }

    // As needed for type hierarchies
    internal abstract void WriteDerived(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}

@trrwilson trrwilson changed the base branch from user/travisw/feedback-changes-subset to main March 29, 2024 00:45
@trrwilson trrwilson changed the base branch from main to joseharriaga/CodeGenAttributes-Part1 April 26, 2024 22:44
@trrwilson
Copy link
Collaborator Author

Closing in favor of
#113

@trrwilson trrwilson closed this May 21, 2024
@trrwilson trrwilson deleted the user/travisw/serialization-abstraction branch May 21, 2024 07:51
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

Successfully merging this pull request may close these issues.

1 participant