Cysharp / ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
MIT License
1.66k stars 95 forks source link

[Question] Support for Object binding and ConsoleApp configuration for Code Generated Serialize - JsonSerializerContext? #146

Closed Simonl9l closed 2 months ago

Simonl9l commented 2 months ago

Today it seem that serialization options can be provided via ConsoleApp.JsonSerializerOptions, and this is used when deserializing Object bindings?

Given the code generated nature and implicit support for AoT, it also seems sensible to support Code Generated Serializers and specifically JsonSerializerContext. Is it intended to configure this like so:

 ConsoleApp.JsonSerializerOptions = new JsonSerializerOptions
  {
     TypeInfoResolverChain = { MyOptionsSerializerContext.Default }
  };

I'm not sure if this generated code, actually leverages the code generates serializer:

try { arg0 = System.Text.Json.JsonSerializer.Deserialize<global::MyOptions>(args[++i], JsonSerializerOptions); } catch { ThrowArgumentParseFailed("options", args[i]); }
arg0Parsed = true;
break;

I'd expect it more to be:

try { arg0 = System.Text.Json.JsonSerializer.Deserialize(args[++i], MyOptionsSerializerContext.Default.MyOptions); } catch { ThrowArgumentParseFailed("options", args[i]); }
arg0Parsed = true;
break;
neuecc commented 2 months ago

If you set it in JsonSerializerOptions, any JsonSerializerContext will be used. I don't think there's any problem with that.

Simonl9l commented 2 months ago

I've not set up a test to enable and to re-validate this, but believe that we'll run into AoT issues (warnings at least) if one passes in the serializer via a JsonSerializerOptions as it does (from recollection) not pickup the JsonSerializerContext semantics.

If I get a chance I'll reverify.