Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlJsonWriter.cs
Go to the documentation of this file.
1using System.IO;
2using System.Text;
3using System.Xml;
4
6
8{
9 private enum JsonDataType
10 {
11 None,
12 Null,
13 Boolean,
14 Number,
15 String,
16 Object,
17 Array
18 }
19
20 [Flags]
21 private enum NameState
22 {
23 None = 0,
27 }
28
29 private sealed class JsonNodeWriter : XmlUTF8NodeWriter
30 {
31 internal unsafe void WriteChars(char* chars, int charCount)
32 {
34 }
35 }
36
37 private const char BACK_SLASH = '\\';
38
39 private const char FORWARD_SLASH = '/';
40
41 private const char HIGH_SURROGATE_START = '\ud800';
42
43 private const char LOW_SURROGATE_END = '\udfff';
44
45 private const char MAX_CHAR = '\ufffe';
46
47 private const char WHITESPACE = ' ';
48
49 private const char CARRIAGE_RETURN = '\r';
50
51 private const char NEWLINE = '\n';
52
53 private const string xmlNamespace = "http://www.w3.org/XML/1998/namespace";
54
55 private const string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
56
57 private static readonly string[] s_escapedJsonStringTable = new string[32]
58 {
59 "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007", "\\b", "\\t",
60 "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013",
61 "\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019", "\\u001a", "\\u001b", "\\u001c", "\\u001d",
62 "\\u001e", "\\u001f"
63 };
64
66
67 private string _attributeText;
68
70
71 private int _depth;
72
73 private bool _endElementBuffer;
74
76
78
80
82
84
86
88
90
91 private string _serverTypeValue;
92
94
96
97 private readonly bool _indent;
98
99 private readonly string _indentChars;
100
101 private int _indentLevel;
102
103 public override XmlWriterSettings Settings => null;
104
105 public override WriteState WriteState
106 {
107 get
108 {
109 if (_writeState == WriteState.Closed)
110 {
111 return WriteState.Closed;
112 }
114 {
115 return WriteState.Attribute;
116 }
117 switch (_nodeType)
118 {
119 case JsonNodeType.None:
120 return WriteState.Start;
121 case JsonNodeType.Element:
122 return WriteState.Element;
123 case JsonNodeType.EndElement:
124 case JsonNodeType.QuotedText:
125 case JsonNodeType.StandaloneText:
126 return WriteState.Content;
127 default:
128 return WriteState.Error;
129 }
130 }
131 }
132
133 public override string XmlLang => null;
134
135 public override XmlSpace XmlSpace => XmlSpace.None;
136
138 {
139 get
140 {
141 if (s_binHexEncoding == null)
142 {
144 }
145 return s_binHexEncoding;
146 }
147 }
148
149 private bool HasOpenAttribute
150 {
151 get
152 {
154 {
156 }
157 return true;
158 }
159 }
160
161 private bool IsClosed => WriteState == WriteState.Closed;
162
164 {
165 get
166 {
167 if (_depth > 0)
168 {
169 return _scopes[_depth] == JsonNodeType.Collection;
170 }
171 return false;
172 }
173 }
174
175 private bool IsWritingNameAttribute => (_nameState & NameState.IsWritingNameAttribute) == NameState.IsWritingNameAttribute;
176
177 private bool IsWritingNameWithMapping => (_nameState & NameState.IsWritingNameWithMapping) == NameState.IsWritingNameWithMapping;
178
179 private bool WrittenNameWithMapping => (_nameState & NameState.WrittenNameWithMapping) == NameState.WrittenNameWithMapping;
180
182 : this(indent: false, null)
183 {
184 }
185
186 public XmlJsonWriter(bool indent, string indentChars)
187 {
188 _indent = indent;
189 if (indent)
190 {
191 if (indentChars == null)
192 {
193 throw new ArgumentNullException("indentChars");
194 }
196 }
198 }
199
200 public override void Close()
201 {
202 if (!IsClosed)
203 {
204 try
205 {
207 }
208 finally
209 {
210 try
211 {
214 }
215 finally
216 {
217 _writeState = WriteState.Closed;
218 if (_depth != 0)
219 {
220 _depth = 0;
221 }
222 }
223 }
224 }
225 base.Close();
226 }
227
228 public override void Flush()
229 {
230 if (IsClosed)
231 {
232 ThrowClosed();
233 }
235 }
236
237 public override string LookupPrefix(string ns)
238 {
239 if (ns == null)
240 {
241 throw new ArgumentNullException("ns");
242 }
243 if (ns == "http://www.w3.org/2000/xmlns/")
244 {
245 return "xmlns";
246 }
247 if (ns == "http://www.w3.org/XML/1998/namespace")
248 {
249 return "xml";
250 }
251 if (ns.Length == 0)
252 {
253 return string.Empty;
254 }
255 return null;
256 }
257
258 public void SetOutput(Stream stream, Encoding encoding, bool ownsStream)
259 {
260 if (stream == null)
261 {
262 throw new ArgumentNullException("stream");
263 }
264 if (encoding == null)
265 {
266 throw new ArgumentNullException("encoding");
267 }
268 if (encoding.WebName != Encoding.UTF8.WebName)
269 {
270 stream = new JsonEncodingStreamWrapper(stream, encoding, isReader: false);
271 }
272 else
273 {
274 encoding = null;
275 }
276 if (_nodeWriter == null)
277 {
279 }
282 }
283
284 public override void WriteArray(string prefix, string localName, string namespaceUri, bool[] array, int offset, int count)
285 {
287 }
288
289 public override void WriteArray(string prefix, string localName, string namespaceUri, short[] array, int offset, int count)
290 {
292 }
293
294 public override void WriteArray(string prefix, string localName, string namespaceUri, int[] array, int offset, int count)
295 {
297 }
298
299 public override void WriteArray(string prefix, string localName, string namespaceUri, long[] array, int offset, int count)
300 {
302 }
303
304 public override void WriteArray(string prefix, string localName, string namespaceUri, float[] array, int offset, int count)
305 {
307 }
308
309 public override void WriteArray(string prefix, string localName, string namespaceUri, double[] array, int offset, int count)
310 {
312 }
313
314 public override void WriteArray(string prefix, string localName, string namespaceUri, decimal[] array, int offset, int count)
315 {
317 }
318
319 public override void WriteArray(string prefix, string localName, string namespaceUri, DateTime[] array, int offset, int count)
320 {
322 }
323
324 public override void WriteArray(string prefix, string localName, string namespaceUri, Guid[] array, int offset, int count)
325 {
327 }
328
329 public override void WriteArray(string prefix, string localName, string namespaceUri, TimeSpan[] array, int offset, int count)
330 {
332 }
333
334 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, bool[] array, int offset, int count)
335 {
337 }
338
339 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, decimal[] array, int offset, int count)
340 {
342 }
343
344 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, double[] array, int offset, int count)
345 {
347 }
348
349 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, float[] array, int offset, int count)
350 {
352 }
353
354 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, int[] array, int offset, int count)
355 {
357 }
358
359 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, long[] array, int offset, int count)
360 {
362 }
363
364 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, short[] array, int offset, int count)
365 {
367 }
368
369 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, DateTime[] array, int offset, int count)
370 {
372 }
373
374 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Guid[] array, int offset, int count)
375 {
377 }
378
379 public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, TimeSpan[] array, int offset, int count)
380 {
382 }
383
384 public override void WriteBase64(byte[] buffer, int index, int count)
385 {
386 if (buffer == null)
387 {
388 throw new ArgumentNullException("buffer");
389 }
390 if (index < 0)
391 {
393 }
394 if (count < 0)
395 {
397 }
398 if (count > buffer.Length - index)
399 {
401 }
402 StartText();
404 }
405
406 public override void WriteBinHex(byte[] buffer, int index, int count)
407 {
408 if (buffer == null)
409 {
410 throw new ArgumentNullException("buffer");
411 }
412 if (index < 0)
413 {
415 }
416 if (count < 0)
417 {
419 }
420 if (count > buffer.Length - index)
421 {
423 }
424 StartText();
426 }
427
428 public override void WriteCData(string text)
429 {
431 }
432
433 public override void WriteCharEntity(char ch)
434 {
435 WriteString(ch.ToString());
436 }
437
438 public override void WriteChars(char[] buffer, int index, int count)
439 {
440 if (buffer == null)
441 {
442 throw new ArgumentNullException("buffer");
443 }
444 if (index < 0)
445 {
447 }
448 if (count < 0)
449 {
451 }
452 if (count > buffer.Length - index)
453 {
455 }
456 WriteString(new string(buffer, index, count));
457 }
458
459 public override void WriteComment(string text)
460 {
462 }
463
464 public override void WriteDocType(string name, string pubid, string sysid, string subset)
465 {
467 }
468
469 public override void WriteEndAttribute()
470 {
471 if (IsClosed)
472 {
473 ThrowClosed();
474 }
475 if (!HasOpenAttribute)
476 {
478 }
480 {
481 switch (_attributeText)
482 {
483 case "number":
484 ThrowIfServerTypeWritten("number");
485 _dataType = JsonDataType.Number;
486 break;
487 case "string":
488 ThrowIfServerTypeWritten("string");
489 _dataType = JsonDataType.String;
490 break;
491 case "array":
493 _dataType = JsonDataType.Array;
494 break;
495 case "object":
496 _dataType = JsonDataType.Object;
497 break;
498 case "null":
500 _dataType = JsonDataType.Null;
501 break;
502 case "boolean":
503 ThrowIfServerTypeWritten("boolean");
504 _dataType = JsonDataType.Boolean;
505 break;
506 default:
508 }
509 _attributeText = null;
512 {
514 }
515 }
517 {
519 _attributeText = null;
522 {
524 }
525 }
526 else if (IsWritingNameAttribute)
527 {
529 _attributeText = null;
532 }
534 {
535 if (!string.IsNullOrEmpty(_attributeText) && _isWritingXmlnsAttributeDefaultNs)
536 {
538 }
539 _attributeText = null;
542 }
543 }
544
545 public override void WriteEndDocument()
546 {
547 if (IsClosed)
548 {
549 ThrowClosed();
550 }
551 if (_nodeType != 0)
552 {
553 while (_depth > 0)
554 {
556 }
557 }
558 }
559
560 public override void WriteEndElement()
561 {
562 if (IsClosed)
563 {
564 ThrowClosed();
565 }
566 if (_depth == 0)
567 {
569 }
571 {
573 }
574 _endElementBuffer = false;
576 if (jsonNodeType == JsonNodeType.Collection)
577 {
578 _indentLevel--;
579 if (_indent)
580 {
581 if (_nodeType == JsonNodeType.Element)
582 {
584 }
585 else
586 {
587 WriteNewLine();
588 WriteIndent();
589 }
590 }
593 }
594 else if (_nodeType == JsonNodeType.QuotedText)
595 {
597 }
598 else if (_nodeType == JsonNodeType.Element)
599 {
600 if (_dataType == JsonDataType.None && _serverTypeValue != null)
601 {
602 throw new XmlException(System.SR.Format(System.SR.JsonMustSpecifyDataType, "type", "object", "__type"));
603 }
605 {
606 throw new XmlException(System.SR.Format(System.SR.JsonMustSpecifyDataType, "item", string.Empty, "item"));
607 }
608 if (_dataType == JsonDataType.None || _dataType == JsonDataType.String)
609 {
612 }
613 }
614 if (_depth != 0)
615 {
616 switch (jsonNodeType)
617 {
618 case JsonNodeType.Element:
619 _endElementBuffer = true;
620 break;
621 case JsonNodeType.Object:
622 _indentLevel--;
623 if (_indent)
624 {
625 if (_nodeType == JsonNodeType.Element)
626 {
628 }
629 else
630 {
631 WriteNewLine();
632 WriteIndent();
633 }
634 }
636 if (_depth > 0 && _scopes[_depth] == JsonNodeType.Element)
637 {
638 ExitScope();
639 _endElementBuffer = true;
640 }
641 break;
642 }
643 }
644 _dataType = JsonDataType.None;
645 _nodeType = JsonNodeType.EndElement;
646 _nameState = NameState.None;
648 }
649
650 public override void WriteEntityRef(string name)
651 {
653 }
654
655 public override void WriteFullEndElement()
656 {
658 }
659
660 public override void WriteProcessingInstruction(string name, string text)
661 {
662 if (IsClosed)
663 {
664 ThrowClosed();
665 }
666 if (!name.Equals("xml", StringComparison.OrdinalIgnoreCase))
667 {
669 }
670 if (WriteState != 0)
671 {
673 }
674 }
675
676 public override void WriteQualifiedName(string localName, string ns)
677 {
678 if (localName == null)
679 {
680 throw new ArgumentNullException("localName");
681 }
682 if (localName.Length == 0)
683 {
685 }
686 if (ns == null)
687 {
688 ns = string.Empty;
689 }
690 base.WriteQualifiedName(localName, ns);
691 }
692
693 public override void WriteRaw(string data)
694 {
695 WriteString(data);
696 }
697
698 public override void WriteRaw(char[] buffer, int index, int count)
699 {
700 if (buffer == null)
701 {
702 throw new ArgumentNullException("buffer");
703 }
704 if (index < 0)
705 {
707 }
708 if (count < 0)
709 {
711 }
712 if (count > buffer.Length - index)
713 {
715 }
716 WriteString(new string(buffer, index, count));
717 }
718
719 public override void WriteStartAttribute(string prefix, string localName, string ns)
720 {
721 if (IsClosed)
722 {
723 ThrowClosed();
724 }
725 if (!string.IsNullOrEmpty(prefix))
726 {
727 if (!IsWritingNameWithMapping || !(prefix == "xmlns"))
728 {
730 }
731 if (ns != null && ns != "http://www.w3.org/2000/xmlns/")
732 {
733 throw new ArgumentException(System.SR.Format(System.SR.XmlPrefixBoundToNamespace, "xmlns", "http://www.w3.org/2000/xmlns/", ns), "ns");
734 }
735 }
736 else if (IsWritingNameWithMapping && ns == "http://www.w3.org/2000/xmlns/" && localName != "xmlns")
737 {
738 prefix = "xmlns";
739 }
740 if (!string.IsNullOrEmpty(ns))
741 {
742 if (IsWritingNameWithMapping && ns == "http://www.w3.org/2000/xmlns/")
743 {
744 prefix = "xmlns";
745 }
746 else
747 {
748 if (!string.IsNullOrEmpty(prefix) || !(localName == "xmlns") || !(ns == "http://www.w3.org/2000/xmlns/"))
749 {
751 }
752 prefix = "xmlns";
754 }
755 }
756 if (localName == null)
757 {
758 throw new ArgumentNullException("localName");
759 }
760 if (localName.Length == 0)
761 {
763 }
765 {
767 }
769 {
770 throw new XmlException(System.SR.Format(System.SR.JsonOpenAttributeMustBeClosedFirst, "WriteStartAttribute"));
771 }
772 if (prefix == "xmlns")
773 {
775 return;
776 }
777 switch (localName)
778 {
779 case "type":
780 if (_dataType != 0)
781 {
783 }
785 break;
786 case "__type":
787 if (_serverTypeValue != null)
788 {
790 }
791 if (_dataType != 0 && _dataType != JsonDataType.Object)
792 {
793 throw new XmlException(System.SR.Format(System.SR.JsonServerTypeSpecifiedForInvalidDataType, "__type", "type", _dataType.ToString().ToLowerInvariant(), "object"));
794 }
796 break;
797 case "item":
799 {
801 }
803 {
805 }
806 _nameState |= NameState.IsWritingNameAttribute;
807 break;
808 default:
809 throw new ArgumentException(System.SR.Format(System.SR.JsonUnexpectedAttributeLocalName, localName), "localName");
810 }
811 }
812
813 public override void WriteStartDocument(bool standalone)
814 {
816 }
817
818 public override void WriteStartDocument()
819 {
820 if (IsClosed)
821 {
822 ThrowClosed();
823 }
824 if (WriteState != 0)
825 {
826 throw new XmlException(System.SR.Format(System.SR.JsonInvalidWriteState, "WriteStartDocument", WriteState.ToString()));
827 }
828 }
829
830 public override void WriteStartElement(string prefix, string localName, string ns)
831 {
832 if (localName == null)
833 {
834 throw new ArgumentNullException("localName");
835 }
836 if (localName.Length == 0)
837 {
839 }
840 if (!string.IsNullOrEmpty(prefix) && (string.IsNullOrEmpty(ns) || !TrySetWritingNameWithMapping(localName, ns)))
841 {
843 }
844 if (!string.IsNullOrEmpty(ns) && !TrySetWritingNameWithMapping(localName, ns))
845 {
847 }
848 if (IsClosed)
849 {
850 ThrowClosed();
851 }
853 {
855 }
856 if (_nodeType != 0 && _depth == 0)
857 {
859 }
860 switch (_nodeType)
861 {
862 case JsonNodeType.None:
863 if (!localName.Equals("root"))
864 {
865 throw new XmlException(System.SR.Format(System.SR.JsonInvalidRootElementName, localName, "root"));
866 }
867 EnterScope(JsonNodeType.Element);
868 break;
869 case JsonNodeType.Element:
870 if (_dataType != JsonDataType.Array && _dataType != JsonDataType.Object)
871 {
873 }
874 if (_indent)
875 {
876 WriteNewLine();
877 WriteIndent();
878 }
880 {
881 if (_nameState != NameState.IsWritingNameWithMapping)
882 {
883 WriteJsonElementName(localName);
884 }
885 }
886 else if (!localName.Equals("item"))
887 {
889 }
890 EnterScope(JsonNodeType.Element);
891 break;
892 case JsonNodeType.EndElement:
894 {
896 }
897 if (_indent)
898 {
899 WriteNewLine();
900 WriteIndent();
901 }
903 {
904 if (_nameState != NameState.IsWritingNameWithMapping)
905 {
906 WriteJsonElementName(localName);
907 }
908 }
909 else if (!localName.Equals("item"))
910 {
912 }
913 EnterScope(JsonNodeType.Element);
914 break;
915 default:
917 }
922 _serverTypeValue = null;
923 _dataType = JsonDataType.None;
924 _nodeType = JsonNodeType.Element;
925 }
926
927 public override void WriteString(string text)
928 {
929 if (HasOpenAttribute && text != null)
930 {
932 return;
933 }
934 if (text == null)
935 {
936 text = string.Empty;
937 }
938 if ((_dataType != JsonDataType.Array && _dataType != JsonDataType.Object && _nodeType != JsonNodeType.EndElement) || !XmlConverter.IsWhitespace(text))
939 {
940 StartText();
942 }
943 }
944
945 public override void WriteSurrogateCharEntity(char lowChar, char highChar)
946 {
948 WriteString(new string(span));
949 }
950
951 public override void WriteValue(bool value)
952 {
953 StartText();
955 }
956
957 public override void WriteValue(decimal value)
958 {
959 StartText();
961 }
962
963 public override void WriteValue(double value)
964 {
965 StartText();
967 }
968
969 public override void WriteValue(float value)
970 {
971 StartText();
973 }
974
975 public override void WriteValue(int value)
976 {
977 StartText();
979 }
980
981 public override void WriteValue(long value)
982 {
983 StartText();
985 }
986
987 public override void WriteValue(Guid value)
988 {
989 StartText();
991 }
992
993 public override void WriteValue(DateTime value)
994 {
995 StartText();
997 }
998
999 public override void WriteValue(string value)
1000 {
1002 }
1003
1004 public override void WriteValue(TimeSpan value)
1005 {
1006 StartText();
1008 }
1009
1010 public override void WriteValue(UniqueId value)
1011 {
1012 if (value == null)
1013 {
1014 throw new ArgumentNullException("value");
1015 }
1016 StartText();
1018 }
1019
1020 public override void WriteValue(object value)
1021 {
1022 if (IsClosed)
1023 {
1024 ThrowClosed();
1025 }
1026 if (value == null)
1027 {
1028 throw new ArgumentNullException("value");
1029 }
1030 if (value is Array)
1031 {
1033 }
1034 else if (value is IStreamProvider)
1035 {
1037 }
1038 else
1039 {
1041 }
1042 }
1043
1044 public override void WriteWhitespace(string ws)
1045 {
1046 if (IsClosed)
1047 {
1048 ThrowClosed();
1049 }
1050 if (ws == null)
1051 {
1052 throw new ArgumentNullException("ws");
1053 }
1054 for (int i = 0; i < ws.Length; i++)
1055 {
1056 char c = ws[i];
1057 if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
1058 {
1059 throw new ArgumentException(System.SR.Format(System.SR.JsonOnlyWhitespace, c.ToString(), "WriteWhitespace"), "ws");
1060 }
1061 }
1062 WriteString(ws);
1063 }
1064
1065 public override void WriteXmlAttribute(string localName, string value)
1066 {
1067 throw new NotSupportedException(System.SR.Format(System.SR.JsonMethodNotSupported, "WriteXmlAttribute"));
1068 }
1069
1071 {
1072 throw new NotSupportedException(System.SR.Format(System.SR.JsonMethodNotSupported, "WriteXmlAttribute"));
1073 }
1074
1075 public override void WriteXmlnsAttribute(string prefix, string namespaceUri)
1076 {
1078 {
1079 throw new NotSupportedException(System.SR.Format(System.SR.JsonMethodNotSupported, "WriteXmlnsAttribute"));
1080 }
1081 }
1082
1083 public override void WriteXmlnsAttribute(string prefix, XmlDictionaryString namespaceUri)
1084 {
1086 {
1087 throw new NotSupportedException(System.SR.Format(System.SR.JsonMethodNotSupported, "WriteXmlnsAttribute"));
1088 }
1089 }
1090
1091 internal static bool CharacterNeedsEscaping(char ch)
1092 {
1093 if (ch != '/' && ch != '"' && ch >= ' ' && ch != '\\')
1094 {
1095 if (ch >= '\ud800')
1096 {
1097 if (ch > '\udfff')
1098 {
1099 return ch >= '\ufffe';
1100 }
1101 return true;
1102 }
1103 return false;
1104 }
1105 return true;
1106 }
1107
1108 private static void ThrowClosed()
1109 {
1111 }
1112
1114 {
1115 if (IsClosed)
1116 {
1117 ThrowClosed();
1118 }
1119 if (_depth == 0)
1120 {
1122 }
1123 if (nextNodeType == JsonNodeType.StandaloneText && _nodeType == JsonNodeType.QuotedText)
1124 {
1126 }
1127 }
1128
1130 {
1131 _depth++;
1132 if (_scopes == null)
1133 {
1134 _scopes = new JsonNodeType[4];
1135 }
1136 else if (_scopes.Length == _depth)
1137 {
1140 _scopes = array;
1141 }
1143 }
1144
1146 {
1147 JsonNodeType result = _scopes[_depth];
1148 _scopes[_depth] = JsonNodeType.None;
1149 _depth--;
1150 return result;
1151 }
1152
1153 private void InitializeWriter()
1154 {
1155 _nodeType = JsonNodeType.None;
1156 _dataType = JsonDataType.None;
1160 _serverTypeValue = null;
1161 _attributeText = null;
1162 if (_depth != 0)
1163 {
1164 _depth = 0;
1165 }
1166 if (_scopes != null && _scopes.Length > 25)
1167 {
1168 _scopes = null;
1169 }
1170 _writeState = WriteState.Start;
1171 _endElementBuffer = false;
1172 _indentLevel = 0;
1173 }
1174
1175 private static bool IsUnicodeNewlineCharacter(char c)
1176 {
1177 if (c != '\u0085' && c != '\u2028')
1178 {
1179 return c == '\u2029';
1180 }
1181 return true;
1182 }
1183
1184 private void StartText()
1185 {
1186 if (HasOpenAttribute)
1187 {
1189 }
1190 if (_dataType == JsonDataType.None && _serverTypeValue != null)
1191 {
1192 throw new XmlException(System.SR.Format(System.SR.JsonMustSpecifyDataType, "type", "object", "__type"));
1193 }
1195 {
1196 throw new XmlException(System.SR.Format(System.SR.JsonMustSpecifyDataType, "item", string.Empty, "item"));
1197 }
1198 if (_dataType == JsonDataType.String || _dataType == JsonDataType.None)
1199 {
1200 CheckText(JsonNodeType.QuotedText);
1201 if (_nodeType != JsonNodeType.QuotedText)
1202 {
1204 }
1205 _nodeType = JsonNodeType.QuotedText;
1206 }
1207 else if (_dataType == JsonDataType.Number || _dataType == JsonDataType.Boolean)
1208 {
1209 CheckText(JsonNodeType.StandaloneText);
1210 _nodeType = JsonNodeType.StandaloneText;
1211 }
1212 else
1213 {
1215 }
1216 }
1217
1219 {
1220 if (_serverTypeValue != null)
1221 {
1223 }
1224 }
1225
1227 {
1228 if (HasOpenAttribute)
1229 {
1231 }
1232 throw new XmlException(System.SR.Format(System.SR.JsonCannotWriteTextAfterNonTextAttribute, _dataType.ToString().ToLowerInvariant()));
1233 }
1234
1235 private bool TrySetWritingNameWithMapping(string localName, string ns)
1236 {
1237 if (localName.Equals("item") && ns.Equals("item"))
1238 {
1239 _nameState = NameState.IsWritingNameWithMapping;
1240 return true;
1241 }
1242 return false;
1243 }
1244
1246 {
1247 if (_dataType != 0)
1248 {
1249 switch (_dataType)
1250 {
1251 case JsonDataType.Array:
1252 EnterScope(JsonNodeType.Collection);
1254 _indentLevel++;
1255 break;
1256 case JsonDataType.Object:
1257 EnterScope(JsonNodeType.Object);
1259 _indentLevel++;
1260 break;
1261 case JsonDataType.Null:
1262 _nodeWriter.WriteText("null");
1263 break;
1264 }
1265 if (_serverTypeValue != null)
1266 {
1268 }
1269 }
1270 }
1271
1272 private unsafe void WriteEscapedJsonString(string str)
1273 {
1274 fixed (char* ptr = str)
1275 {
1276 int num = 0;
1277 int i;
1278 for (i = 0; i < str.Length; i++)
1279 {
1280 char c = ptr[i];
1281 if (c <= '/')
1282 {
1283 if (c == '/' || c == '"')
1284 {
1285 _nodeWriter.WriteChars(ptr + num, i - num);
1288 num = i + 1;
1289 }
1290 else if (c < ' ')
1291 {
1292 _nodeWriter.WriteChars(ptr + num, i - num);
1294 num = i + 1;
1295 }
1296 }
1297 else if (c == '\\')
1298 {
1299 _nodeWriter.WriteChars(ptr + num, i - num);
1302 num = i + 1;
1303 }
1304 else if ((c >= '\ud800' && (c <= '\udfff' || c >= '\ufffe')) || IsUnicodeNewlineCharacter(c))
1305 {
1306 _nodeWriter.WriteChars(ptr + num, i - num);
1309 _nodeWriter.WriteText($"{c:x4}");
1310 num = i + 1;
1311 }
1312 }
1313 if (num < i)
1314 {
1315 _nodeWriter.WriteChars(ptr + num, i - num);
1316 }
1317 }
1318 }
1319
1320 private void WriteIndent()
1321 {
1322 for (int i = 0; i < _indentLevel; i++)
1323 {
1325 }
1326 }
1327
1328 private void WriteNewLine()
1329 {
1332 }
1333
1334 private void WriteJsonElementName(string localName)
1335 {
1337 WriteEscapedJsonString(localName);
1340 if (_indent)
1341 {
1343 }
1344 }
1345
1346 private void WriteJsonQuote()
1347 {
1349 }
1350
1351 private void WritePrimitiveValue(object value)
1352 {
1353 if (IsClosed)
1354 {
1355 ThrowClosed();
1356 }
1357 if (value == null)
1358 {
1359 throw new ArgumentNullException("value");
1360 }
1361 if (value is ulong)
1362 {
1363 WriteValue((ulong)value);
1364 return;
1365 }
1366 if (value is string)
1367 {
1368 WriteValue((string)value);
1369 return;
1370 }
1371 if (value is int)
1372 {
1373 WriteValue((int)value);
1374 return;
1375 }
1376 if (value is long)
1377 {
1378 WriteValue((long)value);
1379 return;
1380 }
1381 if (value is bool)
1382 {
1383 WriteValue((bool)value);
1384 return;
1385 }
1386 if (value is double)
1387 {
1388 WriteValue((double)value);
1389 return;
1390 }
1391 if (value is DateTime)
1392 {
1394 return;
1395 }
1396 if (value is float)
1397 {
1398 WriteValue((float)value);
1399 return;
1400 }
1401 if (value is decimal)
1402 {
1403 WriteValue((decimal)value);
1404 return;
1405 }
1407 {
1409 return;
1410 }
1411 if (value is UniqueId)
1412 {
1414 return;
1415 }
1416 if (value is Guid)
1417 {
1419 return;
1420 }
1421 if (value is TimeSpan)
1422 {
1424 return;
1425 }
1426 if (value.GetType().IsArray)
1427 {
1429 }
1430 base.WriteValue(value);
1431 }
1432
1434 {
1436 JsonDataType dataType = _dataType;
1438 WriteStartElement("__type");
1441 _dataType = dataType;
1444 }
1445
1446 private void WriteValue(ulong value)
1447 {
1448 StartText();
1450 }
1451
1452 private void WriteValue(Array array)
1453 {
1454 JsonDataType dataType = _dataType;
1455 _dataType = JsonDataType.String;
1456 StartText();
1457 for (int i = 0; i < array.Length; i++)
1458 {
1459 if (i != 0)
1460 {
1462 }
1463 WritePrimitiveValue(array.GetValue(i));
1464 }
1465 _dataType = dataType;
1466 }
1467}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
override void WriteStartElement(string prefix, string localName, string ns)
override void WriteArray(string prefix, string localName, string namespaceUri, decimal[] array, int offset, int count)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, double[] array, int offset, int count)
override void WriteXmlAttribute(string localName, string value)
override void WriteRaw(char[] buffer, int index, int count)
override void WriteArray(string prefix, string localName, string namespaceUri, int[] array, int offset, int count)
override void WriteXmlnsAttribute(string prefix, string namespaceUri)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, decimal[] array, int offset, int count)
override void WriteArray(string prefix, string localName, string namespaceUri, double[] array, int offset, int count)
override void WriteDocType(string name, string pubid, string sysid, string subset)
bool TrySetWritingNameWithMapping(string localName, string ns)
override void WriteQualifiedName(string localName, string ns)
override void WriteBase64(byte[] buffer, int index, int count)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, bool[] array, int offset, int count)
override void WriteArray(string prefix, string localName, string namespaceUri, bool[] array, int offset, int count)
override void WriteArray(string prefix, string localName, string namespaceUri, Guid[] array, int offset, int count)
override void WriteSurrogateCharEntity(char lowChar, char highChar)
override void WriteArray(string prefix, string localName, string namespaceUri, DateTime[] array, int offset, int count)
override void WriteStartDocument(bool standalone)
override void WriteChars(char[] buffer, int index, int count)
void CheckText(JsonNodeType nextNodeType)
void ThrowIfServerTypeWritten(string dataTypeSpecified)
override void WriteArray(string prefix, string localName, string namespaceUri, TimeSpan[] array, int offset, int count)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, short[] array, int offset, int count)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, long[] array, int offset, int count)
override void WriteArray(string prefix, string localName, string namespaceUri, short[] array, int offset, int count)
override void WriteBinHex(byte[] buffer, int index, int count)
override void WriteArray(string prefix, string localName, string namespaceUri, float[] array, int offset, int count)
override void WriteProcessingInstruction(string name, string text)
XmlJsonWriter(bool indent, string indentChars)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, int[] array, int offset, int count)
override void WriteXmlnsAttribute(string prefix, XmlDictionaryString namespaceUri)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Guid[] array, int offset, int count)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, float[] array, int offset, int count)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, DateTime[] array, int offset, int count)
override void WriteStartAttribute(string prefix, string localName, string ns)
override void WriteArray(string prefix, string localName, string namespaceUri, long[] array, int offset, int count)
override void WriteXmlAttribute(XmlDictionaryString localName, XmlDictionaryString value)
void SetOutput(Stream stream, Encoding encoding, bool ownsStream)
void EnterScope(JsonNodeType currentNodeType)
override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, TimeSpan[] array, int offset, int count)
static string JsonWriterClosed
Definition SR.cs:596
static string JsonInvalidWriteState
Definition SR.cs:582
static string JsonMultipleRootElementsNotAllowedOnWriter
Definition SR.cs:584
static string JsonInvalidMethodBetweenStartEndAttribute
Definition SR.cs:604
static string JsonAttributeAlreadyWritten
Definition SR.cs:576
static string JsonNestedArraysNotSupported
Definition SR.cs:608
static string JsonAttributeMustHaveElement
Definition SR.cs:574
static string JsonInvalidLocalNameEmpty
Definition SR.cs:570
static string JsonNodeTypeArrayOrObjectNotSpecified
Definition SR.cs:588
static string JsonInvalidRootElementName
Definition SR.cs:586
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string JsonSizeExceedsRemainingBufferSpace
Definition SR.cs:544
static string JsonPrefixMustBeNullOrEmpty
Definition SR.cs:572
static string JsonServerTypeSpecifiedForInvalidDataType
Definition SR.cs:578
static string JsonNamespaceMustBeEmpty
Definition SR.cs:558
static string ValueMustBeNonNegative
Definition SR.cs:296
static string JsonInvalidStartElementCall
Definition SR.cs:592
static string JsonCannotWriteTextAfterNonTextAttribute
Definition SR.cs:606
static string JsonInvalidItemNameForArrayElement
Definition SR.cs:590
static string JsonUnexpectedAttributeLocalName
Definition SR.cs:580
static string JsonXmlProcessingInstructionNotSupported
Definition SR.cs:566
static string JsonMustSpecifyDataType
Definition SR.cs:564
static string JsonInvalidDataTypeSpecifiedForServerType
Definition SR.cs:602
static string XmlPrefixBoundToNamespace
Definition SR.cs:486
static string JsonXmlInvalidDeclaration
Definition SR.cs:568
static string JsonEndElementNoOpenNodes
Definition SR.cs:560
static string JsonOpenAttributeMustBeClosedFirst
Definition SR.cs:562
static string JsonCannotWriteStandaloneTextAfterQuotedText
Definition SR.cs:598
static string JsonWriteArrayNotSupported
Definition SR.cs:552
static string JsonMethodNotSupported
Definition SR.cs:554
static string JsonUnexpectedAttributeValue
Definition SR.cs:534
static string XmlIllegalOutsideRoot
Definition SR.cs:414
static string JsonOnlyWhitespace
Definition SR.cs:594
static string JsonNoMatchingStartAttribute
Definition SR.cs:556
static string JsonMustUseWriteStringForWritingAttributeValues
Definition SR.cs:600
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
virtual string WebName
Definition Encoding.cs:386
unsafe string GetString(byte *bytes, int byteCount)
Definition Encoding.cs:973
static bool IsWhitespace(string s)
unsafe void UnsafeWriteUTF8Chars(char *chars, int charCount)
override void WriteDateTimeText(DateTime value)
new void SetOutput(Stream stream, bool ownsStream, Encoding encoding)
override void WriteUniqueIdText(UniqueId value)
override void WriteBoolText(bool value)
override void WriteDoubleText(double value)
override void WriteUInt64Text(ulong value)
override void WriteTimeSpanText(TimeSpan value)
override void WriteInt32Text(int value)
override void WriteDecimalText(decimal value)
override void WriteInt64Text(long value)
override void WriteBase64Text(byte[] trailBytes, int trailByteCount, byte[] buffer, int offset, int count)
override void WriteGuidText(Guid value)
override void WriteFloatText(float value)