DataContractJsonSerializer.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Runtime.Serialization.Json
  5. {
  6. using System.Runtime.Serialization;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.IO;
  11. using System.Text;
  12. using System.Xml;
  13. using System.ServiceModel;
  14. using System.Collections;
  15. using DataContractDictionary = System.Collections.Generic.Dictionary<System.Xml.XmlQualifiedName, DataContract>;
  16. using System.Runtime.CompilerServices;
  17. [TypeForwardedFrom("System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  18. public sealed class DataContractJsonSerializer : XmlObjectSerializer
  19. {
  20. internal IList<Type> knownTypeList;
  21. internal DataContractDictionary knownDataContracts;
  22. EmitTypeInformation emitTypeInformation;
  23. IDataContractSurrogate dataContractSurrogate;
  24. bool ignoreExtensionDataObject;
  25. ReadOnlyCollection<Type> knownTypeCollection;
  26. int maxItemsInObjectGraph;
  27. DataContract rootContract; // post-surrogate
  28. XmlDictionaryString rootName;
  29. bool rootNameRequiresMapping;
  30. Type rootType;
  31. bool serializeReadOnlyTypes;
  32. DateTimeFormat dateTimeFormat;
  33. bool useSimpleDictionaryFormat;
  34. public DataContractJsonSerializer(Type type)
  35. : this(type, (IEnumerable<Type>)null)
  36. {
  37. }
  38. public DataContractJsonSerializer(Type type, string rootName)
  39. : this(type, rootName, null)
  40. {
  41. }
  42. public DataContractJsonSerializer(Type type, XmlDictionaryString rootName)
  43. : this(type, rootName, null)
  44. {
  45. }
  46. public DataContractJsonSerializer(Type type, IEnumerable<Type> knownTypes)
  47. : this(type, knownTypes, int.MaxValue, false, null, false)
  48. {
  49. }
  50. public DataContractJsonSerializer(Type type, string rootName, IEnumerable<Type> knownTypes)
  51. : this(type, rootName, knownTypes, int.MaxValue, false, null, false)
  52. {
  53. }
  54. public DataContractJsonSerializer(Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes)
  55. : this(type, rootName, knownTypes, int.MaxValue, false, null, false)
  56. {
  57. }
  58. public DataContractJsonSerializer(Type type,
  59. IEnumerable<Type> knownTypes,
  60. int maxItemsInObjectGraph,
  61. bool ignoreExtensionDataObject,
  62. IDataContractSurrogate dataContractSurrogate,
  63. bool alwaysEmitTypeInformation)
  64. {
  65. EmitTypeInformation emitTypeInformation = alwaysEmitTypeInformation ? EmitTypeInformation.Always : EmitTypeInformation.AsNeeded;
  66. Initialize(type, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, emitTypeInformation, false, null, false);
  67. }
  68. public DataContractJsonSerializer(Type type, string rootName,
  69. IEnumerable<Type> knownTypes,
  70. int maxItemsInObjectGraph,
  71. bool ignoreExtensionDataObject,
  72. IDataContractSurrogate dataContractSurrogate,
  73. bool alwaysEmitTypeInformation)
  74. {
  75. EmitTypeInformation emitTypeInformation = alwaysEmitTypeInformation ? EmitTypeInformation.Always : EmitTypeInformation.AsNeeded;
  76. XmlDictionary dictionary = new XmlDictionary(2);
  77. Initialize(type, dictionary.Add(rootName), knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, emitTypeInformation, false, null, false);
  78. }
  79. public DataContractJsonSerializer(Type type, XmlDictionaryString rootName,
  80. IEnumerable<Type> knownTypes,
  81. int maxItemsInObjectGraph,
  82. bool ignoreExtensionDataObject,
  83. IDataContractSurrogate dataContractSurrogate,
  84. bool alwaysEmitTypeInformation)
  85. {
  86. EmitTypeInformation emitTypeInformation = alwaysEmitTypeInformation ? EmitTypeInformation.Always : EmitTypeInformation.AsNeeded;
  87. Initialize(type, rootName, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, emitTypeInformation, false, null, false);
  88. }
  89. public DataContractJsonSerializer(Type type, DataContractJsonSerializerSettings settings)
  90. {
  91. if (settings == null)
  92. {
  93. settings = new DataContractJsonSerializerSettings();
  94. }
  95. XmlDictionaryString rootName = (settings.RootName == null) ? null : new XmlDictionary(1).Add(settings.RootName);
  96. Initialize(type, rootName, settings.KnownTypes, settings.MaxItemsInObjectGraph, settings.IgnoreExtensionDataObject, settings.DataContractSurrogate,
  97. settings.EmitTypeInformation, settings.SerializeReadOnlyTypes, settings.DateTimeFormat, settings.UseSimpleDictionaryFormat);
  98. }
  99. public IDataContractSurrogate DataContractSurrogate
  100. {
  101. get { return dataContractSurrogate; }
  102. }
  103. public bool IgnoreExtensionDataObject
  104. {
  105. get { return ignoreExtensionDataObject; }
  106. }
  107. public ReadOnlyCollection<Type> KnownTypes
  108. {
  109. get
  110. {
  111. if (knownTypeCollection == null)
  112. {
  113. if (knownTypeList != null)
  114. {
  115. knownTypeCollection = new ReadOnlyCollection<Type>(knownTypeList);
  116. }
  117. else
  118. {
  119. knownTypeCollection = new ReadOnlyCollection<Type>(Globals.EmptyTypeArray);
  120. }
  121. }
  122. return knownTypeCollection;
  123. }
  124. }
  125. internal override DataContractDictionary KnownDataContracts
  126. {
  127. get
  128. {
  129. if (this.knownDataContracts == null && this.knownTypeList != null)
  130. {
  131. // This assignment may be performed concurrently and thus is a race condition.
  132. // It's safe, however, because at worse a new (and identical) dictionary of
  133. // data contracts will be created and re-assigned to this field. Introduction
  134. // of a lock here could lead to deadlocks.
  135. this.knownDataContracts = XmlObjectSerializerContext.GetDataContractsForKnownTypes(this.knownTypeList);
  136. }
  137. return this.knownDataContracts;
  138. }
  139. }
  140. public int MaxItemsInObjectGraph
  141. {
  142. get { return maxItemsInObjectGraph; }
  143. }
  144. internal bool AlwaysEmitTypeInformation
  145. {
  146. get
  147. {
  148. return emitTypeInformation == EmitTypeInformation.Always;
  149. }
  150. }
  151. public EmitTypeInformation EmitTypeInformation
  152. {
  153. get
  154. {
  155. return emitTypeInformation;
  156. }
  157. }
  158. public bool SerializeReadOnlyTypes
  159. {
  160. get
  161. {
  162. return serializeReadOnlyTypes;
  163. }
  164. }
  165. public DateTimeFormat DateTimeFormat
  166. {
  167. get
  168. {
  169. return dateTimeFormat;
  170. }
  171. }
  172. public bool UseSimpleDictionaryFormat
  173. {
  174. get
  175. {
  176. return useSimpleDictionaryFormat;
  177. }
  178. }
  179. DataContract RootContract
  180. {
  181. get
  182. {
  183. if (rootContract == null)
  184. {
  185. rootContract = DataContract.GetDataContract(((dataContractSurrogate == null) ? rootType :
  186. DataContractSerializer.GetSurrogatedType(dataContractSurrogate, rootType)));
  187. CheckIfTypeIsReference(rootContract);
  188. }
  189. return rootContract;
  190. }
  191. }
  192. XmlDictionaryString RootName
  193. {
  194. get
  195. {
  196. return rootName ?? JsonGlobals.rootDictionaryString;
  197. }
  198. }
  199. public override bool IsStartObject(XmlReader reader)
  200. {
  201. // No need to pass in DateTimeFormat to JsonReaderDelegator: no DateTimes will be read in IsStartObject
  202. return IsStartObjectHandleExceptions(new JsonReaderDelegator(reader));
  203. }
  204. public override bool IsStartObject(XmlDictionaryReader reader)
  205. {
  206. // No need to pass in DateTimeFormat to JsonReaderDelegator: no DateTimes will be read in IsStartObject
  207. return IsStartObjectHandleExceptions(new JsonReaderDelegator(reader));
  208. }
  209. public override object ReadObject(Stream stream)
  210. {
  211. CheckNull(stream, "stream");
  212. return ReadObject(JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max));
  213. }
  214. public override object ReadObject(XmlReader reader)
  215. {
  216. return ReadObjectHandleExceptions(new JsonReaderDelegator(reader, this.DateTimeFormat), true);
  217. }
  218. public override object ReadObject(XmlReader reader, bool verifyObjectName)
  219. {
  220. return ReadObjectHandleExceptions(new JsonReaderDelegator(reader, this.DateTimeFormat), verifyObjectName);
  221. }
  222. public override object ReadObject(XmlDictionaryReader reader)
  223. {
  224. return ReadObjectHandleExceptions(new JsonReaderDelegator(reader, this.DateTimeFormat), true); // verifyObjectName
  225. }
  226. public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
  227. {
  228. return ReadObjectHandleExceptions(new JsonReaderDelegator(reader, this.DateTimeFormat), verifyObjectName);
  229. }
  230. public override void WriteEndObject(XmlWriter writer)
  231. {
  232. // No need to pass in DateTimeFormat to JsonWriterDelegator: no DateTimes will be written in end object
  233. WriteEndObjectHandleExceptions(new JsonWriterDelegator(writer));
  234. }
  235. public override void WriteEndObject(XmlDictionaryWriter writer)
  236. {
  237. // No need to pass in DateTimeFormat to JsonWriterDelegator: no DateTimes will be written in end object
  238. WriteEndObjectHandleExceptions(new JsonWriterDelegator(writer));
  239. }
  240. public override void WriteObject(Stream stream, object graph)
  241. {
  242. CheckNull(stream, "stream");
  243. XmlDictionaryWriter jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, false); // ownsStream
  244. WriteObject(jsonWriter, graph);
  245. jsonWriter.Flush();
  246. }
  247. public override void WriteObject(XmlWriter writer, object graph)
  248. {
  249. WriteObjectHandleExceptions(new JsonWriterDelegator(writer, this.DateTimeFormat), graph);
  250. }
  251. public override void WriteObject(XmlDictionaryWriter writer, object graph)
  252. {
  253. WriteObjectHandleExceptions(new JsonWriterDelegator(writer, this.DateTimeFormat), graph);
  254. }
  255. public override void WriteObjectContent(XmlWriter writer, object graph)
  256. {
  257. WriteObjectContentHandleExceptions(new JsonWriterDelegator(writer, this.DateTimeFormat), graph);
  258. }
  259. public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
  260. {
  261. WriteObjectContentHandleExceptions(new JsonWriterDelegator(writer, this.DateTimeFormat), graph);
  262. }
  263. public override void WriteStartObject(XmlWriter writer, object graph)
  264. {
  265. // No need to pass in DateTimeFormat to JsonWriterDelegator: no DateTimes will be written in start object
  266. WriteStartObjectHandleExceptions(new JsonWriterDelegator(writer), graph);
  267. }
  268. public override void WriteStartObject(XmlDictionaryWriter writer, object graph)
  269. {
  270. // No need to pass in DateTimeFormat to JsonWriterDelegator: no DateTimes will be written in start object
  271. WriteStartObjectHandleExceptions(new JsonWriterDelegator(writer), graph);
  272. }
  273. internal static bool CheckIfJsonNameRequiresMapping(string jsonName)
  274. {
  275. if (jsonName != null)
  276. {
  277. if (!DataContract.IsValidNCName(jsonName))
  278. {
  279. return true;
  280. }
  281. for (int i = 0; i < jsonName.Length; i++)
  282. {
  283. if (XmlJsonWriter.CharacterNeedsEscaping(jsonName[i]))
  284. {
  285. return true;
  286. }
  287. }
  288. }
  289. return false;
  290. }
  291. internal static bool CheckIfJsonNameRequiresMapping(XmlDictionaryString jsonName)
  292. {
  293. return (jsonName == null) ? false : CheckIfJsonNameRequiresMapping(jsonName.Value);
  294. }
  295. internal static bool CheckIfXmlNameRequiresMapping(string xmlName)
  296. {
  297. return (xmlName == null) ? false : CheckIfJsonNameRequiresMapping(ConvertXmlNameToJsonName(xmlName));
  298. }
  299. internal static bool CheckIfXmlNameRequiresMapping(XmlDictionaryString xmlName)
  300. {
  301. return (xmlName == null) ? false : CheckIfXmlNameRequiresMapping(xmlName.Value);
  302. }
  303. internal static string ConvertXmlNameToJsonName(string xmlName)
  304. {
  305. return XmlConvert.DecodeName(xmlName);
  306. }
  307. internal static XmlDictionaryString ConvertXmlNameToJsonName(XmlDictionaryString xmlName)
  308. {
  309. return (xmlName == null) ? null : new XmlDictionary().Add(ConvertXmlNameToJsonName(xmlName.Value));
  310. }
  311. internal static bool IsJsonLocalName(XmlReaderDelegator reader, string elementName)
  312. {
  313. string name;
  314. if (XmlObjectSerializerReadContextComplexJson.TryGetJsonLocalName(reader, out name))
  315. {
  316. return (elementName == name);
  317. }
  318. return false;
  319. }
  320. internal static object ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson context)
  321. {
  322. return JsonDataContract.GetJsonDataContract(contract).ReadJsonValue(reader, context);
  323. }
  324. internal static void WriteJsonNull(XmlWriterDelegator writer)
  325. {
  326. writer.WriteAttributeString(null, JsonGlobals.typeString, null, JsonGlobals.nullString); // prefix // namespace
  327. }
  328. internal static void WriteJsonValue(JsonDataContract contract, XmlWriterDelegator writer, object graph, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle)
  329. {
  330. contract.WriteJsonValue(writer, graph, context, declaredTypeHandle);
  331. }
  332. internal override Type GetDeserializeType()
  333. {
  334. return rootType;
  335. }
  336. internal override Type GetSerializeType(object graph)
  337. {
  338. return (graph == null) ? rootType : graph.GetType();
  339. }
  340. internal override bool InternalIsStartObject(XmlReaderDelegator reader)
  341. {
  342. if (IsRootElement(reader, RootContract, RootName, XmlDictionaryString.Empty))
  343. {
  344. return true;
  345. }
  346. return IsJsonLocalName(reader, RootName.Value);
  347. }
  348. internal override object InternalReadObject(XmlReaderDelegator xmlReader, bool verifyObjectName)
  349. {
  350. if (MaxItemsInObjectGraph == 0)
  351. {
  352. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph)));
  353. }
  354. if (verifyObjectName)
  355. {
  356. if (!InternalIsStartObject(xmlReader))
  357. {
  358. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationExceptionWithReaderDetails(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ExpectingElement, XmlDictionaryString.Empty, RootName), xmlReader));
  359. }
  360. }
  361. else if (!IsStartElement(xmlReader))
  362. {
  363. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationExceptionWithReaderDetails(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ExpectingElementAtDeserialize, XmlNodeType.Element), xmlReader));
  364. }
  365. DataContract contract = RootContract;
  366. if (contract.IsPrimitive && object.ReferenceEquals(contract.UnderlyingType, rootType))// handle Nullable<T> differently
  367. {
  368. return DataContractJsonSerializer.ReadJsonValue(contract, xmlReader, null);
  369. }
  370. XmlObjectSerializerReadContextComplexJson context = XmlObjectSerializerReadContextComplexJson.CreateContext(this, contract);
  371. return context.InternalDeserialize(xmlReader, rootType, contract, null, null);
  372. }
  373. internal override void InternalWriteEndObject(XmlWriterDelegator writer)
  374. {
  375. writer.WriteEndElement();
  376. }
  377. internal override void InternalWriteObject(XmlWriterDelegator writer, object graph)
  378. {
  379. InternalWriteStartObject(writer, graph);
  380. InternalWriteObjectContent(writer, graph);
  381. InternalWriteEndObject(writer);
  382. }
  383. internal override void InternalWriteObjectContent(XmlWriterDelegator writer, object graph)
  384. {
  385. if (MaxItemsInObjectGraph == 0)
  386. {
  387. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph)));
  388. }
  389. DataContract contract = RootContract;
  390. Type declaredType = contract.UnderlyingType;
  391. Type graphType = (graph == null) ? declaredType : graph.GetType();
  392. if (dataContractSurrogate != null)
  393. {
  394. graph = DataContractSerializer.SurrogateToDataContractType(dataContractSurrogate, graph, declaredType, ref graphType);
  395. }
  396. if (graph == null)
  397. {
  398. WriteJsonNull(writer);
  399. }
  400. else
  401. {
  402. if (declaredType == graphType)
  403. {
  404. if (contract.CanContainReferences)
  405. {
  406. XmlObjectSerializerWriteContextComplexJson context = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, contract);
  407. context.OnHandleReference(writer, graph, true); // canContainReferences
  408. context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle);
  409. }
  410. else
  411. {
  412. DataContractJsonSerializer.WriteJsonValue(JsonDataContract.GetJsonDataContract(contract), writer, graph, null, declaredType.TypeHandle); // XmlObjectSerializerWriteContextComplexJson
  413. }
  414. }
  415. else
  416. {
  417. XmlObjectSerializerWriteContextComplexJson context = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, RootContract);
  418. contract = DataContractJsonSerializer.GetDataContract(contract, declaredType, graphType);
  419. if (contract.CanContainReferences)
  420. {
  421. context.OnHandleReference(writer, graph, true); // canContainCyclicReference
  422. context.SerializeWithXsiTypeAtTopLevel(contract, writer, graph, declaredType.TypeHandle, graphType);
  423. }
  424. else
  425. {
  426. context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle);
  427. }
  428. }
  429. }
  430. }
  431. internal override void InternalWriteStartObject(XmlWriterDelegator writer, object graph)
  432. {
  433. if (this.rootNameRequiresMapping)
  434. {
  435. writer.WriteStartElement("a", JsonGlobals.itemString, JsonGlobals.itemString);
  436. writer.WriteAttributeString(null, JsonGlobals.itemString, null, RootName.Value);
  437. }
  438. else
  439. {
  440. writer.WriteStartElement(RootName, XmlDictionaryString.Empty);
  441. }
  442. }
  443. void AddCollectionItemTypeToKnownTypes(Type knownType)
  444. {
  445. Type itemType;
  446. Type typeToCheck = knownType;
  447. while (CollectionDataContract.IsCollection(typeToCheck, out itemType))
  448. {
  449. if (itemType.IsGenericType && (itemType.GetGenericTypeDefinition() == Globals.TypeOfKeyValue))
  450. {
  451. itemType = Globals.TypeOfKeyValuePair.MakeGenericType(itemType.GetGenericArguments());
  452. }
  453. this.knownTypeList.Add(itemType);
  454. typeToCheck = itemType;
  455. }
  456. }
  457. void Initialize(Type type,
  458. IEnumerable<Type> knownTypes,
  459. int maxItemsInObjectGraph,
  460. bool ignoreExtensionDataObject,
  461. IDataContractSurrogate dataContractSurrogate,
  462. EmitTypeInformation emitTypeInformation,
  463. bool serializeReadOnlyTypes,
  464. DateTimeFormat dateTimeFormat,
  465. bool useSimpleDictionaryFormat)
  466. {
  467. CheckNull(type, "type");
  468. this.rootType = type;
  469. if (knownTypes != null)
  470. {
  471. this.knownTypeList = new List<Type>();
  472. foreach (Type knownType in knownTypes)
  473. {
  474. this.knownTypeList.Add(knownType);
  475. if (knownType != null)
  476. {
  477. AddCollectionItemTypeToKnownTypes(knownType);
  478. }
  479. }
  480. }
  481. if (maxItemsInObjectGraph < 0)
  482. {
  483. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxItemsInObjectGraph", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ValueMustBeNonNegative)));
  484. }
  485. this.maxItemsInObjectGraph = maxItemsInObjectGraph;
  486. this.ignoreExtensionDataObject = ignoreExtensionDataObject;
  487. this.dataContractSurrogate = dataContractSurrogate;
  488. this.emitTypeInformation = emitTypeInformation;
  489. this.serializeReadOnlyTypes = serializeReadOnlyTypes;
  490. this.dateTimeFormat = dateTimeFormat;
  491. this.useSimpleDictionaryFormat = useSimpleDictionaryFormat;
  492. }
  493. void Initialize(Type type,
  494. XmlDictionaryString rootName,
  495. IEnumerable<Type> knownTypes,
  496. int maxItemsInObjectGraph,
  497. bool ignoreExtensionDataObject,
  498. IDataContractSurrogate dataContractSurrogate,
  499. EmitTypeInformation emitTypeInformation,
  500. bool serializeReadOnlyTypes,
  501. DateTimeFormat dateTimeFormat,
  502. bool useSimpleDictionaryFormat)
  503. {
  504. Initialize(type, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, emitTypeInformation, serializeReadOnlyTypes, dateTimeFormat, useSimpleDictionaryFormat);
  505. this.rootName = ConvertXmlNameToJsonName(rootName);
  506. this.rootNameRequiresMapping = CheckIfJsonNameRequiresMapping(this.rootName);
  507. }
  508. internal static void CheckIfTypeIsReference(DataContract dataContract)
  509. {
  510. if (dataContract.IsReference)
  511. {
  512. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  513. XmlObjectSerializer.CreateSerializationException(SR.GetString(
  514. SR.JsonUnsupportedForIsReference,
  515. DataContract.GetClrTypeFullName(dataContract.UnderlyingType),
  516. dataContract.IsReference)));
  517. }
  518. }
  519. internal static DataContract GetDataContract(DataContract declaredTypeContract, Type declaredType, Type objectType)
  520. {
  521. DataContract contract = DataContractSerializer.GetDataContract(declaredTypeContract, declaredType, objectType);
  522. CheckIfTypeIsReference(contract);
  523. return contract;
  524. }
  525. }
  526. }