Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\JsonTextWriterTest.cs

Symbol Coverage: 98.73% (311 of 315)

Branch Coverage: 92.86% (39 of 42)

Cyclomatic Complexity Avg: 1.95 Max:3

Code Lines: 344


L V Source
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 ValueFormatting()
39
    {
40
 1
      StringBuilder sb = new StringBuilder();
41
 1
      StringWriter sw = new StringWriter(sb);
42

  
43
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
44
      {
45
 1
        jsonWriter.WriteStartArray();
46
 1
        jsonWriter.WriteValue('@');
47
 1
        jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
48
 1
        jsonWriter.WriteValue(true);
49
 1
        jsonWriter.WriteValue(10);
50
 1
        jsonWriter.WriteValue(10.99);
51
 1
        jsonWriter.WriteValue(0.99);
52
 1
        jsonWriter.WriteValue(0.000000000000000001d);
53
 1
        jsonWriter.WriteValue(0.000000000000000001m);
54
 1
        jsonWriter.WriteValue((string)null);
55
 1
        jsonWriter.WriteValue((object)null);
56
 1
        jsonWriter.WriteValue("This is a string.");
57
 1
        jsonWriter.WriteNull();
58
 1
        jsonWriter.WriteUndefined();
59
 1
        jsonWriter.WriteEndArray();
60
      }
61

  
62
 1
      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]";
63
 1
      string result = sb.ToString();
64

  
65
 1
      Console.WriteLine("ValueFormatting");
66
 1
      Console.WriteLine(result);
67

  
68
 1
      Assert.AreEqual(expected, result);
69
 1
    }
70

  
71
    [Test]
72
    public void NullableValueFormatting()
73
    {
74
 1
      StringWriter sw = new StringWriter();
75
 1
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
76
      {
77
 1
        jsonWriter.WriteStartArray();
78
 1
        jsonWriter.WriteValue((char?)null);
79
 1
        jsonWriter.WriteValue((char?)'c');
80
 1
        jsonWriter.WriteValue((bool?)null);
81
 1
        jsonWriter.WriteValue((bool?)true);
82
 1
        jsonWriter.WriteValue((byte?)null);
83
 1
        jsonWriter.WriteValue((byte?)1);
84
 1
        jsonWriter.WriteValue((sbyte?)null);
85
 1
        jsonWriter.WriteValue((sbyte?)1);
86
 1
        jsonWriter.WriteValue((short?)null);
87
 1
        jsonWriter.WriteValue((short?)1);
88
 1
        jsonWriter.WriteValue((ushort?)null);
89
 1
        jsonWriter.WriteValue((ushort?)1);
90
 1
        jsonWriter.WriteValue((int?)null);
91
 1
        jsonWriter.WriteValue((int?)1);
92
 1
        jsonWriter.WriteValue((uint?)null);
93
 1
        jsonWriter.WriteValue((uint?)1);
94
 1
        jsonWriter.WriteValue((long?)null);
95
 1
        jsonWriter.WriteValue((long?)1);
96
 1
        jsonWriter.WriteValue((ulong?)null);
97
 1
        jsonWriter.WriteValue((ulong?)1);
98
 1
        jsonWriter.WriteValue((double?)null);
99
 1
        jsonWriter.WriteValue((double?)1.1);
100
 1
        jsonWriter.WriteValue((float?)null);
101
 1
        jsonWriter.WriteValue((float?)1.1);
102
 1
        jsonWriter.WriteValue((decimal?)null);
103
 1
        jsonWriter.WriteValue((decimal?)1.1m);
104
 1
        jsonWriter.WriteValue((DateTime?)null);
105
 1
        jsonWriter.WriteValue((DateTime?)new DateTime(JsonConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc));
106
#if !PocketPC && !NET20
107
 1
        jsonWriter.WriteValue((DateTimeOffset?)null);
108
 1
        jsonWriter.WriteValue((DateTimeOffset?)new DateTimeOffset(JsonConvert.InitialJavaScriptDateTicks, TimeSpan.Zero));
109
#endif
110
 1
        jsonWriter.WriteEndArray();
111
      }
112

  
113
 1
      string json = sw.ToString();
114
      string expected;
115

  
116
#if !PocketPC && !NET20
117
 1
      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)\/""]";
118
#else
119
      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)\/""]";
120
#endif
121

  
122
 1
      Assert.AreEqual(expected, json);
123
 1
    }
124

  
125
    [Test]
126
    public void WriteValueObjectWithNullable()
127
    {
128
 1
      StringWriter sw = new StringWriter();
129
 1
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
130
      {
131
 1
        char? value = 'c';
132

  
133
 1
        jsonWriter.WriteStartArray();
134
 1
        jsonWriter.WriteValue((object)value);
135
 1
        jsonWriter.WriteEndArray();
136
      }
137

  
138
 1
      string json = sw.ToString();
139
 1
      string expected = @"[""c""]";
140

  
141
 1
      Assert.AreEqual(expected, json);
142
 1
    }
143

  
144
    [Test]
145
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.")]
146
    public void WriteValueObjectWithUnsupportedValue()
147
    {
148
 1
      StringWriter sw = new StringWriter();
149
 1
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
150
      {
151
 1
        jsonWriter.WriteStartArray();
152
 1
        jsonWriter.WriteValue(new Version(1, 1, 1, 1));
153
 0
        jsonWriter.WriteEndArray();
154
      }
155
 0
    }
156

  
157
    [Test]
158
    public void StringEscaping()
159
    {
160
 1
      StringBuilder sb = new StringBuilder();
161
 1
      StringWriter sw = new StringWriter(sb);
162

  
163
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
164
      {
165
 1
        jsonWriter.WriteStartArray();
166
 1
        jsonWriter.WriteValue(@"""These pretzels are making me thirsty!""");
167
 1
        jsonWriter.WriteValue("Jeff's house was burninated.");
168
 1
        jsonWriter.WriteValue(@"1. You don't talk about fight club.
169
 1
2. You don't talk about fight club.");
170
 1
        jsonWriter.WriteValue("35% of\t statistics\n are made\r up.");
171
 1
        jsonWriter.WriteEndArray();
172
      }
173

  
174
 1
      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.""]";
175
 1
      string result = sb.ToString();
176

  
177
 1
      Console.WriteLine("StringEscaping");
178
 1
      Console.WriteLine(result);
179

  
180
 1
      Assert.AreEqual(expected, result);
181
 1
    }
182

  
183
    [Test]
184
    public void Indenting()
185
    {
186
 1
      StringBuilder sb = new StringBuilder();
187
 1
      StringWriter sw = new StringWriter(sb);
188

  
189
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
190
      {
191
 1
        jsonWriter.Formatting = Formatting.Indented;
192

  
193
 1
        jsonWriter.WriteStartObject();
194
 1
        jsonWriter.WritePropertyName("CPU");
195
 1
        jsonWriter.WriteValue("Intel");
196
 1
        jsonWriter.WritePropertyName("PSU");
197
 1
        jsonWriter.WriteValue("500W");
198
 1
        jsonWriter.WritePropertyName("Drives");
199
 1
        jsonWriter.WriteStartArray();
200
 1
        jsonWriter.WriteValue("DVD read/writer");
201
 1
        jsonWriter.WriteComment("(broken)");
202
 1
        jsonWriter.WriteValue("500 gigabyte hard drive");
203
 1
        jsonWriter.WriteValue("200 gigabype hard drive");
204
 1
        jsonWriter.WriteEnd();
205
 1
        jsonWriter.WriteEndObject();
206
      }
207

  
208
      // {
209
      //   "CPU": "Intel",
210
      //   "PSU": "500W",
211
      //   "Drives": [
212
      //     "DVD read/writer"
213
      //     /*(broken)*/,
214
      //     "500 gigabyte hard drive",
215
      //     "200 gigabype hard drive"
216
      //   ]
217
      // }
218

  
219
 1
      string expected = @"{
220
 1
  ""CPU"": ""Intel"",
221
 1
  ""PSU"": ""500W"",
222
 1
  ""Drives"": [
223
 1
    ""DVD read/writer""
224
 1
    /*(broken)*/,
225
 1
    ""500 gigabyte hard drive"",
226
 1
    ""200 gigabype hard drive""
227
 1
  ]
228
 1
}";
229
 1
      string result = sb.ToString();
230

  
231
 1
      Console.WriteLine("Indenting");
232
 1
      Console.WriteLine(result);
233

  
234
 1
      Assert.AreEqual(expected, result);
235
 1
    }
236

  
237
    [Test]
238
    public void State()
239
    {
240
 1
      StringBuilder sb = new StringBuilder();
241
 1
      StringWriter sw = new StringWriter(sb);
242

  
243
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
244
      {
245
 1
        Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
246

  
247
 1
        jsonWriter.WriteStartObject();
248
 1
        Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
249

  
250
 1
        jsonWriter.WritePropertyName("CPU");
251
 1
        Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
252

  
253
 1
        jsonWriter.WriteValue("Intel");
254
 1
        Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
255

  
256
 1
        jsonWriter.WritePropertyName("Drives");
257
 1
        Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
258

  
259
 1
        jsonWriter.WriteStartArray();
260
 1
        Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
261

  
262
 1
        jsonWriter.WriteValue("DVD read/writer");
263
 1
        Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
264

  
265
 1
        jsonWriter.WriteEnd();
266
 1
        Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
267

  
268
 1
        jsonWriter.WriteEndObject();
269
 1
        Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
270
      }
271
 1
    }
272

  
273
    [Test]
274
    public void FloatingPointNonFiniteNumbers()
275
    {
276
 1
      StringBuilder sb = new StringBuilder();
277
 1
      StringWriter sw = new StringWriter(sb);
278

  
279
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
280
      {
281
 1
        jsonWriter.Formatting = Formatting.Indented;
282

  
283
 1
        jsonWriter.WriteStartArray();
284
 1
        jsonWriter.WriteValue(double.NaN);
285
 1
        jsonWriter.WriteValue(double.PositiveInfinity);
286
 1
        jsonWriter.WriteValue(double.NegativeInfinity);
287
 1
        jsonWriter.WriteValue(float.NaN);
288
 1
        jsonWriter.WriteValue(float.PositiveInfinity);
289
 1
        jsonWriter.WriteValue(float.NegativeInfinity);
290
 1
        jsonWriter.WriteEndArray();
291

  
292
 1
        jsonWriter.Flush();
293
      }
294

  
295
 1
      string expected = @"[
296
 1
  NaN,
297
 1
  Infinity,
298
 1
  -Infinity,
299
 1
  NaN,
300
 1
  Infinity,
301
 1
  -Infinity
302
 1
]";
303
 1
      string result = sb.ToString();
304

  
305
 1
      Assert.AreEqual(expected, result);
306
 1
    }
307

  
308
    [Test]
309
    public void WriteRawInStart()
310
    {
311
 1
      StringBuilder sb = new StringBuilder();
312
 1
      StringWriter sw = new StringWriter(sb);
313

  
314
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
315
      {
316
 1
        jsonWriter.Formatting = Formatting.Indented;
317

  
318
 1
        jsonWriter.WriteRaw("[1,2,3,4,5]");
319
 1
        jsonWriter.WriteWhitespace("  ");
320
 1
        jsonWriter.WriteStartArray();
321
 1
        jsonWriter.WriteValue(double.NaN);
322
 1
        jsonWriter.WriteEndArray();
323
      }
324

  
325
 1
      string expected = @"[1,2,3,4,5]  [
326
 1
  NaN
327
 1
]";
328
 1
      string result = sb.ToString();
329

  
330
 1
      Assert.AreEqual(expected, result);
331
 1
    }
332

  
333
    [Test]
334
    public void WriteRawInArray()
335
    {
336
 1
      StringBuilder sb = new StringBuilder();
337
 1
      StringWriter sw = new StringWriter(sb);
338

  
339
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
340
      {
341
 1
        jsonWriter.Formatting = Formatting.Indented;
342

  
343
 1
        jsonWriter.WriteStartArray();
344
 1
        jsonWriter.WriteValue(double.NaN);
345
 1
        jsonWriter.WriteRaw(",[1,2,3,4,5]");
346
 1
        jsonWriter.WriteRaw(",[1,2,3,4,5]");
347
 1
        jsonWriter.WriteValue(float.NaN);
348
 1
        jsonWriter.WriteEndArray();
349
      }
350

  
351
 1
      string expected = @"[
352
 1
  NaN,[1,2,3,4,5],[1,2,3,4,5],
353
 1
  NaN
354
 1
]";
355
 1
      string result = sb.ToString();
356

  
357
 1
      Assert.AreEqual(expected, result);
358
 1
    }
359

  
360
    [Test]
361
    public void WriteRawInObject()
362
    {
363
 1
      StringBuilder sb = new StringBuilder();
364
 1
      StringWriter sw = new StringWriter(sb);
365

  
366
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
367
      {
368
 1
        jsonWriter.Formatting = Formatting.Indented;
369

  
370
 1
        jsonWriter.WriteStartObject();
371
 1
        jsonWriter.WriteRaw(@"""PropertyName"":[1,2,3,4,5]");
372
 1
        jsonWriter.WriteEnd();
373
      }
374

  
375
 1
      string expected = @"{""PropertyName"":[1,2,3,4,5]}";
376
 1
      string result = sb.ToString();
377

  
378
 1
      Assert.AreEqual(expected, result);
379
 1
    }
380

  
381
    [Test]
382
    public void WriteToken()
383
    {
384
 1
      JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]"));
385
 1
      reader.Read();
386
 1
      reader.Read();
387

  
388
 1
      StringWriter sw = new StringWriter();
389
 1
      JsonTextWriter writer = new JsonTextWriter(sw);
390
 1
      writer.WriteToken(reader);
391

  
392
 1
      Assert.AreEqual("1", sw.ToString());
393
 1
    }
394

  
395
    [Test]
396
    public void WriteRawValue()
397
    {
398
 1
      StringBuilder sb = new StringBuilder();
399
 1
      StringWriter sw = new StringWriter(sb);
400

  
401
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
402
      {
403
 1
        int i = 0;
404
 1
        string rawJson = "[1,2]";
405

  
406
 1
        jsonWriter.WriteStartObject();
407

  
408
 4
        while (i < 3)
409
        {
410
 3
          jsonWriter.WritePropertyName("d" + i);
411
 3
          jsonWriter.WriteRawValue(rawJson);
412

  
413
 3
          i++;
414
        }
415

  
416
 1
        jsonWriter.WriteEndObject();
417
      }
418

  
419
 1
      Assert.AreEqual(@"{""d0"":[1,2],""d1"":[1,2],""d2"":[1,2]}", sb.ToString());
420
 1
    }
421

  
422
    [Test]
423
    public void WriteObjectNestedInConstructor()
424
    {
425
 1
      StringBuilder sb = new StringBuilder();
426
 1
      StringWriter sw = new StringWriter(sb);
427

  
428
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
429
      {
430
 1
        jsonWriter.WriteStartObject();
431
 1
        jsonWriter.WritePropertyName("con");
432

  
433
 1
        jsonWriter.WriteStartConstructor("Ext.data.JsonStore");
434
 1
        jsonWriter.WriteStartObject();
435
 1
        jsonWriter.WritePropertyName("aa");
436
 1
        jsonWriter.WriteValue("aa");
437
 1
        jsonWriter.WriteEndObject();
438
 1
        jsonWriter.WriteEndConstructor();
439

  
440
 1
        jsonWriter.WriteEndObject();
441
      }
442

  
443
 1
      Assert.AreEqual(@"{""con"":new Ext.data.JsonStore({""aa"":""aa""})}", sb.ToString());
444
 1
    }
445

  
446
    [Test]
447
    public void WriteFloatingPointNumber()
448
    {
449
 1
      StringBuilder sb = new StringBuilder();
450
 1
      StringWriter sw = new StringWriter(sb);
451

  
452
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
453
      {
454
 1
        jsonWriter.WriteStartArray();
455

  
456
 1
        jsonWriter.WriteValue(0.0);
457
 1
        jsonWriter.WriteValue(0f);
458
 1
        jsonWriter.WriteValue(0.1);
459
 1
        jsonWriter.WriteValue(1.0);
460
 1
        jsonWriter.WriteValue(1.000001);
461
 1
        jsonWriter.WriteValue(0.000001);
462
 1
        jsonWriter.WriteValue(double.Epsilon);
463
 1
        jsonWriter.WriteValue(double.PositiveInfinity);
464
 1
        jsonWriter.WriteValue(double.NegativeInfinity);
465
 1
        jsonWriter.WriteValue(double.NaN);
466
 1
        jsonWriter.WriteValue(double.MaxValue);
467
 1
        jsonWriter.WriteValue(double.MinValue);
468
 1
        jsonWriter.WriteValue(float.PositiveInfinity);
469
 1
        jsonWriter.WriteValue(float.NegativeInfinity);
470
 1
        jsonWriter.WriteValue(float.NaN);
471

  
472
 1
        jsonWriter.WriteEndArray();
473
      }
474

  
475
 1
      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());
476
 1
    }
477

  
478
    [Test]
479
    [ExpectedException(typeof(JsonWriterException), ExpectedMessage = "No token to close.")]
480
    public void BadWriteEndArray()
481
    {
482
 1
      StringBuilder sb = new StringBuilder();
483
 1
      StringWriter sw = new StringWriter(sb);
484

  
485
 1
      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
486
      {
487
 1
        jsonWriter.WriteStartArray();
488

  
489
 1
        jsonWriter.WriteValue(0.0);
490

  
491
 1
        jsonWriter.WriteEndArray();
492
 1
        jsonWriter.WriteEndArray();
493
      }
494
 0
    }
495

  
496
    [Test]
497
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Invalid JavaScript string quote character. Valid quote characters are ' and "".")]
498
    public void InvalidQuoteChar()
499
    {
500
 1
      StringBuilder sb = new StringBuilder();
501
 1
      StringWriter sw = new StringWriter(sb);
502

  
503
 1
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
504
      {
505
 1
        jsonWriter.Formatting = Formatting.Indented;
506
 1
        jsonWriter.QuoteChar = '*';
507
      }
508
 0
    }
509

  
510
    [Test]
511
    public void Indentation()
512
    {
513
 1
      StringBuilder sb = new StringBuilder();
514
 1
      StringWriter sw = new StringWriter(sb);
515

  
516
 1
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
517
      {
518
 1
        jsonWriter.Formatting = Formatting.Indented;
519
 1
        Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting);
520

  
521
 1
        jsonWriter.Indentation = 5;
522
 1
        Assert.AreEqual(5, jsonWriter.Indentation);
523
 1
        jsonWriter.IndentChar = '_';
524
 1
        Assert.AreEqual('_', jsonWriter.IndentChar);
525
 1
        jsonWriter.QuoteName = true;
526
 1
        Assert.AreEqual(true, jsonWriter.QuoteName);
527
 1
        jsonWriter.QuoteChar = '\'';
528
 1
        Assert.AreEqual('\'', jsonWriter.QuoteChar);
529

  
530
 1
        jsonWriter.WriteStartObject();
531
 1
        jsonWriter.WritePropertyName("propertyName");
532
 1
        jsonWriter.WriteValue(double.NaN);
533
 1
        jsonWriter.WriteEndObject();
534
      }
535

  
536
 1
      string expected = @"{
537
 1
_____'propertyName': NaN
538
 1
}";
539
 1
      string result = sb.ToString();
540

  
541
 1
      Assert.AreEqual(expected, result);
542
 1
    }
543

  
544
    [Test]
545
    public void WriteSingleBytes()
546
    {
547
 1
      StringBuilder sb = new StringBuilder();
548
 1
      StringWriter sw = new StringWriter(sb);
549

  
550
 1
      string text = "Hello world.";
551
 1
      byte[] data = Encoding.UTF8.GetBytes(text);
552

  
553
 1
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
554
      {
555
 1
        jsonWriter.Formatting = Formatting.Indented;
556
 1
        Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting);
557

  
558
 1
        jsonWriter.WriteValue(data);
559
      }
560

  
561
 1
      string expected = @"""SGVsbG8gd29ybGQu""";
562
 1
      string result = sb.ToString();
563

  
564
 1
      Assert.AreEqual(expected, result);
565

  
566
 1
      byte[] d2 = Convert.FromBase64String(result.Trim('"'));
567

  
568
 1
      Assert.AreEqual(text, Encoding.UTF8.GetString(d2, 0, d2.Length));
569
 1
    }
570

  
571
    [Test]
572
    public void WriteBytesInArray()
573
    {
574
 1
      StringBuilder sb = new StringBuilder();
575
 1
      StringWriter sw = new StringWriter(sb);
576

  
577
 1
      string text = "Hello world.";
578
 1
      byte[] data = Encoding.UTF8.GetBytes(text);
579

  
580
 1
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
581
      {
582
 1
        jsonWriter.Formatting = Formatting.Indented;
583
 1
        Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting);
584

  
585
 1
        jsonWriter.WriteStartArray();
586
 1
        jsonWriter.WriteValue(data);
587
 1
        jsonWriter.WriteValue(data);
588
 1
        jsonWriter.WriteValue((object)data);
589
 1
        jsonWriter.WriteValue((byte[])null);
590
 1
        jsonWriter.WriteEndArray();
591
      }
592

  
593
 1
      string expected = @"[
594
 1
  ""SGVsbG8gd29ybGQu"",
595
 1
  ""SGVsbG8gd29ybGQu"",
596
 1
  ""SGVsbG8gd29ybGQu"",
597
 1
  null
598
 1
]";
599
 1
      string result = sb.ToString();
600

  
601
 1
      Assert.AreEqual(expected, result);
602
 1
    }
603
  }
604
}