1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
- using Azure . Core ;
5
4
using Azure . Generator . Primitives ;
6
5
using Azure . Generator . Providers ;
7
6
using Azure . Generator . Providers . Abstraction ;
8
7
using Microsoft . Generator . CSharp . ClientModel ;
9
8
using Microsoft . Generator . CSharp . ClientModel . Providers ;
10
- using Microsoft . Generator . CSharp . ClientModel . Snippets ;
11
9
using Microsoft . Generator . CSharp . Expressions ;
12
10
using Microsoft . Generator . CSharp . Input ;
13
11
using Microsoft . Generator . CSharp . Primitives ;
16
14
using System ;
17
15
using System . ClientModel . Primitives ;
18
16
using System . Text . Json ;
19
- using static Microsoft . Generator . CSharp . Snippets . Snippet ;
20
17
21
18
namespace Azure . Generator
22
19
{
@@ -66,7 +63,7 @@ public class AzureTypeFactory : ScmTypeFactory
66
63
InputPrimitiveType ? primitiveType = inputType ;
67
64
while ( primitiveType != null )
68
65
{
69
- if ( KnownAzureTypes . PrimitiveTypes . TryGetValue ( primitiveType . CrossLanguageDefinitionId , out var knownType ) )
66
+ if ( KnownAzureTypes . TryGetPrimitiveType ( primitiveType . CrossLanguageDefinitionId , out var knownType ) )
70
67
{
71
68
return knownType ;
72
69
}
@@ -78,40 +75,34 @@ public class AzureTypeFactory : ScmTypeFactory
78
75
}
79
76
80
77
/// <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 )
82
79
{
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 ) ;
85
82
}
86
83
87
- private ValueExpression ? GetValueTypeDeserializationExpressionCore (
84
+ private ValueExpression ? DeserializeJsonValueCore (
88
85
Type valueType ,
89
86
ScopedApi < JsonElement > element ,
90
87
SerializationFormat format )
91
88
{
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 ;
98
92
}
99
93
100
94
/// <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 )
102
96
{
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 ) ;
105
99
}
106
100
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 )
108
102
{
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 ;
115
106
}
116
107
}
117
108
}
0 commit comments