~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#if !(NET35 || NET20 || PORTABLE)
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Dynamic;
 
30
using System.Linq;
 
31
using System.Text;
 
32
using Newtonsoft.Json.Linq;
 
33
#if !NETFX_CORE
 
34
using NUnit.Framework;
 
35
#else
 
36
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
37
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
38
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
39
#endif
 
40
using Newtonsoft.Json.Utilities;
 
41
using System.Globalization;
 
42
 
 
43
namespace Newtonsoft.Json.Tests.Linq
 
44
{
 
45
  [TestFixture]
 
46
  public class DynamicTests : TestFixtureBase
 
47
  {
 
48
    [Test]
 
49
    public void JObjectPropertyNames()
 
50
    {
 
51
      JObject o = new JObject(
 
52
        new JProperty("ChildValue", "blah blah"));
 
53
 
 
54
      dynamic d = o;
 
55
 
 
56
      d.First = "A value!";
 
57
 
 
58
      Assert.AreEqual(new JValue("A value!"), d.First);
 
59
      Assert.AreEqual("A value!", (string)d.First);
 
60
 
 
61
      d.First = null;
 
62
      Assert.AreEqual(JTokenType.Null, d.First.Type);
 
63
 
 
64
      Assert.IsTrue(d.Remove("First"));
 
65
      Assert.IsNull(d.First);
 
66
 
 
67
      JValue v1 = d.ChildValue;
 
68
      JValue v2 = d["ChildValue"];
 
69
      Assert.AreEqual(v1, v2);
 
70
 
 
71
      JValue newValue1 = new JValue("Blah blah");
 
72
      d.NewValue = newValue1;
 
73
      JValue newValue2 = d.NewValue;
 
74
 
 
75
      Assert.IsTrue(ReferenceEquals(newValue1, newValue2));
 
76
    }
 
77
 
 
78
    [Test]
 
79
    public void JObjectEnumerator()
 
80
    {
 
81
      JObject o = new JObject(
 
82
        new JProperty("ChildValue", "blah blah"));
 
83
 
 
84
      dynamic d = o;
 
85
 
 
86
      foreach (JProperty value in d)
 
87
      {
 
88
        Assert.AreEqual("ChildValue", value.Name);
 
89
        Assert.AreEqual("blah blah", (string)value.Value);
 
90
      }
 
91
 
 
92
      foreach (dynamic value in d)
 
93
      {
 
94
        Assert.AreEqual("ChildValue", value.Name);
 
95
        Assert.AreEqual("blah blah", (string)value.Value);
 
96
      }
 
97
    }
 
98
 
 
99
    [Test]
 
100
    public void JObjectPropertyNameWithJArray()
 
101
    {
 
102
      JObject o = new JObject(
 
103
        new JProperty("ChildValue", "blah blah"));
 
104
 
 
105
      dynamic d = o;
 
106
 
 
107
      d.First = new JArray();
 
108
      d.First.Add("Hi");
 
109
 
 
110
      Assert.AreEqual(1, d.First.Count);
 
111
    }
 
112
 
 
113
    [Test]
 
114
    public void JObjectPropertyNameWithNonToken()
 
115
    {
 
116
      ExceptionAssert.Throws<ArgumentException>(
 
117
        "Could not determine JSON object type for type System.String[].",
 
118
        () =>
 
119
        {
 
120
          dynamic d = new JObject();
 
121
 
 
122
          d.First = new[] { "One", "II", "3" };
 
123
        });
 
124
    }
 
125
 
 
126
    [Test]
 
127
    public void JObjectMethods()
 
128
    {
 
129
      JObject o = new JObject(
 
130
        new JProperty("ChildValue", "blah blah"));
 
131
 
 
132
      dynamic d = o;
 
133
 
 
134
      d.Add("NewValue", 1);
 
135
 
 
136
      object count = d.Count;
 
137
 
 
138
      Assert.IsNull(count);
 
139
      Assert.IsNull(d["Count"]);
 
140
 
 
141
      JToken v;
 
142
      Assert.IsTrue(d.TryGetValue("ChildValue", out v));
 
143
      Assert.AreEqual("blah blah", (string)v);
 
144
    }
 
145
 
 
146
    [Test]
 
147
    public void JValueEquals()
 
148
    {
 
149
      JObject o = new JObject(
 
150
        new JProperty("Null", new JValue(null, JTokenType.Null)),
 
151
        new JProperty("Integer", new JValue(1)),
 
152
        new JProperty("Float", new JValue(1.1d)),
 
153
        new JProperty("Decimal", new JValue(1.1m)),
 
154
        new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
 
155
        new JProperty("Boolean", new JValue(true)),
 
156
        new JProperty("String", new JValue("A string lol!")),
 
157
        new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
 
158
        new JProperty("Uri", new Uri("http://json.codeplex.com/")),
 
159
        new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
 
160
        new JProperty("TimeSpan", TimeSpan.FromDays(1))
 
161
        );
 
162
 
 
163
      dynamic d = o;
 
164
 
 
165
      Assert.IsTrue(d.Null == d.Null);
 
166
      Assert.IsTrue(d.Null == null);
 
167
      Assert.IsTrue(d.Null == new JValue(null, JTokenType.Null));
 
168
      Assert.IsFalse(d.Null == 1);
 
169
 
 
170
      Assert.IsTrue(d.Integer == d.Integer);
 
171
      Assert.IsTrue(d.Integer > 0);
 
172
      Assert.IsTrue(d.Integer > 0.0m);
 
173
      Assert.IsTrue(d.Integer > 0.0f);
 
174
      Assert.IsTrue(d.Integer > null);
 
175
      Assert.IsTrue(d.Integer >= null);
 
176
      Assert.IsTrue(d.Integer == 1);
 
177
      Assert.IsTrue(d.Integer == 1m);
 
178
      Assert.IsTrue(d.Integer != 1.1f);
 
179
      Assert.IsTrue(d.Integer != 1.1d);
 
180
 
 
181
      Assert.IsTrue(d.Decimal == d.Decimal);
 
182
      Assert.IsTrue(d.Decimal > 0);
 
183
      Assert.IsTrue(d.Decimal > 0.0m);
 
184
      Assert.IsTrue(d.Decimal > 0.0f);
 
185
      Assert.IsTrue(d.Decimal > null);
 
186
      Assert.IsTrue(d.Decimal >= null);
 
187
      Assert.IsTrue(d.Decimal == 1.1);
 
188
      Assert.IsTrue(d.Decimal == 1.1m);
 
189
      Assert.IsTrue(d.Decimal != 1.0f);
 
190
      Assert.IsTrue(d.Decimal != 1.0d);
 
191
 
 
192
      Assert.IsTrue(d.Float == d.Float);
 
193
      Assert.IsTrue(d.Float > 0);
 
194
      Assert.IsTrue(d.Float > 0.0m);
 
195
      Assert.IsTrue(d.Float > 0.0f);
 
196
      Assert.IsTrue(d.Float > null);
 
197
      Assert.IsTrue(d.Float >= null);
 
198
      Assert.IsTrue(d.Float < 2);
 
199
      Assert.IsTrue(d.Float <= 1.1);
 
200
      Assert.IsTrue(d.Float == 1.1);
 
201
      Assert.IsTrue(d.Float == 1.1m);
 
202
      Assert.IsTrue(d.Float != 1.0f);
 
203
      Assert.IsTrue(d.Float != 1.0d);
 
204
 
 
205
      Assert.IsTrue(d.Bytes == d.Bytes);
 
206
      Assert.IsTrue(d.Bytes == Encoding.UTF8.GetBytes("A string lol!"));
 
207
      Assert.IsTrue(d.Bytes == new JValue(Encoding.UTF8.GetBytes("A string lol!")));
 
208
 
 
209
      Assert.IsTrue(d.Uri == d.Uri);
 
210
      Assert.IsTrue(d.Uri == new Uri("http://json.codeplex.com/"));
 
211
      Assert.IsTrue(d.Uri > new Uri("http://abc.org/"));
 
212
      Assert.IsTrue(d.Uri >= new Uri("http://abc.com/"));
 
213
      Assert.IsTrue(d.Uri > null);
 
214
      Assert.IsTrue(d.Uri >= null);
 
215
 
 
216
      Assert.IsTrue(d.Guid == d.Guid);
 
217
      Assert.IsTrue(d.Guid == new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF"));
 
218
      Assert.IsTrue(d.Guid > new Guid("AAAAAAAA-0D80-44F2-BF34-4654156FA7AF"));
 
219
      Assert.IsTrue(d.Guid >= new Guid("AAAAAAAA-0D80-44F2-BF34-4654156FA7AF"));
 
220
      Assert.IsTrue(d.Guid > null);
 
221
      Assert.IsTrue(d.Guid >= null);
 
222
 
 
223
      Assert.IsTrue(d.TimeSpan == d.TimeSpan);
 
224
      Assert.IsTrue(d.TimeSpan == TimeSpan.FromDays(1));
 
225
      Assert.IsTrue(d.TimeSpan > TimeSpan.FromHours(1));
 
226
      Assert.IsTrue(d.TimeSpan >= TimeSpan.FromHours(1));
 
227
      Assert.IsTrue(d.TimeSpan > null);
 
228
      Assert.IsTrue(d.TimeSpan >= null);
 
229
    }
 
230
 
 
231
    [Test]
 
232
    public void JValueAddition()
 
233
    {
 
234
      JObject o = new JObject(
 
235
        new JProperty("Null", new JValue(null, JTokenType.Null)),
 
236
        new JProperty("Integer", new JValue(1)),
 
237
        new JProperty("Float", new JValue(1.1d)),
 
238
        new JProperty("Decimal", new JValue(1.1m)),
 
239
        new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
 
240
        new JProperty("Boolean", new JValue(true)),
 
241
        new JProperty("String", new JValue("A string lol!")),
 
242
        new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
 
243
        new JProperty("Uri", new Uri("http://json.codeplex.com/")),
 
244
        new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
 
245
        new JProperty("TimeSpan", TimeSpan.FromDays(1))
 
246
        );
 
247
 
 
248
      dynamic d = o;
 
249
      dynamic r;
 
250
 
 
251
      #region Add
 
252
      r = d.String + " LAMO!";
 
253
      Assert.AreEqual("A string lol! LAMO!", (string)r);
 
254
      r += " gg";
 
255
      Assert.AreEqual("A string lol! LAMO! gg", (string)r);
 
256
 
 
257
      r = d.String + null;
 
258
      Assert.AreEqual("A string lol!", (string)r);
 
259
      r += null;
 
260
      Assert.AreEqual("A string lol!", (string)r);
 
261
 
 
262
      r = d.Integer + 1;
 
263
      Assert.AreEqual(2, (int)r);
 
264
      r += 2;
 
265
      Assert.AreEqual(4, (int)r);
 
266
 
 
267
      r = d.Integer + 1.1;
 
268
      Assert.AreEqual(2.1, (double)r);
 
269
      r += 2;
 
270
      Assert.AreEqual(4.1, (double)r);
 
271
 
 
272
      r = d.Integer + 1.1d;
 
273
      Assert.AreEqual(2.1m, (decimal)r);
 
274
      r += 2;
 
275
      Assert.AreEqual(4.1m, (decimal)r);
 
276
 
 
277
      r = d.Integer + null;
 
278
      Assert.AreEqual(null, r.Value);
 
279
      r += 2;
 
280
      Assert.AreEqual(null, r.Value);
 
281
 
 
282
      r = d.Float + 1;
 
283
      Assert.AreEqual(2.1d, (double)r);
 
284
      r += 2;
 
285
      Assert.AreEqual(4.1d, (double)r);
 
286
 
 
287
      r = d.Float + 1.1;
 
288
      Assert.AreEqual(2.2d, (double)r);
 
289
      r += 2;
 
290
      Assert.AreEqual(4.2d, (double)r);
 
291
 
 
292
      r = d.Float + 1.1d;
 
293
      Assert.AreEqual(2.2m, (decimal)r);
 
294
      r += 2;
 
295
      Assert.AreEqual(4.2m, (decimal)r);
 
296
 
 
297
      r = d.Float + null;
 
298
      Assert.AreEqual(null, r.Value);
 
299
      r += 2;
 
300
      Assert.AreEqual(null, r.Value);
 
301
 
 
302
      r = d.Decimal + 1;
 
303
      Assert.AreEqual(2.1m, (decimal)r);
 
304
      r += 2;
 
305
      Assert.AreEqual(4.1m, (decimal)r);
 
306
 
 
307
      r = d.Decimal + 1.1;
 
308
      Assert.AreEqual(2.2m, (decimal)r);
 
309
      r += 2;
 
310
      Assert.AreEqual(4.2m, (decimal)r);
 
311
 
 
312
      r = d.Decimal + 1.1d;
 
313
      Assert.AreEqual(2.2m, (decimal)r);
 
314
      r += 2;
 
315
      Assert.AreEqual(4.2m, (decimal)r);
 
316
 
 
317
      r = d.Decimal + null;
 
318
      Assert.AreEqual(null, r.Value);
 
319
      r += 2;
 
320
      Assert.AreEqual(null, r.Value);
 
321
      #endregion
 
322
 
 
323
      #region Subtract
 
324
      r = d.Integer - 1;
 
325
      Assert.AreEqual(0, (int)r);
 
326
      r -= 2;
 
327
      Assert.AreEqual(-2, (int)r);
 
328
 
 
329
      r = d.Integer - 1.1;
 
330
      Assert.AreEqual(-0.1d, (double)r, 0.00001);
 
331
      r -= 2;
 
332
      Assert.AreEqual(-2.1d, (double)r);
 
333
 
 
334
      r = d.Integer - 1.1d;
 
335
      Assert.AreEqual(-0.1m, (decimal)r);
 
336
      r -= 2;
 
337
      Assert.AreEqual(-2.1m, (decimal)r);
 
338
 
 
339
      r = d.Integer - null;
 
340
      Assert.AreEqual(null, r.Value);
 
341
      r -= 2;
 
342
      Assert.AreEqual(null, r.Value);
 
343
 
 
344
      r = d.Float - 1;
 
345
      Assert.AreEqual(0.1d, (double)r, 0.00001);
 
346
      r -= 2;
 
347
      Assert.AreEqual(-1.9d, (double)r);
 
348
 
 
349
      r = d.Float - 1.1;
 
350
      Assert.AreEqual(0d, (double)r);
 
351
      r -= 2;
 
352
      Assert.AreEqual(-2d, (double)r);
 
353
 
 
354
      r = d.Float - 1.1d;
 
355
      Assert.AreEqual(0m, (decimal)r);
 
356
      r -= 2;
 
357
      Assert.AreEqual(-2m, (decimal)r);
 
358
 
 
359
      r = d.Float - null;
 
360
      Assert.AreEqual(null, r.Value);
 
361
      r -= 2;
 
362
      Assert.AreEqual(null, r.Value);
 
363
 
 
364
      r = d.Decimal - 1;
 
365
      Assert.AreEqual(0.1m, (decimal)r);
 
366
      r -= 2;
 
367
      Assert.AreEqual(-1.9m, (decimal)r);
 
368
 
 
369
      r = d.Decimal - 1.1;
 
370
      Assert.AreEqual(0m, (decimal)r);
 
371
      r -= 2;
 
372
      Assert.AreEqual(-2m, (decimal)r);
 
373
 
 
374
      r = d.Decimal - 1.1d;
 
375
      Assert.AreEqual(0m, (decimal)r);
 
376
      r -= 2;
 
377
      Assert.AreEqual(-2m, (decimal)r);
 
378
 
 
379
      r = d.Decimal - null;
 
380
      Assert.AreEqual(null, r.Value);
 
381
      r -= 2;
 
382
      Assert.AreEqual(null, r.Value);
 
383
      #endregion
 
384
 
 
385
      #region Multiply
 
386
      r = d.Integer * 1;
 
387
      Assert.AreEqual(1, (int)r);
 
388
      r *= 2;
 
389
      Assert.AreEqual(2, (int)r);
 
390
 
 
391
      r = d.Integer * 1.1;
 
392
      Assert.AreEqual(1.1d, (double)r);
 
393
      r *= 2;
 
394
      Assert.AreEqual(2.2d, (double)r);
 
395
 
 
396
      r = d.Integer * 1.1d;
 
397
      Assert.AreEqual(1.1m, (decimal)r);
 
398
      r *= 2;
 
399
      Assert.AreEqual(2.2m, (decimal)r);
 
400
 
 
401
      r = d.Integer * null;
 
402
      Assert.AreEqual(null, r.Value);
 
403
      r *= 2;
 
404
      Assert.AreEqual(null, r.Value);
 
405
 
 
406
      r = d.Float * 1;
 
407
      Assert.AreEqual(1.1d, (double)r);
 
408
      r *= 2;
 
409
      Assert.AreEqual(2.2d, (double)r);
 
410
 
 
411
      r = d.Float * 1.1;
 
412
      Assert.AreEqual(1.21d, (double)r, 0.00001);
 
413
      r *= 2;
 
414
      Assert.AreEqual(2.42d, (double)r, 0.00001);
 
415
 
 
416
      r = d.Float * 1.1d;
 
417
      Assert.AreEqual(1.21m, (decimal)r);
 
418
      r *= 2;
 
419
      Assert.AreEqual(2.42m, (decimal)r);
 
420
 
 
421
      r = d.Float * null;
 
422
      Assert.AreEqual(null, r.Value);
 
423
      r *= 2;
 
424
      Assert.AreEqual(null, r.Value);
 
425
 
 
426
      r = d.Decimal * 1;
 
427
      Assert.AreEqual(1.1m, (decimal)r);
 
428
      r *= 2;
 
429
      Assert.AreEqual(2.2m, (decimal)r);
 
430
 
 
431
      r = d.Decimal * 1.1;
 
432
      Assert.AreEqual(1.21m, (decimal)r);
 
433
      r *= 2;
 
434
      Assert.AreEqual(2.42m, (decimal)r);
 
435
 
 
436
      r = d.Decimal * 1.1d;
 
437
      Assert.AreEqual(1.21m, (decimal)r);
 
438
      r *= 2;
 
439
      Assert.AreEqual(2.42m, (decimal)r);
 
440
 
 
441
      r = d.Decimal * null;
 
442
      Assert.AreEqual(null, r.Value);
 
443
      r *= 2;
 
444
      Assert.AreEqual(null, r.Value);
 
445
      #endregion
 
446
 
 
447
      #region Divide
 
448
      r = d.Integer / 1;
 
449
      Assert.AreEqual(1, (int)r);
 
450
      r /= 2;
 
451
      Assert.AreEqual(0, (int)r);
 
452
 
 
453
      r = d.Integer / 1.1;
 
454
      Assert.AreEqual(0.9090909090909091d, (double)r);
 
455
      r /= 2;
 
456
      Assert.AreEqual(0.454545454545455d, (double)r, 0.00001);
 
457
 
 
458
      r = d.Integer / 1.1d;
 
459
      Assert.AreEqual(0.909090909090909m, (decimal)r);
 
460
      r /= 2;
 
461
      Assert.AreEqual(0.454545454545454m, (decimal)r);
 
462
 
 
463
      r = d.Integer / null;
 
464
      Assert.AreEqual(null, r.Value);
 
465
      r /= 2;
 
466
      Assert.AreEqual(null, r.Value);
 
467
 
 
468
      r = d.Float / 1;
 
469
      Assert.AreEqual(1.1d, (double)r);
 
470
      r /= 2;
 
471
      Assert.AreEqual(0.55d, (double)r);
 
472
 
 
473
      r = d.Float / 1.1;
 
474
      Assert.AreEqual(1d, (double)r, 0.00001);
 
475
      r /= 2;
 
476
      Assert.AreEqual(0.5d, (double)r, 0.00001);
 
477
 
 
478
      r = d.Float / 1.1d;
 
479
      Assert.AreEqual(1m, (decimal)r);
 
480
      r /= 2;
 
481
      Assert.AreEqual(0.5m, (decimal)r);
 
482
 
 
483
      r = d.Float / null;
 
484
      Assert.AreEqual(null, r.Value);
 
485
      r /= 2;
 
486
      Assert.AreEqual(null, r.Value);
 
487
 
 
488
      r = d.Decimal / 1;
 
489
      Assert.AreEqual(1.1m, (decimal)r);
 
490
      r /= 2;
 
491
      Assert.AreEqual(0.55m, (decimal)r);
 
492
 
 
493
      r = d.Decimal / 1.1;
 
494
      Assert.AreEqual(1m, (decimal)r);
 
495
      r /= 2;
 
496
      Assert.AreEqual(0.5m, (decimal)r);
 
497
 
 
498
      r = d.Decimal / 1.1d;
 
499
      Assert.AreEqual(1m, (decimal)r);
 
500
      r /= 2;
 
501
      Assert.AreEqual(0.5m, (decimal)r);
 
502
 
 
503
      r = d.Decimal / null;
 
504
      Assert.AreEqual(null, r.Value);
 
505
      r /= 2;
 
506
      Assert.AreEqual(null, r.Value);
 
507
      #endregion
 
508
    }
 
509
 
 
510
    [Test]
 
511
    public void JValueToString()
 
512
    {
 
513
      JObject o = new JObject(
 
514
        new JProperty("Null", new JValue(null, JTokenType.Null)),
 
515
        new JProperty("Integer", new JValue(1)),
 
516
        new JProperty("Float", new JValue(1.1)),
 
517
        new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
 
518
        new JProperty("Boolean", new JValue(true)),
 
519
        new JProperty("String", new JValue("A string lol!")),
 
520
        new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
 
521
        new JProperty("Uri", new Uri("http://json.codeplex.com/")),
 
522
        new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
 
523
        new JProperty("TimeSpan", TimeSpan.FromDays(1))
 
524
        );
 
525
 
 
526
      dynamic d = o;
 
527
 
 
528
      Assert.AreEqual("", d.Null.ToString());
 
529
      Assert.AreEqual("1", d.Integer.ToString());
 
530
      Assert.AreEqual("1.1", d.Float.ToString(CultureInfo.InvariantCulture));
 
531
      Assert.AreEqual("12/29/2000 23:51:10", d.DateTime.ToString(null, CultureInfo.InvariantCulture));
 
532
      Assert.AreEqual("True", d.Boolean.ToString());
 
533
      Assert.AreEqual("A string lol!", d.String.ToString());
 
534
      Assert.AreEqual("System.Byte[]", d.Bytes.ToString());
 
535
      Assert.AreEqual("http://json.codeplex.com/", d.Uri.ToString());
 
536
      Assert.AreEqual("ea27fe1d-0d80-44f2-bf34-4654156fa7af", d.Guid.ToString());
 
537
      Assert.AreEqual("1.00:00:00", d.TimeSpan.ToString());
 
538
    }
 
539
 
 
540
    [Test]
 
541
    public void JObjectGetDynamicPropertyNames()
 
542
    {
 
543
      JObject o = new JObject(
 
544
        new JProperty("ChildValue", "blah blah"),
 
545
        new JProperty("Hello Joe", null));
 
546
 
 
547
      dynamic d = o;
 
548
 
 
549
      List<string> memberNames = o.GetDynamicMemberNames().ToList();
 
550
 
 
551
      Assert.AreEqual(2, memberNames.Count);
 
552
      Assert.AreEqual("ChildValue", memberNames[0]);
 
553
      Assert.AreEqual("Hello Joe", memberNames[1]);
 
554
 
 
555
      o = new JObject(
 
556
        new JProperty("ChildValue1", "blah blah"),
 
557
        new JProperty("Hello Joe1", null));
 
558
 
 
559
      d = o;
 
560
 
 
561
      memberNames = o.GetDynamicMemberNames().ToList();
 
562
 
 
563
      Assert.AreEqual(2, memberNames.Count);
 
564
      Assert.AreEqual("ChildValue1", memberNames[0]);
 
565
      Assert.AreEqual("Hello Joe1", memberNames[1]);
 
566
    }
 
567
 
 
568
    [Test]
 
569
    public void JValueConvert()
 
570
    {
 
571
      AssertValueConverted<bool>(true);
 
572
      AssertValueConverted<bool?>(true);
 
573
      AssertValueConverted<bool?>(false);
 
574
      AssertValueConverted<bool?>(null);
 
575
      AssertValueConverted<bool?>("true", true);
 
576
      AssertValueConverted<byte[]>(null);
 
577
      AssertValueConverted<byte[]>(Encoding.UTF8.GetBytes("blah"));
 
578
      AssertValueConverted<DateTime>(new DateTime(2000, 12, 20, 23, 59, 2, DateTimeKind.Utc));
 
579
      AssertValueConverted<DateTime?>(new DateTime(2000, 12, 20, 23, 59, 2, DateTimeKind.Utc));
 
580
      AssertValueConverted<DateTime?>(null);
 
581
      AssertValueConverted<DateTimeOffset>(new DateTimeOffset(2000, 12, 20, 23, 59, 2, TimeSpan.FromHours(1)));
 
582
      AssertValueConverted<DateTimeOffset?>(new DateTimeOffset(2000, 12, 20, 23, 59, 2, TimeSpan.FromHours(1)));
 
583
      AssertValueConverted<DateTimeOffset?>(null);
 
584
      AssertValueConverted<decimal>(99.9m);
 
585
      AssertValueConverted<decimal?>(99.9m);
 
586
      AssertValueConverted<decimal>(1m);
 
587
      AssertValueConverted<decimal>(1.1f, 1.1m);
 
588
      AssertValueConverted<decimal>("1.1", 1.1m);
 
589
      AssertValueConverted<double>(99.9);
 
590
      AssertValueConverted<double>(99.9d);
 
591
      AssertValueConverted<double?>(99.9d);
 
592
      AssertValueConverted<float>(99.9f);
 
593
      AssertValueConverted<float?>(99.9f);
 
594
      AssertValueConverted<int>(int.MinValue);
 
595
      AssertValueConverted<int?>(int.MinValue);
 
596
      AssertValueConverted<long>(long.MaxValue);
 
597
      AssertValueConverted<long?>(long.MaxValue);
 
598
      AssertValueConverted<short>(short.MaxValue);
 
599
      AssertValueConverted<short?>(short.MaxValue);
 
600
      AssertValueConverted<string>("blah");
 
601
      AssertValueConverted<string>(null);
 
602
      AssertValueConverted<string>(1, "1");
 
603
      AssertValueConverted<uint>(uint.MinValue);
 
604
      AssertValueConverted<uint?>(uint.MinValue);
 
605
      AssertValueConverted<uint?>("1", (uint)1);
 
606
      AssertValueConverted<ulong>(ulong.MaxValue);
 
607
      AssertValueConverted<ulong?>(ulong.MaxValue);
 
608
      AssertValueConverted<ushort>(ushort.MinValue);
 
609
      AssertValueConverted<ushort?>(ushort.MinValue);
 
610
      AssertValueConverted<ushort?>(null);
 
611
      AssertValueConverted<TimeSpan>(TimeSpan.FromDays(1));
 
612
      AssertValueConverted<TimeSpan?>(TimeSpan.FromDays(1));
 
613
      AssertValueConverted<TimeSpan?>(null);
 
614
      AssertValueConverted<Guid>(new Guid("60304274-CD13-4060-B38C-057C8557AB54"));
 
615
      AssertValueConverted<Guid?>(new Guid("60304274-CD13-4060-B38C-057C8557AB54"));
 
616
      AssertValueConverted<Guid?>(null);
 
617
      AssertValueConverted<Uri>(new Uri("http://json.codeplex.com/"));
 
618
      AssertValueConverted<Uri>(null);
 
619
    }
 
620
 
 
621
    private static void AssertValueConverted<T>(object value)
 
622
    {
 
623
      AssertValueConverted<T>(value, value);
 
624
    }
 
625
 
 
626
    private static void AssertValueConverted<T>(object value, object expected)
 
627
    {
 
628
      JValue v = new JValue(value);
 
629
      dynamic d = v;
 
630
 
 
631
      T t = d;
 
632
      Assert.AreEqual(expected, t);
 
633
    }
 
634
 
 
635
    [Test]
 
636
    public void DynamicSerializerExample()
 
637
    {
 
638
      dynamic value = new DynamicDictionary();
 
639
 
 
640
      value.Name = "Arine Admin";
 
641
      value.Enabled = true;
 
642
      value.Roles = new[] {"Admin", "User"};
 
643
 
 
644
      string json = JsonConvert.SerializeObject(value, Formatting.Indented);
 
645
      // {
 
646
      //   "Name": "Arine Admin",
 
647
      //   "Enabled": true,
 
648
      //   "Roles": [
 
649
      //     "Admin",
 
650
      //     "User"
 
651
      //   ]
 
652
      // }
 
653
 
 
654
      dynamic newValue = JsonConvert.DeserializeObject<DynamicDictionary>(json);
 
655
 
 
656
      string role = newValue.Roles[0];
 
657
      // Admin
 
658
    }
 
659
 
 
660
    [Test]
 
661
    public void DynamicLinqExample()
 
662
    {
 
663
      JObject oldAndBusted = new JObject();
 
664
      oldAndBusted["Name"] = "Arnie Admin";
 
665
      oldAndBusted["Enabled"] = true;
 
666
      oldAndBusted["Roles"] = new JArray(new[] { "Admin", "User" });
 
667
 
 
668
      string oldRole = (string) oldAndBusted["Roles"][0];
 
669
      // Admin
 
670
 
 
671
 
 
672
      dynamic newHotness = new JObject();
 
673
      newHotness.Name = "Arnie Admin";
 
674
      newHotness.Enabled = true;
 
675
      newHotness.Roles = new JArray(new[] { "Admin", "User" });
 
676
 
 
677
      string newRole = newHotness.Roles[0];
 
678
      // Admin
 
679
    }
 
680
 
 
681
    [Test]
 
682
    public void ImprovedDynamicLinqExample()
 
683
    {
 
684
      dynamic product = new JObject();
 
685
      product.ProductName = "Elbow Grease";
 
686
      product.Enabled = true;
 
687
      product.Price = 4.90m;
 
688
      product.StockCount = 9000;
 
689
      product.StockValue = 44100;
 
690
 
 
691
      // All Elbow Grease must go sale!
 
692
      // 50% off price
 
693
 
 
694
      product.Price = product.Price / 2;
 
695
      product.StockValue = product.StockCount * product.Price;
 
696
      product.ProductName = product.ProductName + " (SALE)";
 
697
 
 
698
      string json = product.ToString();
 
699
      // {
 
700
      //   "ProductName": "Elbow Grease (SALE)",
 
701
      //   "Enabled": true,
 
702
      //   "Price": 2.45,
 
703
      //   "StockCount": 9000,
 
704
      //   "StockValue": 22050.00
 
705
      // }
 
706
 
 
707
      Assert.AreEqual(@"{
 
708
  ""ProductName"": ""Elbow Grease (SALE)"",
 
709
  ""Enabled"": true,
 
710
  ""Price"": 2.45,
 
711
  ""StockCount"": 9000,
 
712
  ""StockValue"": 22050.00
 
713
}", json);
 
714
    }
 
715
  }
 
716
 
 
717
  public class DynamicDictionary : DynamicObject
 
718
  {
 
719
    private readonly IDictionary<string, object> _values = new Dictionary<string, object>();
 
720
 
 
721
    public override IEnumerable<string> GetDynamicMemberNames()
 
722
    {
 
723
      return _values.Keys;
 
724
    }
 
725
 
 
726
    public override bool TryGetMember(GetMemberBinder binder, out object result)
 
727
    {
 
728
      result = _values[binder.Name];
 
729
      return true;
 
730
    }
 
731
 
 
732
    public override bool TrySetMember(SetMemberBinder binder, object value)
 
733
    {
 
734
      _values[binder.Name] = value;
 
735
      return true;
 
736
    }
 
737
  }
 
738
}
 
739
#endif
 
 
b'\\ No newline at end of file'