Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonDataContract.cs
Go to the documentation of this file.
3using System.Xml;
4
6
7internal class JsonDataContract
8{
10 {
11 private static readonly object s_cacheLock = new object();
12
13 private static readonly object s_createDataContractLock = new object();
14
16
17 private static int s_dataContractID;
18
19 private static readonly TypeHandleRef s_typeHandleRef = new TypeHandleRef();
20
22
24
26
27 private readonly string _typeName;
28
30
32
33 internal virtual string TypeName => _typeName;
34
35 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
42
43 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
55
56 internal static int GetId(RuntimeTypeHandle typeHandle)
57 {
59 {
61 if (!s_typeToIDCache.TryGetValue(s_typeHandleRef, out var value))
62 {
63 int num = s_dataContractID++;
64 if (num >= s_dataContractCache.Length)
65 {
66 int num2 = ((num < 1073741823) ? (num * 2) : int.MaxValue);
67 if (num2 <= num)
68 {
70 }
72 }
73 value = new IntRef(num);
74 try
75 {
77 }
78 catch (Exception ex)
79 {
81 }
82 }
83 return value.Value;
84 }
85 }
86
87 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
89 {
91 {
93 if (jsonDataContract == null)
94 {
97 {
99 }
100 else if (type == typeof(StringDataContract))
101 {
103 }
104 else if (type == typeof(UriDataContract))
105 {
107 }
108 else if (type == typeof(QNameDataContract))
109 {
111 }
112 else if (type == typeof(ByteArrayDataContract))
113 {
115 }
116 else if (traditionalDataContract.IsPrimitive || traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName)
117 {
119 }
120 else if (type == typeof(ClassDataContract))
121 {
123 }
124 else if (type == typeof(EnumDataContract))
125 {
127 }
129 {
131 }
133 {
135 }
136 else
137 {
138 if (!(type == typeof(XmlDataContract)))
139 {
141 }
143 }
144 }
145 return jsonDataContract;
146 }
147 }
148
149 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
180 }
181
183
184 internal virtual string TypeName => null;
185
187
189
191
192 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
197
199 {
200 _helper = helper;
201 }
202
204 {
205 if (!JsonReadWriteDelegates.GetJsonDelegates().TryGetValue(c, out var value))
206 {
207 return null;
208 }
209 return value;
210 }
211
221
226
227 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
232
233 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
235 {
236 PushKnownDataContracts(context);
237 object result = ReadJsonValueCore(jsonReader, context);
238 PopKnownDataContracts(context);
239 return result;
240 }
241
242 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
247
248 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
255
256 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
261
262 protected static object HandleReadValue(object obj, XmlObjectSerializerReadContext context)
263 {
264 context.AddNewObject(obj);
265 return obj;
266 }
267
268 protected static bool TryReadNullAtTopLevel(XmlReaderDelegator reader)
269 {
270 if (reader.MoveToAttribute("type") && reader.Value == "null")
271 {
272 reader.Skip();
273 reader.MoveToElement();
274 return true;
275 }
276 reader.MoveToElement();
277 return false;
278 }
279
281 {
282 if (KnownDataContracts != null)
283 {
284 context.scopedKnownTypes.Pop();
285 }
286 }
287
289 {
290 if (KnownDataContracts != null)
291 {
293 }
294 }
295}
bool TryAdd(TKey key, TValue value)
virtual void WriteXmlValue(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
static DataContract GetDataContract(Type type)
virtual object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
virtual Dictionary< XmlQualifiedName, DataContract > KnownDataContracts
static Exception ThrowHelperFatal(string message, Exception innerException)
static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract)
static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract)
void PopKnownDataContracts(XmlObjectSerializerContext context)
static JsonReadWriteDelegates GetGeneratedReadWriteDelegates(DataContract c)
void WriteJsonValue(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle)
object ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
static JsonReadWriteDelegates TryGetReadWriteDelegatesFromGeneratedAssembly(DataContract c)
JsonDataContract(DataContract traditionalDataContract)
JsonDataContract(JsonDataContractCriticalHelper helper)
static JsonReadWriteDelegates GetReadWriteDelegatesFromGeneratedAssembly(DataContract c)
virtual void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle)
static bool TryReadNullAtTopLevel(XmlReaderDelegator reader)
static object HandleReadValue(object obj, XmlObjectSerializerReadContext context)
void PushKnownDataContracts(XmlObjectSerializerContext context)
Dictionary< XmlQualifiedName, DataContract > KnownDataContracts
virtual object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
readonly JsonDataContractCriticalHelper _helper
static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract)
static Dictionary< DataContract, JsonReadWriteDelegates > GetJsonDelegates()
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string DataContractCacheOverflow
Definition SR.cs:54
static string JsonTypeNotSupportedByDataContractJsonSerializer
Definition SR.cs:530
static string SerializationCodeIsMissingForType
Definition SR.cs:524
Definition SR.cs:7
void Push(Dictionary< XmlQualifiedName, DataContract > dataContractDictionary)