Skip to content

Need to update calls to JsonSerializer when dealing with an IJsonModel #5330

Open
@m-nash

Description

@m-nash

Related to Azure/azure-sdk-for-net#50279 and Azure/azure-sdk-for-net#50129

Today whenever a type is cross library boundary we fall back to JsonSerializer and expect the other library to have implemented a JsonConverter to support this. Before MRW this was our only way to support public serialization / deserialization.

With MRW we can do things slightly differently.

All calls to JsonSerializer.Serializer where the target is an IJsonModel should be updated to

((IJsonModel<T>)Property).Write(writer, options);

For calls to JsonSerializer.Deserializer where the target is an IJsonModel we will need to add a new helper in ModelSerializationExtension.

//ModelSerializationExtension

private readonly static JsonSerializerOptions s_options = new()
{
  Converters =
  {
    new JsonModelConverter(ModelSerializationExtensions.WireOptions, AzureResourceManagerCosmosDBContext.Default)
  }
};

[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "By passing in the JsonSerializerOptions with a reference to AzureResourceManagerCosmosDBContext.Default we are certain there is no AOT compat issue.")]
[UnconditionalSuppressMessage("Trimming", "IL3050", Justification = "By passing in the JsonSerializerOptions with a reference to AzureResourceManagerCosmosDBContext.Default we are certain there is no AOT compat issue.")]
public static T JsonDeserialize<T>(JsonProperty property, ModelReaderWriterOptions options)
{
  return JsonSerializer.Deserialize<T>(property.Value.GetRawText(), s_options);
}

With this new internal helper in place we can change the calls to

JsonDeserialize<T>(property, options);

This will ensure AOT compatibility for cross library boundary serialization and deserialization.

Metadata

Metadata

Assignees

Labels

v3Version 3 of AutoRest C# generator.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions