nats-io / nats.net

Full Async C# / .NET client for NATS
https://nats-io.github.io/nats.net
Apache License 2.0
231 stars 48 forks source link

Accept multiple JSON serialization contexts in DI #504

Open mtmk opened 3 months ago

mtmk commented 3 months ago

Proposed change

public NatsBuilder AddJsonSerialization(params JsonSerializerContext[] contexts)
    => ConfigureOptions(opts =>
    {
        var jsonRegistry = new NatsJsonContextSerializerRegistry(contexts);
        return opts with { SerializerRegistry = jsonRegistry };
    });

...or collect them to add them in the end during build:

public NatsBuilder AddJsonSerialization(params JsonSerializerContext[] contexts)
    => _contexts.AddRange(contexts);

cc @rickdotnet

rickdotnet commented 2 months ago

I rushed this original implementation and missed that NatsJsonContextSerializerRegistry took in params.

If I pick this up, I'll probably opt for the first option unless someone has opinions against it.

rickdotnet commented 2 months ago

@mtmk , should we wait to see where https://github.com/nats-io/nats.net.v2/pull/530 goes? Or do you think it's worth getting this in sooner?

mtmk commented 2 months ago

@mtmk , should we wait to see where #530 goes? Or do you think it's worth getting this in sooner?

@rickdotnet yes good idea. There might be multiple issues and PRs. I might also create a project or milestone for these as well once we have consensus.