@@ -14,29 +14,113 @@ public class OneOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization
1414 options = options ?? throw new global ::System . ArgumentNullException ( nameof ( options ) ) ;
1515 var typeInfoResolver = options . TypeInfoResolver ?? throw new global ::System . InvalidOperationException ( "TypeInfoResolver is not set." ) ;
1616
17- var
18- readerCopy = reader ;
19- T1 ? value1 = default ;
20- try
17+
18+ using var __jsonDocument = global ::System . Text . Json . JsonDocument . ParseValue ( ref reader ) ;
19+ var __rawJson = __jsonDocument . RootElement . GetRawText ( ) ;
20+ var __jsonProps = new global ::System . Collections . Generic . HashSet < string > ( ) ;
21+ if ( __jsonDocument . RootElement . ValueKind == global ::System . Text . Json . JsonValueKind . Object )
2122 {
22- var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T1 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T1 > ??
23- throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T1 ) . Name } ") ;
24- value1 = global ::System . Text . Json . JsonSerializer . Deserialize ( ref readerCopy , typeInfo ) ;
23+ foreach ( var __jsonProp in __jsonDocument . RootElement . EnumerateObject ( ) )
24+ {
25+ __jsonProps . Add ( __jsonProp . Name ) ;
26+ }
27+ }
28+
29+ var __score0 = 0 ;
30+ {
31+ var __ti = typeInfoResolver . GetTypeInfo ( typeof ( T1 ) , options ) ;
32+ if ( __ti != null && __ti . Kind == global ::System . Text . Json . Serialization . Metadata . JsonTypeInfoKind . Object )
33+ {
34+ foreach ( var __prop in __ti . Properties )
35+ {
36+ if ( __jsonProps . Contains ( __prop . Name ) ) __score0 ++ ;
37+ }
38+ }
2539 }
26- catch ( global :: System . Text . Json . JsonException )
40+ var __score1 = 0 ;
2741 {
42+ var __ti = typeInfoResolver . GetTypeInfo ( typeof ( T2 ) , options ) ;
43+ if ( __ti != null && __ti . Kind == global ::System . Text . Json . Serialization . Metadata . JsonTypeInfoKind . Object )
44+ {
45+ foreach ( var __prop in __ti . Properties )
46+ {
47+ if ( __jsonProps . Contains ( __prop . Name ) ) __score1 ++ ;
48+ }
49+ }
2850 }
51+ var __bestScore = 0 ;
52+ var __bestIndex = - 1 ;
53+ if ( __score0 > __bestScore ) { __bestScore = __score0 ; __bestIndex = 0 ; }
54+ if ( __score1 > __bestScore ) { __bestScore = __score1 ; __bestIndex = 1 ; }
2955
30- readerCopy = reader ;
56+ T1 ? value1 = default ;
3157 T2 ? value2 = default ;
32- try
58+ if ( __bestIndex >= 0 )
3359 {
34- var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T2 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T2 > ??
35- throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T2 ) . Name } ") ;
36- value2 = global ::System . Text . Json . JsonSerializer . Deserialize ( ref readerCopy , typeInfo ) ;
60+ if ( __bestIndex == 0 )
61+ {
62+ try
63+ {
64+
65+ var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T1 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T1 > ??
66+ throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T1 ) . Name } ") ;
67+ value1 = global ::System . Text . Json . JsonSerializer . Deserialize ( __rawJson , typeInfo ) ;
68+ }
69+ catch ( global ::System . Text . Json . JsonException )
70+ {
71+ }
72+ catch ( global ::System . InvalidOperationException )
73+ {
74+ }
75+ }
76+
77+ else if ( __bestIndex == 1 )
78+ {
79+ try
80+ {
81+
82+ var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T2 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T2 > ??
83+ throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T2 ) . Name } ") ;
84+ value2 = global ::System . Text . Json . JsonSerializer . Deserialize ( __rawJson , typeInfo ) ;
85+ }
86+ catch ( global ::System . Text . Json . JsonException )
87+ {
88+ }
89+ catch ( global ::System . InvalidOperationException )
90+ {
91+ }
92+ }
3793 }
38- catch ( global ::System . Text . Json . JsonException )
94+
95+ if ( value1 == null && value2 == null )
3996 {
97+ try
98+ {
99+
100+ var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T1 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T1 > ??
101+ throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T1 ) . Name } ") ;
102+ value1 = global ::System . Text . Json . JsonSerializer . Deserialize ( __rawJson , typeInfo ) ;
103+ }
104+ catch ( global ::System . Text . Json . JsonException )
105+ {
106+ }
107+ catch ( global ::System . InvalidOperationException )
108+ {
109+ }
110+
111+ try
112+ {
113+
114+ var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T2 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T2 > ??
115+ throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T2 ) . Name } ") ;
116+ value2 = global ::System . Text . Json . JsonSerializer . Deserialize ( __rawJson , typeInfo ) ;
117+ }
118+ catch ( global ::System . Text . Json . JsonException )
119+ {
120+ }
121+ catch ( global ::System . InvalidOperationException )
122+ {
123+ }
40124 }
41125
42126 var __value = new global ::Reka . OneOf < T1 , T2 > (
@@ -45,19 +129,6 @@ public class OneOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization
45129 value2
46130 ) ;
47131
48- if ( value1 != null )
49- {
50- var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T1 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T1 > ??
51- throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T1 ) . Name } ") ;
52- _ = global ::System . Text . Json . JsonSerializer . Deserialize ( ref reader , typeInfo ) ;
53- }
54- else if ( value2 != null )
55- {
56- var typeInfo = typeInfoResolver . GetTypeInfo ( typeof ( T2 ) , options ) as global ::System . Text . Json . Serialization . Metadata . JsonTypeInfo < T2 > ??
57- throw new global ::System . InvalidOperationException ( $ "Cannot get type info for { typeof ( T2 ) . Name } ") ;
58- _ = global ::System . Text . Json . JsonSerializer . Deserialize ( ref reader , typeInfo ) ;
59- }
60-
61132 return __value ;
62133 }
63134
0 commit comments