@@ -842,15 +842,15 @@ private List<MethodBodyStatement> BuildDeserializePropertiesStatements(ScopedApi
842842
843843 if ( _additionalBinaryDataProperty != null )
844844 {
845- var binaryDataDeserializationValue = ClientModelPlugin . Instance . TypeFactory . GetValueTypeDeserializationExpression (
845+ var binaryDataDeserializationValue = ClientModelPlugin . Instance . TypeFactory . DeserializeJsonValue (
846846 _additionalBinaryDataProperty . Type . ElementType . FrameworkType , jsonProperty . Value ( ) , SerializationFormat . Default ) ;
847847 propertyDeserializationStatements . Add (
848848 _additionalBinaryDataProperty . AsVariableExpression . AsDictionary ( _additionalBinaryDataProperty . Type ) . Add ( jsonProperty . Name ( ) , binaryDataDeserializationValue ) ) ;
849849 }
850850 else if ( rawBinaryData != null )
851851 {
852852 var elementType = rawBinaryData . Type . Arguments [ 1 ] . FrameworkType ;
853- var rawDataDeserializationValue = ClientModelPlugin . Instance . TypeFactory . GetValueTypeDeserializationExpression ( elementType , jsonProperty . Value ( ) , SerializationFormat . Default ) ;
853+ var rawDataDeserializationValue = ClientModelPlugin . Instance . TypeFactory . DeserializeJsonValue ( elementType , jsonProperty . Value ( ) , SerializationFormat . Default ) ;
854854 propertyDeserializationStatements . Add ( new IfStatement ( _isNotEqualToWireConditionSnippet )
855855 {
856856 rawBinaryData . AsVariableExpression . AsDictionary ( rawBinaryData . Type ) . Add ( jsonProperty . Name ( ) , rawDataDeserializationValue )
@@ -1267,11 +1267,11 @@ private ValueExpression CreateDeserializeValueExpression(CSharpType valueType, S
12671267 valueType switch
12681268 {
12691269 { IsFrameworkType : true } when valueType . FrameworkType == typeof ( Nullable < > ) =>
1270- ClientModelPlugin . Instance . TypeFactory . GetValueTypeDeserializationExpression ( valueType . Arguments [ 0 ] . FrameworkType , jsonElement , serializationFormat ) ,
1270+ ClientModelPlugin . Instance . TypeFactory . DeserializeJsonValue ( valueType . Arguments [ 0 ] . FrameworkType , jsonElement , serializationFormat ) ,
12711271 { IsFrameworkType : true } =>
1272- ClientModelPlugin . Instance . TypeFactory . GetValueTypeDeserializationExpression ( valueType . FrameworkType , jsonElement , serializationFormat ) ,
1272+ ClientModelPlugin . Instance . TypeFactory . DeserializeJsonValue ( valueType . FrameworkType , jsonElement , serializationFormat ) ,
12731273 { IsEnum : true } =>
1274- valueType . ToEnum ( ClientModelPlugin . Instance . TypeFactory . GetValueTypeDeserializationExpression ( valueType . UnderlyingEnumType ! , jsonElement , serializationFormat ) ) ,
1274+ valueType . ToEnum ( ClientModelPlugin . Instance . TypeFactory . DeserializeJsonValue ( valueType . UnderlyingEnumType ! , jsonElement , serializationFormat ) ) ,
12751275 _ => valueType . Deserialize ( jsonElement , _mrwOptionsParameterSnippet )
12761276 } ;
12771277
@@ -1584,48 +1584,43 @@ private MethodBodyStatement CreateValueSerializationStatement(
15841584 SerializationFormat serializationFormat ,
15851585 ValueExpression value )
15861586 {
1587+ // append the `.Value` if needed (when the type is nullable and a value type)
1588+ value = value . NullableStructValue ( type ) ;
1589+
1590+ // now we just need to focus on how we serialize a value
15871591 if ( type . IsFrameworkType )
1588- return ClientModelPlugin . Instance . TypeFactory . SerializeValueType ( type , serializationFormat , value , type . FrameworkType , _utf8JsonWriterSnippet , _mrwOptionsParameterSnippet ) ;
1592+ return ClientModelPlugin . Instance . TypeFactory . SerializeJsonValue ( type . FrameworkType , value , _utf8JsonWriterSnippet , _mrwOptionsParameterSnippet , serializationFormat ) ;
15891593
15901594 if ( ! type . IsEnum )
15911595 return _utf8JsonWriterSnippet . WriteObjectValue ( value . As ( type ) , options : _mrwOptionsParameterSnippet ) ;
15921596
1593- var enumerableSnippet = value . NullableStructValue ( type ) . As ( type ) ;
15941597 if ( type . IsStruct ) //is extensible
15951598 {
15961599 if ( type . UnderlyingEnumType . Equals ( typeof ( string ) ) )
1597- return _utf8JsonWriterSnippet . WriteStringValue ( enumerableSnippet . Invoke ( nameof ( ToString ) ) ) ;
1600+ return _utf8JsonWriterSnippet . WriteStringValue ( value . Invoke ( nameof ( ToString ) ) ) ;
15981601
1599- return _utf8JsonWriterSnippet . WriteNumberValue ( enumerableSnippet . Invoke ( $ "ToSerial{ type . UnderlyingEnumType . Name } ") ) ;
1602+ return _utf8JsonWriterSnippet . WriteNumberValue ( value . Invoke ( $ "ToSerial{ type . UnderlyingEnumType . Name } ") ) ;
16001603 }
16011604 else
16021605 {
16031606 if ( type . UnderlyingEnumType . Equals ( typeof ( int ) ) )
16041607 // when the fixed enum is implemented as int, we cast to the value
1605- return _utf8JsonWriterSnippet . WriteNumberValue ( enumerableSnippet . CastTo ( type . UnderlyingEnumType ) ) ;
1608+ return _utf8JsonWriterSnippet . WriteNumberValue ( value . CastTo ( type . UnderlyingEnumType ) ) ;
16061609
16071610 if ( type . UnderlyingEnumType . Equals ( typeof ( string ) ) )
1608- return _utf8JsonWriterSnippet . WriteStringValue ( enumerableSnippet . Invoke ( $ "ToSerial{ type . UnderlyingEnumType . Name } ") ) ;
1611+ return _utf8JsonWriterSnippet . WriteStringValue ( value . Invoke ( $ "ToSerial{ type . UnderlyingEnumType . Name } ") ) ;
16091612
1610- return _utf8JsonWriterSnippet . WriteNumberValue ( enumerableSnippet . Invoke ( $ "ToSerial{ type . UnderlyingEnumType . Name } ") ) ;
1613+ return _utf8JsonWriterSnippet . WriteNumberValue ( value . Invoke ( $ "ToSerial{ type . UnderlyingEnumType . Name } ") ) ;
16111614 }
16121615 }
16131616
1614- internal static MethodBodyStatement SerializeValueTypeCore (
1615- CSharpType type ,
1616- SerializationFormat serializationFormat ,
1617- ValueExpression value ,
1617+ internal static MethodBodyStatement SerializeJsonValueCore (
16181618 Type valueType ,
1619+ ValueExpression value ,
16191620 ScopedApi < Utf8JsonWriter > utf8JsonWriter ,
1620- ScopedApi < ModelReaderWriterOptions > mrwOptionsParameter )
1621+ ScopedApi < ModelReaderWriterOptions > mrwOptionsParameter ,
1622+ SerializationFormat serializationFormat )
16211623 {
1622- if ( valueType == typeof ( Nullable < > ) )
1623- {
1624- valueType = type . Arguments [ 0 ] . FrameworkType ;
1625- }
1626-
1627- value = value . NullableStructValue ( type ) ;
1628-
16291624 return valueType switch
16301625 {
16311626 var t when t == typeof ( JsonElement ) =>
@@ -1656,7 +1651,7 @@ var t when ValueTypeIsNumber(t) =>
16561651 } ;
16571652 }
16581653
1659- internal static ValueExpression GetValueTypeDeserializationExpressionCore (
1654+ internal static ValueExpression DeserializeJsonValueCore (
16601655 Type valueType ,
16611656 ScopedApi < JsonElement > element ,
16621657 SerializationFormat format )
0 commit comments