root / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / JsonTextWriterTest.cs @ 0eea575a
History | View | Annotate | Download (18.6 kB)
1 |
#region License |
---|---|
2 |
// Copyright (c) 2007 James Newton-King |
3 |
// |
4 |
// Permission is hereby granted, free of charge, to any person |
5 |
// obtaining a copy of this software and associated documentation |
6 |
// files (the "Software"), to deal in the Software without |
7 |
// restriction, including without limitation the rights to use, |
8 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
9 |
// copies of the Software, and to permit persons to whom the |
10 |
// Software is furnished to do so, subject to the following |
11 |
// conditions: |
12 |
// |
13 |
// The above copyright notice and this permission notice shall be |
14 |
// included in all copies or substantial portions of the Software. |
15 |
// |
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
18 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
20 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
21 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
23 |
// OTHER DEALINGS IN THE SOFTWARE. |
24 |
#endregion |
25 |
|
26 |
using System; |
27 |
using System.Collections.Generic; |
28 |
using System.Text; |
29 |
using NUnit.Framework; |
30 |
using Newtonsoft.Json; |
31 |
using System.IO; |
32 |
|
33 |
namespace Newtonsoft.Json.Tests |
34 |
{ |
35 |
public class JsonTextWriterTest : TestFixtureBase |
36 |
{ |
37 |
[Test] |
38 |
public void CloseOutput() |
39 |
{ |
40 |
MemoryStream ms = new MemoryStream(); |
41 |
JsonTextWriter writer = new JsonTextWriter(new StreamWriter(ms)); |
42 |
|
43 |
Assert.IsTrue(ms.CanRead); |
44 |
writer.Close(); |
45 |
Assert.IsFalse(ms.CanRead); |
46 |
|
47 |
ms = new MemoryStream(); |
48 |
writer = new JsonTextWriter(new StreamWriter(ms)) { CloseOutput = false }; |
49 |
|
50 |
Assert.IsTrue(ms.CanRead); |
51 |
writer.Close(); |
52 |
Assert.IsTrue(ms.CanRead); |
53 |
} |
54 |
|
55 |
[Test] |
56 |
public void ValueFormatting() |
57 |
{ |
58 |
StringBuilder sb = new StringBuilder(); |
59 |
StringWriter sw = new StringWriter(sb); |
60 |
|
61 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
62 |
{ |
63 |
jsonWriter.WriteStartArray(); |
64 |
jsonWriter.WriteValue('@'); |
65 |
jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'"); |
66 |
jsonWriter.WriteValue(true); |
67 |
jsonWriter.WriteValue(10); |
68 |
jsonWriter.WriteValue(10.99); |
69 |
jsonWriter.WriteValue(0.99); |
70 |
jsonWriter.WriteValue(0.000000000000000001d); |
71 |
jsonWriter.WriteValue(0.000000000000000001m); |
72 |
jsonWriter.WriteValue((string)null); |
73 |
jsonWriter.WriteValue((object)null); |
74 |
jsonWriter.WriteValue("This is a string."); |
75 |
jsonWriter.WriteNull(); |
76 |
jsonWriter.WriteUndefined(); |
77 |
jsonWriter.WriteEndArray(); |
78 |
} |
79 |
|
80 |
string expected = @"[""@"",""\r\n\t\f\b?{\\r\\n\""'"",true,10,10.99,0.99,1E-18,0.000000000000000001,null,null,""This is a string."",null,undefined]"; |
81 |
string result = sb.ToString(); |
82 |
|
83 |
Console.WriteLine("ValueFormatting"); |
84 |
Console.WriteLine(result); |
85 |
|
86 |
Assert.AreEqual(expected, result); |
87 |
} |
88 |
|
89 |
[Test] |
90 |
public void NullableValueFormatting() |
91 |
{ |
92 |
StringWriter sw = new StringWriter(); |
93 |
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) |
94 |
{ |
95 |
jsonWriter.WriteStartArray(); |
96 |
jsonWriter.WriteValue((char?)null); |
97 |
jsonWriter.WriteValue((char?)'c'); |
98 |
jsonWriter.WriteValue((bool?)null); |
99 |
jsonWriter.WriteValue((bool?)true); |
100 |
jsonWriter.WriteValue((byte?)null); |
101 |
jsonWriter.WriteValue((byte?)1); |
102 |
jsonWriter.WriteValue((sbyte?)null); |
103 |
jsonWriter.WriteValue((sbyte?)1); |
104 |
jsonWriter.WriteValue((short?)null); |
105 |
jsonWriter.WriteValue((short?)1); |
106 |
jsonWriter.WriteValue((ushort?)null); |
107 |
jsonWriter.WriteValue((ushort?)1); |
108 |
jsonWriter.WriteValue((int?)null); |
109 |
jsonWriter.WriteValue((int?)1); |
110 |
jsonWriter.WriteValue((uint?)null); |
111 |
jsonWriter.WriteValue((uint?)1); |
112 |
jsonWriter.WriteValue((long?)null); |
113 |
jsonWriter.WriteValue((long?)1); |
114 |
jsonWriter.WriteValue((ulong?)null); |
115 |
jsonWriter.WriteValue((ulong?)1); |
116 |
jsonWriter.WriteValue((double?)null); |
117 |
jsonWriter.WriteValue((double?)1.1); |
118 |
jsonWriter.WriteValue((float?)null); |
119 |
jsonWriter.WriteValue((float?)1.1); |
120 |
jsonWriter.WriteValue((decimal?)null); |
121 |
jsonWriter.WriteValue((decimal?)1.1m); |
122 |
jsonWriter.WriteValue((DateTime?)null); |
123 |
jsonWriter.WriteValue((DateTime?)new DateTime(JsonConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc)); |
124 |
#if !PocketPC && !NET20 |
125 |
jsonWriter.WriteValue((DateTimeOffset?)null); |
126 |
jsonWriter.WriteValue((DateTimeOffset?)new DateTimeOffset(JsonConvert.InitialJavaScriptDateTicks, TimeSpan.Zero)); |
127 |
#endif |
128 |
jsonWriter.WriteEndArray(); |
129 |
} |
130 |
|
131 |
string json = sw.ToString(); |
132 |
string expected; |
133 |
|
134 |
#if !PocketPC && !NET20 |
135 |
expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""\/Date(0)\/"",null,""\/Date(0+0000)\/""]"; |
136 |
#else |
137 |
expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""\/Date(0)\/""]"; |
138 |
#endif |
139 |
|
140 |
Assert.AreEqual(expected, json); |
141 |
} |
142 |
|
143 |
[Test] |
144 |
public void WriteValueObjectWithNullable() |
145 |
{ |
146 |
StringWriter sw = new StringWriter(); |
147 |
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) |
148 |
{ |
149 |
char? value = 'c'; |
150 |
|
151 |
jsonWriter.WriteStartArray(); |
152 |
jsonWriter.WriteValue((object)value); |
153 |
jsonWriter.WriteEndArray(); |
154 |
} |
155 |
|
156 |
string json = sw.ToString(); |
157 |
string expected = @"[""c""]"; |
158 |
|
159 |
Assert.AreEqual(expected, json); |
160 |
} |
161 |
|
162 |
[Test] |
163 |
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.")] |
164 |
public void WriteValueObjectWithUnsupportedValue() |
165 |
{ |
166 |
StringWriter sw = new StringWriter(); |
167 |
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) |
168 |
{ |
169 |
jsonWriter.WriteStartArray(); |
170 |
jsonWriter.WriteValue(new Version(1, 1, 1, 1)); |
171 |
jsonWriter.WriteEndArray(); |
172 |
} |
173 |
} |
174 |
|
175 |
[Test] |
176 |
public void StringEscaping() |
177 |
{ |
178 |
StringBuilder sb = new StringBuilder(); |
179 |
StringWriter sw = new StringWriter(sb); |
180 |
|
181 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
182 |
{ |
183 |
jsonWriter.WriteStartArray(); |
184 |
jsonWriter.WriteValue(@"""These pretzels are making me thirsty!"""); |
185 |
jsonWriter.WriteValue("Jeff's house was burninated."); |
186 |
jsonWriter.WriteValue(@"1. You don't talk about fight club. |
187 |
2. You don't talk about fight club."); |
188 |
jsonWriter.WriteValue("35% of\t statistics\n are made\r up."); |
189 |
jsonWriter.WriteEndArray(); |
190 |
} |
191 |
|
192 |
string expected = @"[""\""These pretzels are making me thirsty!\"""",""Jeff's house was burninated."",""1. You don't talk about fight club.\r\n2. You don't talk about fight club."",""35% of\t statistics\n are made\r up.""]"; |
193 |
string result = sb.ToString(); |
194 |
|
195 |
Console.WriteLine("StringEscaping"); |
196 |
Console.WriteLine(result); |
197 |
|
198 |
Assert.AreEqual(expected, result); |
199 |
} |
200 |
|
201 |
[Test] |
202 |
public void Indenting() |
203 |
{ |
204 |
StringBuilder sb = new StringBuilder(); |
205 |
StringWriter sw = new StringWriter(sb); |
206 |
|
207 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
208 |
{ |
209 |
jsonWriter.Formatting = Formatting.Indented; |
210 |
|
211 |
jsonWriter.WriteStartObject(); |
212 |
jsonWriter.WritePropertyName("CPU"); |
213 |
jsonWriter.WriteValue("Intel"); |
214 |
jsonWriter.WritePropertyName("PSU"); |
215 |
jsonWriter.WriteValue("500W"); |
216 |
jsonWriter.WritePropertyName("Drives"); |
217 |
jsonWriter.WriteStartArray(); |
218 |
jsonWriter.WriteValue("DVD read/writer"); |
219 |
jsonWriter.WriteComment("(broken)"); |
220 |
jsonWriter.WriteValue("500 gigabyte hard drive"); |
221 |
jsonWriter.WriteValue("200 gigabype hard drive"); |
222 |
jsonWriter.WriteEnd(); |
223 |
jsonWriter.WriteEndObject(); |
224 |
} |
225 |
|
226 |
// { |
227 |
// "CPU": "Intel", |
228 |
// "PSU": "500W", |
229 |
// "Drives": [ |
230 |
// "DVD read/writer" |
231 |
// /*(broken)*/, |
232 |
// "500 gigabyte hard drive", |
233 |
// "200 gigabype hard drive" |
234 |
// ] |
235 |
// } |
236 |
|
237 |
string expected = @"{ |
238 |
""CPU"": ""Intel"", |
239 |
""PSU"": ""500W"", |
240 |
""Drives"": [ |
241 |
""DVD read/writer"" |
242 |
/*(broken)*/, |
243 |
""500 gigabyte hard drive"", |
244 |
""200 gigabype hard drive"" |
245 |
] |
246 |
}"; |
247 |
string result = sb.ToString(); |
248 |
|
249 |
Console.WriteLine("Indenting"); |
250 |
Console.WriteLine(result); |
251 |
|
252 |
Assert.AreEqual(expected, result); |
253 |
} |
254 |
|
255 |
[Test] |
256 |
public void State() |
257 |
{ |
258 |
StringBuilder sb = new StringBuilder(); |
259 |
StringWriter sw = new StringWriter(sb); |
260 |
|
261 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
262 |
{ |
263 |
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState); |
264 |
|
265 |
jsonWriter.WriteStartObject(); |
266 |
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState); |
267 |
|
268 |
jsonWriter.WritePropertyName("CPU"); |
269 |
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState); |
270 |
|
271 |
jsonWriter.WriteValue("Intel"); |
272 |
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState); |
273 |
|
274 |
jsonWriter.WritePropertyName("Drives"); |
275 |
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState); |
276 |
|
277 |
jsonWriter.WriteStartArray(); |
278 |
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState); |
279 |
|
280 |
jsonWriter.WriteValue("DVD read/writer"); |
281 |
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState); |
282 |
|
283 |
jsonWriter.WriteEnd(); |
284 |
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState); |
285 |
|
286 |
jsonWriter.WriteEndObject(); |
287 |
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState); |
288 |
} |
289 |
} |
290 |
|
291 |
[Test] |
292 |
public void FloatingPointNonFiniteNumbers() |
293 |
{ |
294 |
StringBuilder sb = new StringBuilder(); |
295 |
StringWriter sw = new StringWriter(sb); |
296 |
|
297 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
298 |
{ |
299 |
jsonWriter.Formatting = Formatting.Indented; |
300 |
|
301 |
jsonWriter.WriteStartArray(); |
302 |
jsonWriter.WriteValue(double.NaN); |
303 |
jsonWriter.WriteValue(double.PositiveInfinity); |
304 |
jsonWriter.WriteValue(double.NegativeInfinity); |
305 |
jsonWriter.WriteValue(float.NaN); |
306 |
jsonWriter.WriteValue(float.PositiveInfinity); |
307 |
jsonWriter.WriteValue(float.NegativeInfinity); |
308 |
jsonWriter.WriteEndArray(); |
309 |
|
310 |
jsonWriter.Flush(); |
311 |
} |
312 |
|
313 |
string expected = @"[ |
314 |
NaN, |
315 |
Infinity, |
316 |
-Infinity, |
317 |
NaN, |
318 |
Infinity, |
319 |
-Infinity |
320 |
]"; |
321 |
string result = sb.ToString(); |
322 |
|
323 |
Assert.AreEqual(expected, result); |
324 |
} |
325 |
|
326 |
[Test] |
327 |
public void WriteRawInStart() |
328 |
{ |
329 |
StringBuilder sb = new StringBuilder(); |
330 |
StringWriter sw = new StringWriter(sb); |
331 |
|
332 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
333 |
{ |
334 |
jsonWriter.Formatting = Formatting.Indented; |
335 |
|
336 |
jsonWriter.WriteRaw("[1,2,3,4,5]"); |
337 |
jsonWriter.WriteWhitespace(" "); |
338 |
jsonWriter.WriteStartArray(); |
339 |
jsonWriter.WriteValue(double.NaN); |
340 |
jsonWriter.WriteEndArray(); |
341 |
} |
342 |
|
343 |
string expected = @"[1,2,3,4,5] [ |
344 |
NaN |
345 |
]"; |
346 |
string result = sb.ToString(); |
347 |
|
348 |
Assert.AreEqual(expected, result); |
349 |
} |
350 |
|
351 |
[Test] |
352 |
public void WriteRawInArray() |
353 |
{ |
354 |
StringBuilder sb = new StringBuilder(); |
355 |
StringWriter sw = new StringWriter(sb); |
356 |
|
357 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
358 |
{ |
359 |
jsonWriter.Formatting = Formatting.Indented; |
360 |
|
361 |
jsonWriter.WriteStartArray(); |
362 |
jsonWriter.WriteValue(double.NaN); |
363 |
jsonWriter.WriteRaw(",[1,2,3,4,5]"); |
364 |
jsonWriter.WriteRaw(",[1,2,3,4,5]"); |
365 |
jsonWriter.WriteValue(float.NaN); |
366 |
jsonWriter.WriteEndArray(); |
367 |
} |
368 |
|
369 |
string expected = @"[ |
370 |
NaN,[1,2,3,4,5],[1,2,3,4,5], |
371 |
NaN |
372 |
]"; |
373 |
string result = sb.ToString(); |
374 |
|
375 |
Assert.AreEqual(expected, result); |
376 |
} |
377 |
|
378 |
[Test] |
379 |
public void WriteRawInObject() |
380 |
{ |
381 |
StringBuilder sb = new StringBuilder(); |
382 |
StringWriter sw = new StringWriter(sb); |
383 |
|
384 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
385 |
{ |
386 |
jsonWriter.Formatting = Formatting.Indented; |
387 |
|
388 |
jsonWriter.WriteStartObject(); |
389 |
jsonWriter.WriteRaw(@"""PropertyName"":[1,2,3,4,5]"); |
390 |
jsonWriter.WriteEnd(); |
391 |
} |
392 |
|
393 |
string expected = @"{""PropertyName"":[1,2,3,4,5]}"; |
394 |
string result = sb.ToString(); |
395 |
|
396 |
Assert.AreEqual(expected, result); |
397 |
} |
398 |
|
399 |
[Test] |
400 |
public void WriteToken() |
401 |
{ |
402 |
JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]")); |
403 |
reader.Read(); |
404 |
reader.Read(); |
405 |
|
406 |
StringWriter sw = new StringWriter(); |
407 |
JsonTextWriter writer = new JsonTextWriter(sw); |
408 |
writer.WriteToken(reader); |
409 |
|
410 |
Assert.AreEqual("1", sw.ToString()); |
411 |
} |
412 |
|
413 |
[Test] |
414 |
public void WriteRawValue() |
415 |
{ |
416 |
StringBuilder sb = new StringBuilder(); |
417 |
StringWriter sw = new StringWriter(sb); |
418 |
|
419 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
420 |
{ |
421 |
int i = 0; |
422 |
string rawJson = "[1,2]"; |
423 |
|
424 |
jsonWriter.WriteStartObject(); |
425 |
|
426 |
while (i < 3) |
427 |
{ |
428 |
jsonWriter.WritePropertyName("d" + i); |
429 |
jsonWriter.WriteRawValue(rawJson); |
430 |
|
431 |
i++; |
432 |
} |
433 |
|
434 |
jsonWriter.WriteEndObject(); |
435 |
} |
436 |
|
437 |
Assert.AreEqual(@"{""d0"":[1,2],""d1"":[1,2],""d2"":[1,2]}", sb.ToString()); |
438 |
} |
439 |
|
440 |
[Test] |
441 |
public void WriteObjectNestedInConstructor() |
442 |
{ |
443 |
StringBuilder sb = new StringBuilder(); |
444 |
StringWriter sw = new StringWriter(sb); |
445 |
|
446 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
447 |
{ |
448 |
jsonWriter.WriteStartObject(); |
449 |
jsonWriter.WritePropertyName("con"); |
450 |
|
451 |
jsonWriter.WriteStartConstructor("Ext.data.JsonStore"); |
452 |
jsonWriter.WriteStartObject(); |
453 |
jsonWriter.WritePropertyName("aa"); |
454 |
jsonWriter.WriteValue("aa"); |
455 |
jsonWriter.WriteEndObject(); |
456 |
jsonWriter.WriteEndConstructor(); |
457 |
|
458 |
jsonWriter.WriteEndObject(); |
459 |
} |
460 |
|
461 |
Assert.AreEqual(@"{""con"":new Ext.data.JsonStore({""aa"":""aa""})}", sb.ToString()); |
462 |
} |
463 |
|
464 |
[Test] |
465 |
public void WriteFloatingPointNumber() |
466 |
{ |
467 |
StringBuilder sb = new StringBuilder(); |
468 |
StringWriter sw = new StringWriter(sb); |
469 |
|
470 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
471 |
{ |
472 |
jsonWriter.WriteStartArray(); |
473 |
|
474 |
jsonWriter.WriteValue(0.0); |
475 |
jsonWriter.WriteValue(0f); |
476 |
jsonWriter.WriteValue(0.1); |
477 |
jsonWriter.WriteValue(1.0); |
478 |
jsonWriter.WriteValue(1.000001); |
479 |
jsonWriter.WriteValue(0.000001); |
480 |
jsonWriter.WriteValue(double.Epsilon); |
481 |
jsonWriter.WriteValue(double.PositiveInfinity); |
482 |
jsonWriter.WriteValue(double.NegativeInfinity); |
483 |
jsonWriter.WriteValue(double.NaN); |
484 |
jsonWriter.WriteValue(double.MaxValue); |
485 |
jsonWriter.WriteValue(double.MinValue); |
486 |
jsonWriter.WriteValue(float.PositiveInfinity); |
487 |
jsonWriter.WriteValue(float.NegativeInfinity); |
488 |
jsonWriter.WriteValue(float.NaN); |
489 |
|
490 |
jsonWriter.WriteEndArray(); |
491 |
} |
492 |
|
493 |
Assert.AreEqual(@"[0.0,0.0,0.1,1.0,1.000001,1E-06,4.94065645841247E-324,Infinity,-Infinity,NaN,1.7976931348623157E+308,-1.7976931348623157E+308,Infinity,-Infinity,NaN]", sb.ToString()); |
494 |
} |
495 |
|
496 |
[Test] |
497 |
[ExpectedException(typeof(JsonWriterException), ExpectedMessage = "No token to close.")] |
498 |
public void BadWriteEndArray() |
499 |
{ |
500 |
StringBuilder sb = new StringBuilder(); |
501 |
StringWriter sw = new StringWriter(sb); |
502 |
|
503 |
using (JsonWriter jsonWriter = new JsonTextWriter(sw)) |
504 |
{ |
505 |
jsonWriter.WriteStartArray(); |
506 |
|
507 |
jsonWriter.WriteValue(0.0); |
508 |
|
509 |
jsonWriter.WriteEndArray(); |
510 |
jsonWriter.WriteEndArray(); |
511 |
} |
512 |
} |
513 |
|
514 |
[Test] |
515 |
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Invalid JavaScript string quote character. Valid quote characters are ' and "".")] |
516 |
public void InvalidQuoteChar() |
517 |
{ |
518 |
StringBuilder sb = new StringBuilder(); |
519 |
StringWriter sw = new StringWriter(sb); |
520 |
|
521 |
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) |
522 |
{ |
523 |
jsonWriter.Formatting = Formatting.Indented; |
524 |
jsonWriter.QuoteChar = '*'; |
525 |
} |
526 |
} |
527 |
|
528 |
[Test] |
529 |
public void Indentation() |
530 |
{ |
531 |
StringBuilder sb = new StringBuilder(); |
532 |
StringWriter sw = new StringWriter(sb); |
533 |
|
534 |
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) |
535 |
{ |
536 |
jsonWriter.Formatting = Formatting.Indented; |
537 |
Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting); |
538 |
|
539 |
jsonWriter.Indentation = 5; |
540 |
Assert.AreEqual(5, jsonWriter.Indentation); |
541 |
jsonWriter.IndentChar = '_'; |
542 |
Assert.AreEqual('_', jsonWriter.IndentChar); |
543 |
jsonWriter.QuoteName = true; |
544 |
Assert.AreEqual(true, jsonWriter.QuoteName); |
545 |
jsonWriter.QuoteChar = '\''; |
546 |
Assert.AreEqual('\'', jsonWriter.QuoteChar); |
547 |
|
548 |
jsonWriter.WriteStartObject(); |
549 |
jsonWriter.WritePropertyName("propertyName"); |
550 |
jsonWriter.WriteValue(double.NaN); |
551 |
jsonWriter.WriteEndObject(); |
552 |
} |
553 |
|
554 |
string expected = @"{ |
555 |
_____'propertyName': NaN |
556 |
}"; |
557 |
string result = sb.ToString(); |
558 |
|
559 |
Assert.AreEqual(expected, result); |
560 |
} |
561 |
|
562 |
[Test] |
563 |
public void WriteSingleBytes() |
564 |
{ |
565 |
StringBuilder sb = new StringBuilder(); |
566 |
StringWriter sw = new StringWriter(sb); |
567 |
|
568 |
string text = "Hello world."; |
569 |
byte[] data = Encoding.UTF8.GetBytes(text); |
570 |
|
571 |
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) |
572 |
{ |
573 |
jsonWriter.Formatting = Formatting.Indented; |
574 |
Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting); |
575 |
|
576 |
jsonWriter.WriteValue(data); |
577 |
} |
578 |
|
579 |
string expected = @"""SGVsbG8gd29ybGQu"""; |
580 |
string result = sb.ToString(); |
581 |
|
582 |
Assert.AreEqual(expected, result); |
583 |
|
584 |
byte[] d2 = Convert.FromBase64String(result.Trim('"')); |
585 |
|
586 |
Assert.AreEqual(text, Encoding.UTF8.GetString(d2, 0, d2.Length)); |
587 |
} |
588 |
|
589 |
[Test] |
590 |
public void WriteBytesInArray() |
591 |
{ |
592 |
StringBuilder sb = new StringBuilder(); |
593 |
StringWriter sw = new StringWriter(sb); |
594 |
|
595 |
string text = "Hello world."; |
596 |
byte[] data = Encoding.UTF8.GetBytes(text); |
597 |
|
598 |
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) |
599 |
{ |
600 |
jsonWriter.Formatting = Formatting.Indented; |
601 |
Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting); |
602 |
|
603 |
jsonWriter.WriteStartArray(); |
604 |
jsonWriter.WriteValue(data); |
605 |
jsonWriter.WriteValue(data); |
606 |
jsonWriter.WriteValue((object)data); |
607 |
jsonWriter.WriteValue((byte[])null); |
608 |
jsonWriter.WriteEndArray(); |
609 |
} |
610 |
|
611 |
string expected = @"[ |
612 |
""SGVsbG8gd29ybGQu"", |
613 |
""SGVsbG8gd29ybGQu"", |
614 |
""SGVsbG8gd29ybGQu"", |
615 |
null |
616 |
]"; |
617 |
string result = sb.ToString(); |
618 |
|
619 |
Assert.AreEqual(expected, result); |
620 |
} |
621 |
} |
622 |
} |