Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dotNET Platform
runtime
提交
42accd52
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,发现更多精彩内容 >>
未验证
提交
42accd52
编写于
4月 01, 2022
作者:
K
Krzysztof Wicher
提交者:
GitHub
4月 01, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clean up JsonTypeInfo constructor (#67430)
上级
07e87bc4
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
129 addition
and
132 deletion
+129
-132
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
...c/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
+129
-132
未找到文件。
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
浏览文件 @
42accd52
...
...
@@ -165,152 +165,24 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions
{
Type
=
type
;
Options
=
options
;
JsonNumberHandling
?
typeNumberHandling
=
GetNumberHandlingForType
(
Type
);
PropertyInfoForTypeInfo
=
CreatePropertyInfoForTypeInfo
(
Type
,
converter
,
typeNumberHandling
,
Options
);
NumberHandling
=
GetNumberHandlingForType
(
Type
);
PropertyInfoForTypeInfo
=
CreatePropertyInfoForTypeInfo
(
Type
,
converter
,
NumberHandling
,
Options
);
ElementType
=
converter
.
ElementType
;
switch
(
PropertyInfoForTypeInfo
.
ConverterStrategy
)
{
case
ConverterStrategy
.
Object
:
{
const
BindingFlags
bindingFlags
=
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
|
BindingFlags
.
DeclaredOnly
;
CreateObject
=
Options
.
MemberAccessorStrategy
.
CreateConstructor
(
type
);
Dictionary
<
string
,
JsonPropertyInfo
>?
ignoredMembers
=
null
;
PropertyInfo
[]
properties
=
type
.
GetProperties
(
bindingFlags
);
bool
propertyOrderSpecified
=
false
;
// PropertyCache is not accessed by other threads until the current JsonTypeInfo instance
// is finished initializing and added to the cache on JsonSerializerOptions.
// Default 'capacity' to the common non-polymorphic + property case.
PropertyCache
=
new
JsonPropertyDictionary
<
JsonPropertyInfo
>(
Options
.
PropertyNameCaseInsensitive
,
capacity
:
properties
.
Length
);
// We start from the most derived type.
Type
?
currentType
=
type
;
while
(
true
)
{
foreach
(
PropertyInfo
propertyInfo
in
properties
)
{
bool
isVirtual
=
propertyInfo
.
IsVirtual
();
string
propertyName
=
propertyInfo
.
Name
;
// Ignore indexers and virtual properties that have overrides that were [JsonIgnore]d.
if
(
propertyInfo
.
GetIndexParameters
().
Length
>
0
||
PropertyIsOverridenAndIgnored
(
propertyName
,
propertyInfo
.
PropertyType
,
isVirtual
,
ignoredMembers
))
{
continue
;
}
// For now we only support public properties (i.e. setter and/or getter is public).
if
(
propertyInfo
.
GetMethod
?.
IsPublic
==
true
||
propertyInfo
.
SetMethod
?.
IsPublic
==
true
)
{
CacheMember
(
currentType
,
propertyInfo
.
PropertyType
,
propertyInfo
,
isVirtual
,
typeNumberHandling
,
ref
propertyOrderSpecified
,
ref
ignoredMembers
);
}
else
{
if
(
JsonPropertyInfo
.
GetAttribute
<
JsonIncludeAttribute
>(
propertyInfo
)
!=
null
)
{
ThrowHelper
.
ThrowInvalidOperationException_JsonIncludeOnNonPublicInvalid
(
propertyName
,
currentType
);
}
// Non-public properties should not be included for (de)serialization.
}
}
foreach
(
FieldInfo
fieldInfo
in
currentType
.
GetFields
(
bindingFlags
))
{
string
fieldName
=
fieldInfo
.
Name
;
if
(
PropertyIsOverridenAndIgnored
(
fieldName
,
fieldInfo
.
FieldType
,
currentMemberIsVirtual
:
false
,
ignoredMembers
))
{
continue
;
}
bool
hasJsonInclude
=
JsonPropertyInfo
.
GetAttribute
<
JsonIncludeAttribute
>(
fieldInfo
)
!=
null
;
if
(
fieldInfo
.
IsPublic
)
{
if
(
hasJsonInclude
||
Options
.
IncludeFields
)
{
CacheMember
(
currentType
,
fieldInfo
.
FieldType
,
fieldInfo
,
isVirtual
:
false
,
typeNumberHandling
,
ref
propertyOrderSpecified
,
ref
ignoredMembers
);
}
}
else
{
if
(
hasJsonInclude
)
{
ThrowHelper
.
ThrowInvalidOperationException_JsonIncludeOnNonPublicInvalid
(
fieldName
,
currentType
);
}
// Non-public fields should not be included for (de)serialization.
}
}
currentType
=
currentType
.
BaseType
;
if
(
currentType
==
null
)
{
break
;
}
properties
=
currentType
.
GetProperties
(
bindingFlags
);
};
if
(
propertyOrderSpecified
)
{
PropertyCache
.
List
.
Sort
((
p1
,
p2
)
=>
p1
.
Value
!.
Order
.
CompareTo
(
p2
.
Value
!.
Order
));
}
if
(
converter
.
ConstructorIsParameterized
)
{
ParameterInfo
[]
parameters
=
converter
.
ConstructorInfo
!.
GetParameters
();
int
parameterCount
=
parameters
.
Length
;
JsonParameterInfoValues
[]
jsonParameters
=
GetParameterInfoArray
(
parameters
);
InitializeConstructorParameters
(
jsonParameters
);
}
}
break
;
case
ConverterStrategy
.
Enumerable
:
{
CreateObject
=
Options
.
MemberAccessorStrategy
.
CreateConstructor
(
type
);
AddPropertiesAndParametersUsingReflection
();
}
break
;
case
ConverterStrategy
.
Dictionary
:
{
KeyType
=
converter
.
KeyType
;
CreateObject
=
Options
.
MemberAccessorStrategy
.
CreateConstructor
(
type
);
}
break
;
case
ConverterStrategy
.
Enumerable
:
case
ConverterStrategy
.
Value
:
{
CreateObject
=
Options
.
MemberAccessorStrategy
.
CreateConstructor
(
type
);
}
break
;
case
ConverterStrategy
.
None
:
{
...
...
@@ -322,12 +194,137 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions
throw
new
InvalidOperationException
();
}
CreateObject
=
Options
.
MemberAccessorStrategy
.
CreateConstructor
(
type
);
// These two method overrides are expected to perform
// orthogonal changes, so we can invoke them both safely.
converter
.
ConfigureJsonTypeInfo
(
this
,
options
);
converter
.
ConfigureJsonTypeInfoUsingReflection
(
this
,
options
);
}
[
RequiresUnreferencedCode
(
JsonSerializer
.
SerializationUnreferencedCodeMessage
)]
private
void
AddPropertiesAndParametersUsingReflection
()
{
Debug
.
Assert
(
PropertyInfoForTypeInfo
.
ConverterStrategy
==
ConverterStrategy
.
Object
);
const
BindingFlags
bindingFlags
=
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
|
BindingFlags
.
DeclaredOnly
;
Dictionary
<
string
,
JsonPropertyInfo
>?
ignoredMembers
=
null
;
PropertyInfo
[]
properties
=
Type
.
GetProperties
(
bindingFlags
);
bool
propertyOrderSpecified
=
false
;
// PropertyCache is not accessed by other threads until the current JsonTypeInfo instance
// is finished initializing and added to the cache on JsonSerializerOptions.
// Default 'capacity' to the common non-polymorphic + property case.
PropertyCache
=
new
JsonPropertyDictionary
<
JsonPropertyInfo
>(
Options
.
PropertyNameCaseInsensitive
,
capacity
:
properties
.
Length
);
// We start from the most derived type.
Type
?
currentType
=
Type
;
while
(
true
)
{
foreach
(
PropertyInfo
propertyInfo
in
properties
)
{
bool
isVirtual
=
propertyInfo
.
IsVirtual
();
string
propertyName
=
propertyInfo
.
Name
;
// Ignore indexers and virtual properties that have overrides that were [JsonIgnore]d.
if
(
propertyInfo
.
GetIndexParameters
().
Length
>
0
||
PropertyIsOverridenAndIgnored
(
propertyName
,
propertyInfo
.
PropertyType
,
isVirtual
,
ignoredMembers
))
{
continue
;
}
// For now we only support public properties (i.e. setter and/or getter is public).
if
(
propertyInfo
.
GetMethod
?.
IsPublic
==
true
||
propertyInfo
.
SetMethod
?.
IsPublic
==
true
)
{
CacheMember
(
currentType
,
propertyInfo
.
PropertyType
,
propertyInfo
,
isVirtual
,
NumberHandling
,
ref
propertyOrderSpecified
,
ref
ignoredMembers
);
}
else
{
if
(
JsonPropertyInfo
.
GetAttribute
<
JsonIncludeAttribute
>(
propertyInfo
)
!=
null
)
{
ThrowHelper
.
ThrowInvalidOperationException_JsonIncludeOnNonPublicInvalid
(
propertyName
,
currentType
);
}
// Non-public properties should not be included for (de)serialization.
}
}
foreach
(
FieldInfo
fieldInfo
in
currentType
.
GetFields
(
bindingFlags
))
{
string
fieldName
=
fieldInfo
.
Name
;
if
(
PropertyIsOverridenAndIgnored
(
fieldName
,
fieldInfo
.
FieldType
,
currentMemberIsVirtual
:
false
,
ignoredMembers
))
{
continue
;
}
bool
hasJsonInclude
=
JsonPropertyInfo
.
GetAttribute
<
JsonIncludeAttribute
>(
fieldInfo
)
!=
null
;
if
(
fieldInfo
.
IsPublic
)
{
if
(
hasJsonInclude
||
Options
.
IncludeFields
)
{
CacheMember
(
currentType
,
fieldInfo
.
FieldType
,
fieldInfo
,
isVirtual
:
false
,
NumberHandling
,
ref
propertyOrderSpecified
,
ref
ignoredMembers
);
}
}
else
{
if
(
hasJsonInclude
)
{
ThrowHelper
.
ThrowInvalidOperationException_JsonIncludeOnNonPublicInvalid
(
fieldName
,
currentType
);
}
// Non-public fields should not be included for (de)serialization.
}
}
currentType
=
currentType
.
BaseType
;
if
(
currentType
==
null
)
{
break
;
}
properties
=
currentType
.
GetProperties
(
bindingFlags
);
};
if
(
propertyOrderSpecified
)
{
PropertyCache
.
List
.
Sort
((
p1
,
p2
)
=>
p1
.
Value
!.
Order
.
CompareTo
(
p2
.
Value
!.
Order
));
}
JsonConverter
converter
=
PropertyInfoForTypeInfo
.
ConverterBase
;
if
(
converter
.
ConstructorIsParameterized
)
{
ParameterInfo
[]
parameters
=
converter
.
ConstructorInfo
!.
GetParameters
();
int
parameterCount
=
parameters
.
Length
;
JsonParameterInfoValues
[]
jsonParameters
=
GetParameterInfoArray
(
parameters
);
InitializeConstructorParameters
(
jsonParameters
);
}
}
private
void
CacheMember
(
Type
declaringType
,
Type
memberType
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录