Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dotNET Platform
runtime
提交
6f40e480
R
runtime
项目概览
dotNET Platform
/
runtime
12 个月 前同步成功
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
runtime
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
6f40e480
编写于
12月 10, 2021
作者:
E
Eirik Tsarpalis
提交者:
GitHub
12月 10, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix custom JsonConverterFactories not working with transitive type/property declarations. (#62595)
上级
25c20735
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
90 addition
and
50 deletion
+90
-50
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
...aries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
+12
-12
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs
...System.Text.Json.SourceGeneration.Tests/ContextClasses.cs
+2
-2
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs
...Json.SourceGeneration.Tests/JsonSerializerContextTests.cs
+40
-0
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs
...eGeneration.Tests/MetadataAndSerializationContextTests.cs
+4
-4
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs
....Text.Json.SourceGeneration.Tests/MetadataContextTests.cs
+8
-8
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs
...Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs
+4
-4
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs
...Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs
+10
-10
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs
....Json.SourceGeneration.Tests/SerializationContextTests.cs
+8
-8
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.CustomConverters.cs
...on.SourceGeneration.Tests/TestClasses.CustomConverters.cs
+2
-2
未找到文件。
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
浏览文件 @
6f40e480
...
...
@@ -96,6 +96,9 @@ private sealed partial class Emitter
private
readonly
HashSet
<
string
>
_emittedPropertyFileNames
=
new
();
private
bool
_generateGetConverterMethodForTypes
=
false
;
private
bool
_generateGetConverterMethodForProperties
=
false
;
public
Emitter
(
in
JsonSourceGenerationContext
sourceGenerationContext
,
SourceGenerationSpec
generationSpec
)
{
_sourceGenerationContext
=
sourceGenerationContext
;
...
...
@@ -107,16 +110,12 @@ public void Emit()
foreach
(
ContextGenerationSpec
contextGenerationSpec
in
_generationSpec
.
ContextGenerationSpecList
)
{
_currentContext
=
contextGenerationSpec
;
bool
generateGetConverterMethodForTypes
=
false
;
bool
generateGetConverterMethodForProperties
=
false
;
_generateGetConverterMethodForTypes
=
false
;
_generateGetConverterMethodForProperties
=
false
;
foreach
(
TypeGenerationSpec
typeGenerationSpec
in
_currentContext
.
RootSerializableTypes
)
{
GenerateTypeInfo
(
typeGenerationSpec
);
generateGetConverterMethodForTypes
|=
typeGenerationSpec
.
HasTypeFactoryConverter
;
generateGetConverterMethodForProperties
|=
typeGenerationSpec
.
HasPropertyFactoryConverters
;
}
string
contextName
=
_currentContext
.
ContextType
.
Name
;
...
...
@@ -124,7 +123,7 @@ public void Emit()
// Add root context implementation.
AddSource
(
$"
{
contextName
}
.g.cs"
,
GetRootJsonContextImplementation
(
generateGetConverterMethodForTypes
,
generateGetConverterMethodForProperties
),
GetRootJsonContextImplementation
(),
isRootContextDef
:
true
);
// Add GetJsonTypeInfo override implementation.
...
...
@@ -297,6 +296,9 @@ private void GenerateTypeInfo(TypeGenerationSpec typeGenerationSpec)
Location
location
=
typeGenerationSpec
.
AttributeLocation
??
_currentContext
.
Location
;
_sourceGenerationContext
.
ReportDiagnostic
(
Diagnostic
.
Create
(
DuplicateTypeName
,
location
,
new
string
[]
{
typeGenerationSpec
.
TypeInfoPropertyName
}));
}
_generateGetConverterMethodForTypes
|=
typeGenerationSpec
.
HasTypeFactoryConverter
;
_generateGetConverterMethodForProperties
|=
typeGenerationSpec
.
HasPropertyFactoryConverters
;
}
private string GenerateForTypeWithKnownConverter(TypeGenerationSpec typeMetadata)
...
...
@@ -1140,9 +1142,7 @@ private string WrapWithCheckForCustomConverter(string source, string typeCompila
{
IndentSource
(
source
,
numIndentations
:
1
)}
}}
";
private
string
GetRootJsonContextImplementation
(
bool
generateGetConverterMethodForTypes
,
bool
generateGetConverterMethodForProperties
)
private
string
GetRootJsonContextImplementation
()
{
string
contextTypeRef
=
_currentContext
.
ContextTypeRef
;
string
contextTypeName
=
_currentContext
.
ContextType
.
Name
;
...
...
@@ -1166,12 +1166,12 @@ private string WrapWithCheckForCustomConverter(string source, string typeCompila
{
GetFetchLogicForRuntimeSpecifiedCustomConverter
()}
");
if
(
generateGetConverterMethodForProperties
)
if
(
_
generateGetConverterMethodForProperties
)
{
sb
.
Append
(
GetFetchLogicForGetCustomConverter_PropertiesWithFactories
());
}
if
(
generateGetConverterMethodForProperties
||
generateGetConverterMethodForTypes
)
if
(
_generateGetConverterMethodForProperties
||
_
generateGetConverterMethodForTypes
)
{
sb
.
Append
(
GetFetchLogicForGetCustomConverter_TypesWithFactories
());
}
...
...
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs
浏览文件 @
6f40e480
...
...
@@ -42,8 +42,8 @@ public interface ITestContext
public
JsonTypeInfo
<
StructWithCustomConverterFactory
>
StructWithCustomConverterFactory
{
get
;
}
public
JsonTypeInfo
<
ClassWithCustomConverterProperty
>
ClassWithCustomConverterProperty
{
get
;
}
public
JsonTypeInfo
<
StructWithCustomConverterProperty
>
StructWithCustomConverterProperty
{
get
;
}
public
JsonTypeInfo
<
ClassWithCustomConverter
PropertyFactory
>
ClassWithCustomConverterPropertyFactor
y
{
get
;
}
public
JsonTypeInfo
<
StructWithCustomConverter
PropertyFactory
>
StructWithCustomConverterPropertyFactor
y
{
get
;
}
public
JsonTypeInfo
<
ClassWithCustomConverter
FactoryProperty
>
ClassWithCustomConverterFactoryPropert
y
{
get
;
}
public
JsonTypeInfo
<
StructWithCustomConverter
FactoryProperty
>
StructWithCustomConverterFactoryPropert
y
{
get
;
}
public
JsonTypeInfo
<
ClassWithBadCustomConverter
>
ClassWithBadCustomConverter
{
get
;
}
public
JsonTypeInfo
<
StructWithBadCustomConverter
>
StructWithBadCustomConverter
{
get
;
}
public
JsonTypeInfo
<
PersonStruct
?>
NullablePersonStruct
{
get
;
}
...
...
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs
浏览文件 @
6f40e480
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using
System.Collections.Generic
;
using
System.Reflection
;
using
System.Text.Json.Serialization
;
using
Microsoft.DotNet.RemoteExecutor
;
...
...
@@ -89,5 +90,44 @@ protected internal partial class NestedProtectedInternalClass : JsonSerializerCo
internal
partial
class
PersonJsonContext
:
JsonSerializerContext
{
}
// Regression test for https://github.com/dotnet/runtime/issues/62079
[
Fact
]
public
static
void
SupportsPropertiesWithCustomConverterFactory
()
{
var
value
=
new
ClassWithCustomConverterFactoryProperty
{
MyEnum
=
Serialization
.
Tests
.
SampleEnum
.
MinZero
};
string
json
=
JsonSerializer
.
Serialize
(
value
,
SingleClassWithCustomConverterFactoryPropertyContext
.
Default
.
ClassWithCustomConverterFactoryProperty
);
Assert
.
Equal
(
@"{""MyEnum"":""MinZero""}"
,
json
);
}
public
class
ParentClass
{
public
ClassWithCustomConverterFactoryProperty
?
Child
{
get
;
set
;
}
}
[
JsonSerializable
(
typeof
(
ParentClass
))]
internal
partial
class
SingleClassWithCustomConverterFactoryPropertyContext
:
JsonSerializerContext
{
}
// Regression test for https://github.com/dotnet/runtime/issues/61860
[
Fact
]
public
static
void
SupportsGenericParameterWithCustomConverterFactory
()
{
var
value
=
new
List
<
TestEnum
>
{
TestEnum
.
Cee
};
string
json
=
JsonSerializer
.
Serialize
(
value
,
GenericParameterWithCustomConverterFactoryContext
.
Default
.
ListTestEnum
);
Assert
.
Equal
(
@"[""Cee""]"
,
json
);
}
[
JsonConverter
(
typeof
(
JsonStringEnumConverter
))]
public
enum
TestEnum
{
Aye
,
Bee
,
Cee
}
[
JsonSerializable
(
typeof
(
List
<
TestEnum
>))]
internal
partial
class
GenericParameterWithCustomConverterFactoryContext
:
JsonSerializerContext
{
}
}
}
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs
浏览文件 @
6f40e480
...
...
@@ -36,8 +36,8 @@ namespace System.Text.Json.SourceGeneration.Tests
[
JsonSerializable
(
typeof
(
StructWithCustomConverterFactory
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverterProperty
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverterProperty
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
PropertyFactor
y
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
PropertyFactor
y
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
FactoryPropert
y
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
FactoryPropert
y
))]
[
JsonSerializable
(
typeof
(
ClassWithBadCustomConverter
))]
[
JsonSerializable
(
typeof
(
StructWithBadCustomConverter
))]
[
JsonSerializable
(
typeof
(
PersonStruct
?))]
...
...
@@ -82,8 +82,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert
.
NotNull
(
MetadataAndSerializationContext
.
Default
.
StructWithCustomConverterFactory
);
Assert
.
NotNull
(
MetadataAndSerializationContext
.
Default
.
ClassWithCustomConverterProperty
);
Assert
.
NotNull
(
MetadataAndSerializationContext
.
Default
.
StructWithCustomConverterProperty
);
Assert
.
NotNull
(
MetadataAndSerializationContext
.
Default
.
ClassWithCustomConverter
PropertyFactor
y
);
Assert
.
NotNull
(
MetadataAndSerializationContext
.
Default
.
StructWithCustomConverter
PropertyFactor
y
);
Assert
.
NotNull
(
MetadataAndSerializationContext
.
Default
.
ClassWithCustomConverter
FactoryPropert
y
);
Assert
.
NotNull
(
MetadataAndSerializationContext
.
Default
.
StructWithCustomConverter
FactoryPropert
y
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MetadataAndSerializationContext
.
Default
.
ClassWithBadCustomConverter
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MetadataAndSerializationContext
.
Default
.
StructWithBadCustomConverter
);
Assert
.
Null
(
MetadataAndSerializationContext
.
Default
.
NullablePersonStruct
.
SerializeHandler
);
...
...
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs
浏览文件 @
6f40e480
...
...
@@ -35,8 +35,8 @@ namespace System.Text.Json.SourceGeneration.Tests
[
JsonSerializable
(
typeof
(
StructWithCustomConverterFactory
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
ClassWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
StructWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
[
JsonSerializable
(
typeof
(
PersonStruct
?),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
)]
...
...
@@ -80,8 +80,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverterFactory
.
SerializeHandler
);
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
ClassWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
ClassWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
ClassWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MetadataWithPerTypeAttributeContext
.
Default
.
ClassWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MetadataWithPerTypeAttributeContext
.
Default
.
StructWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Null
(
MetadataWithPerTypeAttributeContext
.
Default
.
NullablePersonStruct
.
SerializeHandler
);
...
...
@@ -119,8 +119,8 @@ public override void EnsureFastPathGeneratedAsExpected()
[
JsonSerializable
(
typeof
(
StructWithCustomConverterFactory
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverterProperty
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverterProperty
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
PropertyFactor
y
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
PropertyFactor
y
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
FactoryPropert
y
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
FactoryPropert
y
))]
[
JsonSerializable
(
typeof
(
ClassWithBadCustomConverter
))]
[
JsonSerializable
(
typeof
(
StructWithBadCustomConverter
))]
[
JsonSerializable
(
typeof
(
PersonStruct
?))]
...
...
@@ -187,8 +187,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert
.
Null
(
MetadataContext
.
Default
.
StructWithCustomConverterFactory
.
SerializeHandler
);
Assert
.
Null
(
MetadataContext
.
Default
.
ClassWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
MetadataContext
.
Default
.
StructWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
MetadataContext
.
Default
.
ClassWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
MetadataContext
.
Default
.
StructWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
MetadataContext
.
Default
.
ClassWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Null
(
MetadataContext
.
Default
.
StructWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MetadataContext
.
Default
.
ClassWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MetadataContext
.
Default
.
StructWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Null
(
MetadataContext
.
Default
.
NullablePersonStruct
.
SerializeHandler
);
...
...
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs
浏览文件 @
6f40e480
...
...
@@ -36,8 +36,8 @@ namespace System.Text.Json.SourceGeneration.Tests
[
JsonSerializable
(
typeof
(
StructWithCustomConverterFactory
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
PersonStruct
?),
GenerationMode
=
JsonSourceGenerationMode
.
Metadata
|
JsonSourceGenerationMode
.
Serialization
)]
...
...
@@ -82,8 +82,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert
.
Null
(
MixedModeContext
.
Default
.
StructWithCustomConverterFactory
.
SerializeHandler
);
Assert
.
Null
(
MixedModeContext
.
Default
.
ClassWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
MixedModeContext
.
Default
.
StructWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
MixedModeContext
.
Default
.
ClassWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
MixedModeContext
.
Default
.
StructWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
MixedModeContext
.
Default
.
ClassWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Null
(
MixedModeContext
.
Default
.
StructWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MixedModeContext
.
Default
.
ClassWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
MixedModeContext
.
Default
.
StructWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Null
(
MixedModeContext
.
Default
.
NullablePersonStruct
.
SerializeHandler
);
...
...
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs
浏览文件 @
6f40e480
...
...
@@ -246,28 +246,28 @@ public virtual void RoundTripWithCustomPropertyConverterFactory_Class()
{
const
string
Json
=
"{\"MyEnum\":\"One\"}"
;
ClassWithCustomConverter
PropertyFactor
y
obj
=
new
()
ClassWithCustomConverter
FactoryPropert
y
obj
=
new
()
{
MyEnum
=
SampleEnum
.
One
};
if
(
DefaultContext
.
JsonSourceGenerationMode
==
JsonSourceGenerationMode
.
Serialization
)
{
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
ClassWithCustomConverter
PropertyFactor
y
));
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
ClassWithCustomConverter
FactoryPropert
y
));
}
else
{
string
json
=
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
ClassWithCustomConverter
PropertyFactor
y
);
string
json
=
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
ClassWithCustomConverter
FactoryPropert
y
);
Assert
.
Equal
(
Json
,
json
);
}
if
(
DefaultContext
.
JsonSourceGenerationMode
==
JsonSourceGenerationMode
.
Serialization
)
{
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
ClassWithCustomConverter
PropertyFactor
y
));
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
ClassWithCustomConverter
FactoryPropert
y
));
}
else
{
obj
=
JsonSerializer
.
Deserialize
(
Json
,
DefaultContext
.
ClassWithCustomConverter
PropertyFactor
y
);
obj
=
JsonSerializer
.
Deserialize
(
Json
,
DefaultContext
.
ClassWithCustomConverter
FactoryPropert
y
);
Assert
.
Equal
(
SampleEnum
.
One
,
obj
.
MyEnum
);
}
}
...
...
@@ -277,28 +277,28 @@ public virtual void RoundTripWithCustomPropertyConverterFactory_Struct()
{
const
string
Json
=
"{\"MyEnum\":\"One\"}"
;
StructWithCustomConverter
PropertyFactor
y
obj
=
new
()
StructWithCustomConverter
FactoryPropert
y
obj
=
new
()
{
MyEnum
=
SampleEnum
.
One
};
if
(
DefaultContext
.
JsonSourceGenerationMode
==
JsonSourceGenerationMode
.
Serialization
)
{
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
StructWithCustomConverter
PropertyFactor
y
));
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
StructWithCustomConverter
FactoryPropert
y
));
}
else
{
string
json
=
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
StructWithCustomConverter
PropertyFactor
y
);
string
json
=
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
StructWithCustomConverter
FactoryPropert
y
);
Assert
.
Equal
(
Json
,
json
);
}
if
(
DefaultContext
.
JsonSourceGenerationMode
==
JsonSourceGenerationMode
.
Serialization
)
{
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
StructWithCustomConverter
PropertyFactor
y
));
Assert
.
Throws
<
InvalidOperationException
>(()
=>
JsonSerializer
.
Serialize
(
obj
,
DefaultContext
.
StructWithCustomConverter
FactoryPropert
y
));
}
else
{
obj
=
JsonSerializer
.
Deserialize
(
Json
,
DefaultContext
.
StructWithCustomConverter
PropertyFactor
y
);
obj
=
JsonSerializer
.
Deserialize
(
Json
,
DefaultContext
.
StructWithCustomConverter
FactoryPropert
y
);
Assert
.
Equal
(
SampleEnum
.
One
,
obj
.
MyEnum
);
}
}
...
...
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs
浏览文件 @
6f40e480
...
...
@@ -36,8 +36,8 @@ namespace System.Text.Json.SourceGeneration.Tests
[
JsonSerializable
(
typeof
(
StructWithCustomConverterFactory
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverterProperty
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverterProperty
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
PropertyFactor
y
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
PropertyFactor
y
))]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
FactoryPropert
y
))]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
FactoryPropert
y
))]
[
JsonSerializable
(
typeof
(
ClassWithBadCustomConverter
))]
[
JsonSerializable
(
typeof
(
StructWithBadCustomConverter
))]
[
JsonSerializable
(
typeof
(
PersonStruct
?))]
...
...
@@ -75,8 +75,8 @@ internal partial class SerializationContext : JsonSerializerContext, ITestContex
[
JsonSerializable
(
typeof
(
StructWithCustomConverterFactory
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
PersonStruct
?),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
...
...
@@ -115,8 +115,8 @@ internal partial class SerializationWithPerTypeAttributeContext : JsonSerializer
[
JsonSerializable
(
typeof
(
StructWithCustomConverterFactory
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverterProperty
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
PropertyFactor
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithCustomConverter
FactoryPropert
y
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
ClassWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
StructWithBadCustomConverter
),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
[
JsonSerializable
(
typeof
(
PersonStruct
?),
GenerationMode
=
JsonSourceGenerationMode
.
Serialization
)]
...
...
@@ -486,8 +486,8 @@ public override void EnsureFastPathGeneratedAsExpected()
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverterFactory
.
SerializeHandler
);
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
ClassWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverterProperty
.
SerializeHandler
);
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
ClassWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverter
PropertyFactor
y
.
SerializeHandler
);
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
ClassWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
StructWithCustomConverter
FactoryPropert
y
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
SerializationWithPerTypeAttributeContext
.
Default
.
ClassWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Throws
<
InvalidOperationException
>(()
=>
SerializationWithPerTypeAttributeContext
.
Default
.
StructWithBadCustomConverter
.
SerializeHandler
);
Assert
.
Null
(
SerializationWithPerTypeAttributeContext
.
Default
.
NullablePersonStruct
.
SerializeHandler
);
...
...
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.CustomConverters.cs
浏览文件 @
6f40e480
...
...
@@ -245,13 +245,13 @@ public struct StructWithCustomConverterProperty
public
ClassWithCustomConverterProperty
.
NestedPoco
Property
{
get
;
set
;
}
}
public
struct
ClassWithCustomConverterPropertyFactor
y
public
class
ClassWithCustomConverterFactoryPropert
y
{
[
JsonConverter
(
typeof
(
JsonStringEnumConverter
))]
// This converter is a JsonConverterFactory
public
Serialization
.
Tests
.
SampleEnum
MyEnum
{
get
;
set
;
}
}
public
struct
StructWithCustomConverter
PropertyFactor
y
public
struct
StructWithCustomConverter
FactoryPropert
y
{
[
JsonConverter
(
typeof
(
JsonStringEnumConverter
))]
// This converter is a JsonConverterFactory
public
Serialization
.
Tests
.
SampleEnum
MyEnum
{
get
;
set
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录