2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
30
using NUnit.Framework;
32
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
33
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
34
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
36
using Newtonsoft.Json.Linq;
37
using System.Globalization;
39
using Newtonsoft.Json.Utilities.LinqBridge;
44
namespace Newtonsoft.Json.Tests.Linq
47
public class JValueTests : TestFixtureBase
50
public void ChangeValue()
52
JValue v = new JValue(true);
53
Assert.AreEqual(true, v.Value);
54
Assert.AreEqual(JTokenType.Boolean, v.Type);
57
Assert.AreEqual("Pie", v.Value);
58
Assert.AreEqual(JTokenType.String, v.Type);
61
Assert.AreEqual(null, v.Value);
62
Assert.AreEqual(JTokenType.Null, v.Type);
64
v.Value = (int?) null;
65
Assert.AreEqual(null, v.Value);
66
Assert.AreEqual(JTokenType.Null, v.Type);
69
Assert.AreEqual("Pie", v.Value);
70
Assert.AreEqual(JTokenType.String, v.Type);
72
#if !(NETFX_CORE || PORTABLE)
73
v.Value = DBNull.Value;
74
Assert.AreEqual(DBNull.Value, v.Value);
75
Assert.AreEqual(JTokenType.Null, v.Type);
78
byte[] data = new byte[0];
81
Assert.AreEqual(data, v.Value);
82
Assert.AreEqual(JTokenType.Bytes, v.Type);
84
v.Value = StringComparison.OrdinalIgnoreCase;
85
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
86
Assert.AreEqual(JTokenType.Integer, v.Type);
88
v.Value = new Uri("http://json.codeplex.com/");
89
Assert.AreEqual(new Uri("http://json.codeplex.com/"), v.Value);
90
Assert.AreEqual(JTokenType.Uri, v.Type);
92
v.Value = TimeSpan.FromDays(1);
93
Assert.AreEqual(TimeSpan.FromDays(1), v.Value);
94
Assert.AreEqual(JTokenType.TimeSpan, v.Type);
96
Guid g = Guid.NewGuid();
98
Assert.AreEqual(g, v.Value);
99
Assert.AreEqual(JTokenType.Guid, v.Type);
103
public void CreateComment()
105
JValue commentValue = JValue.CreateComment(null);
106
Assert.AreEqual(null, commentValue.Value);
107
Assert.AreEqual(JTokenType.Comment, commentValue.Type);
109
commentValue.Value = "Comment";
110
Assert.AreEqual("Comment", commentValue.Value);
111
Assert.AreEqual(JTokenType.Comment, commentValue.Type);
115
public void CreateString()
117
JValue stringValue = JValue.CreateString(null);
118
Assert.AreEqual(null, stringValue.Value);
119
Assert.AreEqual(JTokenType.String, stringValue.Type);
123
public void JValueToString()
127
v = new JValue(true);
128
Assert.AreEqual("True", v.ToString());
130
v = new JValue(Encoding.UTF8.GetBytes("Blah"));
131
Assert.AreEqual("System.Byte[]", v.ToString(null, CultureInfo.InvariantCulture));
133
v = new JValue("I am a string!");
134
Assert.AreEqual("I am a string!", v.ToString());
136
v = new JValue(null, JTokenType.Null);
137
Assert.AreEqual("", v.ToString());
139
v = new JValue(null, JTokenType.Null);
140
Assert.AreEqual("", v.ToString(null, CultureInfo.InvariantCulture));
142
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
143
Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
145
v = new JValue(new Uri("http://json.codeplex.com/"));
146
Assert.AreEqual("http://json.codeplex.com/", v.ToString(null, CultureInfo.InvariantCulture));
148
v = new JValue(TimeSpan.FromDays(1));
149
Assert.AreEqual("1.00:00:00", v.ToString(null, CultureInfo.InvariantCulture));
151
v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5"));
152
Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture));
158
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
161
JValue v = new JValue(true);
162
JToken last = v.Last;
167
public void Children()
169
JValue v = new JValue(true);
170
var c = v.Children();
171
Assert.AreEqual(JEnumerable<JToken>.Empty, c);
177
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
180
JValue v = new JValue(true);
181
JToken first = v.First;
188
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
191
JValue v = new JValue(true);
199
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
202
JValue v = new JValue(true);
208
public void RemoveParentNull()
210
ExceptionAssert.Throws<InvalidOperationException>("The parent is missing.",
213
JValue v = new JValue(true);
221
JValue v = new JValue(true);
222
Assert.AreEqual(v, v.Root);
226
public void Previous()
228
JValue v = new JValue(true);
229
Assert.IsNull(v.Previous);
235
JValue v = new JValue(true);
236
Assert.IsNull(v.Next);
240
public void DeepEquals()
242
Assert.IsTrue(JToken.DeepEquals(new JValue(5L), new JValue(5)));
243
Assert.IsFalse(JToken.DeepEquals(new JValue(5M), new JValue(5)));
244
Assert.IsTrue(JToken.DeepEquals(new JValue((ulong) long.MaxValue), new JValue(long.MaxValue)));
248
public void HasValues()
250
Assert.IsFalse((new JValue(5L)).HasValues);
254
public void SetValue()
256
ExceptionAssert.Throws<InvalidOperationException>("Cannot set child value on Newtonsoft.Json.Linq.JValue.",
259
JToken t = new JValue(5L);
260
t[0] = new JValue(3);
265
public void CastNullValueToNonNullable()
267
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to Int32.",
270
JValue v = new JValue((object) null);
276
public void ConvertValueToCompatibleType()
278
IComparable c = (new JValue(1).Value<IComparable>());
279
Assert.AreEqual(1L, c);
283
public void ConvertValueToFormattableType()
285
IFormattable f = (new JValue(1).Value<IFormattable>());
286
Assert.AreEqual(1L, f);
288
Assert.AreEqual("01", f.ToString("00", CultureInfo.InvariantCulture));
292
public void Ordering()
294
JObject o = new JObject(
295
new JProperty("Integer", new JValue(1)),
296
new JProperty("Float", new JValue(1.2d)),
297
new JProperty("Decimal", new JValue(1.1m))
300
IList<object> orderedValues = o.Values().Cast<JValue>().OrderBy(v => v).Select(v => v.Value).ToList();
302
Assert.AreEqual(1L, orderedValues[0]);
303
Assert.AreEqual(1.1m, orderedValues[1]);
304
Assert.AreEqual(1.2d, orderedValues[2]);
308
public void WriteSingle()
311
JValue value = new JValue(f);
313
string json = value.ToString(Formatting.None);
315
Assert.AreEqual("5.2", json);
320
public decimal Compoundings { get; set; }
323
private readonly Rate rate = new Rate {Compoundings = 12.166666666666666666666666667m};
326
public void WriteFullDecimalPrecision()
328
var jTokenWriter = new JTokenWriter();
329
new JsonSerializer().Serialize(jTokenWriter, rate);
330
string json = jTokenWriter.Token.ToString();
332
""Compoundings"": 12.166666666666666666666666667
337
public void RoundTripDecimal()
339
var jTokenWriter = new JTokenWriter();
340
new JsonSerializer().Serialize(jTokenWriter, rate);
341
var rate2 = new JsonSerializer().Deserialize<Rate>(new JTokenReader(jTokenWriter.Token));
343
Assert.AreEqual(rate.Compoundings, rate2.Compoundings);
347
public class ObjectWithDateTimeOffset
349
public DateTimeOffset DateTimeOffset { get; set; }
353
public void SetDateTimeOffsetProperty()
355
var dateTimeOffset = new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(3));
356
var json = JsonConvert.SerializeObject(
357
new ObjectWithDateTimeOffset
359
DateTimeOffset = dateTimeOffset
362
var o = JObject.Parse(json);
363
o.Property("DateTimeOffset").Value = dateTimeOffset;
b'\\ No newline at end of file'