11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- using Azure . Core ;
54using Azure . Generator . Primitives ;
65using Azure . Generator . Providers ;
76using Azure . Generator . Providers . Abstraction ;
87using Microsoft . Generator . CSharp . ClientModel ;
98using Microsoft . Generator . CSharp . ClientModel . Providers ;
10- using Microsoft . Generator . CSharp . ClientModel . Snippets ;
119using Microsoft . Generator . CSharp . Expressions ;
1210using Microsoft . Generator . CSharp . Input ;
1311using Microsoft . Generator . CSharp . Primitives ;
1614using System ;
1715using System . ClientModel . Primitives ;
1816using System . Text . Json ;
19- using static Microsoft . Generator . CSharp . Snippets . Snippet ;
2017
2118namespace Azure . Generator
2219{
@@ -66,7 +63,7 @@ public class AzureTypeFactory : ScmTypeFactory
6663 InputPrimitiveType ? primitiveType = inputType ;
6764 while ( primitiveType != null )
6865 {
69- if ( KnownAzureTypes . PrimitiveTypes . TryGetValue ( primitiveType . CrossLanguageDefinitionId , out var knownType ) )
66+ if ( KnownAzureTypes . TryGetPrimitiveType ( primitiveType . CrossLanguageDefinitionId , out var knownType ) )
7067 {
7168 return knownType ;
7269 }
@@ -78,40 +75,34 @@ public class AzureTypeFactory : ScmTypeFactory
7875 }
7976
8077 /// <inheritdoc/>
81- public override ValueExpression GetValueTypeDeserializationExpression ( Type valueType , ScopedApi < JsonElement > element , SerializationFormat format )
78+ public override ValueExpression DeserializeJsonValue ( Type valueType , ScopedApi < JsonElement > element , SerializationFormat format )
8279 {
83- var expression = GetValueTypeDeserializationExpressionCore ( valueType , element , format ) ;
84- return expression ?? base . GetValueTypeDeserializationExpression ( valueType , element , format ) ;
80+ var expression = DeserializeJsonValueCore ( valueType , element , format ) ;
81+ return expression ?? base . DeserializeJsonValue ( valueType , element , format ) ;
8582 }
8683
87- private ValueExpression ? GetValueTypeDeserializationExpressionCore (
84+ private ValueExpression ? DeserializeJsonValueCore (
8885 Type valueType ,
8986 ScopedApi < JsonElement > element ,
9087 SerializationFormat format )
9188 {
92- return valueType switch
93- {
94- Type t when t == typeof ( ResourceIdentifier ) =>
95- New . Instance ( valueType , element . GetString ( ) ) ,
96- _ => null ,
97- } ;
89+ return KnownAzureTypes . TryGetJsonDeserializationExpression ( valueType , out var deserializationExpression ) ?
90+ deserializationExpression ( new CSharpType ( valueType ) , element , format ) :
91+ null ;
9892 }
9993
10094 /// <inheritdoc/>
101- public override MethodBodyStatement SerializeValueType ( CSharpType type , SerializationFormat serializationFormat , ValueExpression value , Type valueType , ScopedApi < Utf8JsonWriter > utf8JsonWriter , ScopedApi < ModelReaderWriterOptions > mrwOptionsParameter )
95+ public override MethodBodyStatement SerializeJsonValue ( Type valueType , ValueExpression value , ScopedApi < Utf8JsonWriter > utf8JsonWriter , ScopedApi < ModelReaderWriterOptions > mrwOptionsParameter , SerializationFormat serializationFormat )
10296 {
103- var statement = SerializeValueTypeCore ( type , serializationFormat , value , valueType , utf8JsonWriter , mrwOptionsParameter ) ;
104- return statement ?? base . SerializeValueType ( type , serializationFormat , value , valueType , utf8JsonWriter , mrwOptionsParameter ) ;
97+ var statement = SerializeValueTypeCore ( serializationFormat , value , valueType , utf8JsonWriter , mrwOptionsParameter ) ;
98+ return statement ?? base . SerializeJsonValue ( valueType , value , utf8JsonWriter , mrwOptionsParameter , serializationFormat ) ;
10599 }
106100
107- private MethodBodyStatement ? SerializeValueTypeCore ( CSharpType type , SerializationFormat serializationFormat , ValueExpression value , Type valueType , ScopedApi < Utf8JsonWriter > utf8JsonWriter , ScopedApi < ModelReaderWriterOptions > mrwOptionsParameter )
101+ private MethodBodyStatement ? SerializeValueTypeCore ( SerializationFormat serializationFormat , ValueExpression value , Type valueType , ScopedApi < Utf8JsonWriter > utf8JsonWriter , ScopedApi < ModelReaderWriterOptions > mrwOptionsParameter )
108102 {
109- return valueType switch
110- {
111- Type t when t == typeof ( ResourceIdentifier ) =>
112- utf8JsonWriter . WriteStringValue ( value . Property ( nameof ( ResourceIdentifier . Name ) ) ) ,
113- _ => null ,
114- } ;
103+ return KnownAzureTypes . TryGetJsonSerializationExpression ( valueType , out var serializationExpression ) ?
104+ serializationExpression ( value , utf8JsonWriter , mrwOptionsParameter , serializationFormat ) :
105+ null ;
115106 }
116107 }
117108}
0 commit comments