Skip to content

Use AOT-compatible JsonSerialization #5323

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,11 @@ private static MethodBodyStatement SerializeValue(Utf8JsonWriterExpression utf8J
switch (valueSerialization.Type.Implementation)
{
case SystemObjectType systemObjectType when IsCustomJsonConverterAdded(systemObjectType.SystemType):
if (valueSerialization.Options == JsonSerializationOptions.UseManagedServiceIdentityV3)
{
return new[]
{
Var("serializeOptions", New.JsonSerializerOptions(), out var serializeOptions),
JsonSerializerExpression.Serialize(utf8JsonWriter, value, serializeOptions).ToStatement()
};
}

return JsonSerializerExpression.Serialize(utf8JsonWriter, value).ToStatement();
var jsonModelInterface = new CSharpType(typeof(IJsonModel<>), systemObjectType.Type);
var cast = value.CastTo(jsonModelInterface);
// ((IJsonModel<T>)Value).Write(writer, options)
options ??= ModelReaderWriterOptionsExpression.Wire;
return cast.Invoke(nameof(IJsonModel<object>.Write), utf8JsonWriter, options).ToStatement();

case ObjectType:
return utf8JsonWriter.WriteObjectValue(value, options: options);
Expand Down Expand Up @@ -567,7 +562,11 @@ private static MethodBodyStatement SerializeFrameworkTypeValue(Utf8JsonWriterExp

if (IsCustomJsonConverterAdded(valueType))
{
return JsonSerializerExpression.Serialize(utf8JsonWriter, value).ToStatement();
var jsonModelInterface = new CSharpType(typeof(IJsonModel<>), valueType);
var cast = value.CastTo(jsonModelInterface);
// ((IJsonModel<T>)Value).Write(writer)
options ??= ModelReaderWriterOptionsExpression.Wire;
return cast.Invoke(nameof(IJsonModel<object>.Write), utf8JsonWriter, options).ToStatement();
}

throw new NotSupportedException($"Framework type {valueType} serialization not supported, please add `CodeGenMemberSerializationHooks` to specify the serialization of this type with the customized property");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;

namespace AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions
{
internal static class JsonModelExpression
{
public static InvokeInstanceMethodExpression Write(ValueExpression writer, ValueExpression value, ValueExpression? options = null)
{
var arguments = options is null
? new[] { writer, }
: new[] { writer, options };
return new InvokeInstanceMethodExpression(value, "Write", arguments, callAsAsync: false);
}
}
}
Loading