1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Xunit;
namespace System.Json
{
/// <summary>
/// Tests for round-tripping <see cref="JsonValue"/> instances via JSON strings.
/// </summary>
public class JsonStringRoundTripTests
{
/// <summary>
/// Tests for <see cref="JsonObject"/> round-trip.
/// </summary>
[Fact]
public void ValidJsonObjectRoundTrip()
{
bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;
CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
try
{
int seed = 1;
Log.Info("Seed: {0}", seed);
Random rndGen = new Random(seed);
JsonObject sourceJson = new JsonObject(new Dictionary<string, JsonValue>()
{
{ "Name", PrimitiveCreator.CreateInstanceOfString(rndGen) },
{ "Age", PrimitiveCreator.CreateInstanceOfInt32(rndGen) },
{ "DateTimeOffset", PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen) },
{ "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
});
sourceJson.Add("NewItem1", PrimitiveCreator.CreateInstanceOfString(rndGen));
sourceJson.Add(new KeyValuePair<string, JsonValue>("NewItem2", PrimitiveCreator.CreateInstanceOfString(rndGen)));
JsonObject newJson = (JsonObject)JsonValue.Parse(sourceJson.ToString());
newJson.Remove("NewItem1");
sourceJson.Remove("NewItem1");
Assert.False(newJson.ContainsKey("NewItem1"));
Assert.False(!JsonValueVerifier.Compare(sourceJson, newJson));
}
finally
{
CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
}
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="DateTime"/>.
/// </summary>
[Fact]
public void SimpleDateTimeTest()
{
JsonValue jv = DateTime.Now;
JsonValue jv2 = JsonValue.Parse(jv.ToString());
Assert.Equal(jv.ToString(), jv2.ToString());
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="DateTimeOffset"/>.
/// </summary>
[Fact]
public void ValidJsonObjectDateTimeOffsetRoundTrip()
{
int seed = 1;
Log.Info("Seed: {0}", seed);
Random rndGen = new Random(seed);
JsonPrimitive sourceJson = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen));
JsonPrimitive newJson = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString());
Assert.True(JsonValueVerifier.Compare(sourceJson, newJson));
}
/// <summary>
/// Tests for <see cref="JsonArray"/> round-trip.
/// </summary>
[Fact]
public void ValidJsonArrayRoundTrip()
{
bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;
CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
try
{
int seed = 1;
Log.Info("Seed: {0}", seed);
Random rndGen = new Random(seed);
JsonArray sourceJson = new JsonArray(new JsonValue[]
{
PrimitiveCreator.CreateInstanceOfBoolean(rndGen),
PrimitiveCreator.CreateInstanceOfByte(rndGen),
PrimitiveCreator.CreateInstanceOfDateTime(rndGen),
PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen),
PrimitiveCreator.CreateInstanceOfDecimal(rndGen),
PrimitiveCreator.CreateInstanceOfDouble(rndGen),
PrimitiveCreator.CreateInstanceOfInt16(rndGen),
PrimitiveCreator.CreateInstanceOfInt32(rndGen),
PrimitiveCreator.CreateInstanceOfInt64(rndGen),
PrimitiveCreator.CreateInstanceOfSByte(rndGen),
PrimitiveCreator.CreateInstanceOfSingle(rndGen),
PrimitiveCreator.CreateInstanceOfString(rndGen),
PrimitiveCreator.CreateInstanceOfUInt16(rndGen),
PrimitiveCreator.CreateInstanceOfUInt32(rndGen),
PrimitiveCreator.CreateInstanceOfUInt64(rndGen)
});
JsonArray newJson = (JsonArray)JsonValue.Parse(sourceJson.ToString());
Log.Info("Original JsonArray object is: {0}", sourceJson);
Log.Info("Round-tripped JsonArray object is: {0}", newJson);
Assert.True(JsonValueVerifier.Compare(sourceJson, newJson));
}
finally
{
CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
}
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="String"/>.
/// </summary>
[Fact]
public void ValidPrimitiveStringRoundTrip()
{
Assert.True(this.TestPrimitiveType("String"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="DateTime"/>.
/// </summary>
[Fact]
public void ValidPrimitiveDateTimeRoundTrip()
{
bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;
CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
try
{
Assert.True(this.TestPrimitiveType("DateTime"));
}
finally
{
CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
}
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Boolean"/>.
/// </summary>
[Fact]
public void ValidPrimitiveBooleanRoundTrip()
{
Assert.True(this.TestPrimitiveType("Boolean"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Byte"/>.
/// </summary>
[Fact]
public void ValidPrimitiveByteRoundTrip()
{
Assert.True(this.TestPrimitiveType("Byte"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Decimal"/>.
/// </summary>
[Fact]
public void ValidPrimitiveDecimalRoundTrip()
{
Assert.True(this.TestPrimitiveType("Decimal"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Double"/>.
/// </summary>
[Fact]
public void ValidPrimitiveDoubleRoundTrip()
{
Assert.True(this.TestPrimitiveType("Double"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Int16"/>.
/// </summary>
[Fact]
public void ValidPrimitiveInt16RoundTrip()
{
Assert.True(this.TestPrimitiveType("Int16"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Int32"/>.
/// </summary>
[Fact]
public void ValidPrimitiveInt32RoundTrip()
{
Assert.True(this.TestPrimitiveType("Int32"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Int64"/>.
/// </summary>
[Fact]
public void ValidPrimitiveInt64RoundTrip()
{
Assert.True(this.TestPrimitiveType("Int64"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="SByte"/>.
/// </summary>
[Fact]
public void ValidPrimitiveSByteRoundTrip()
{
Assert.True(this.TestPrimitiveType("SByte"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="UInt16"/>.
/// </summary>
[Fact]
public void ValidPrimitiveUInt16RoundTrip()
{
Assert.True(this.TestPrimitiveType("Uint16"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="UInt32"/>.
/// </summary>
[Fact]
public void ValidPrimitiveUInt32RoundTrip()
{
Assert.True(this.TestPrimitiveType("UInt32"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="UInt64"/>.
/// </summary>
[Fact]
public void ValidPrimitiveUInt64RoundTrip()
{
Assert.True(this.TestPrimitiveType("UInt64"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Char"/>.
/// </summary>
[Fact]
public void ValidPrimitiveCharRoundTrip()
{
Assert.True(this.TestPrimitiveType("Char"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Guid"/>.
/// </summary>
[Fact]
public void ValidPrimitiveGuidRoundTrip()
{
Assert.True(this.TestPrimitiveType("Guid"));
}
/// <summary>
/// Test for <see cref="JsonPrimitive"/> round-trip created via <see cref="Uri"/>.
/// </summary>
[Fact]
public void ValidPrimitiveUriRoundTrip()
{
Assert.True(this.TestPrimitiveType("Uri"));
}
/// <summary>
/// Tests for <see cref="JsonValue"/> round-trip created via <code>null</code> values.
/// </summary>
[Fact]
public void ValidPrimitiveNullRoundTrip()
{
Assert.True(this.TestPrimitiveType("Null"));
}
/// <summary>
/// Tests for round-tripping <see cref="JsonPrimitive"/> objects via casting to CLR instances.
/// </summary>
[Fact]
public void JsonValueRoundTripCastTests()
{
int seed = 1;
Log.Info("Seed: {0}", seed);
Random rndGen = new Random(seed);
this.DoRoundTripCasting(String.Empty, typeof(string));
this.DoRoundTripCasting("null", typeof(string));
string str;
do
{
str = PrimitiveCreator.CreateInstanceOfString(rndGen);
} while (str == null);
this.DoRoundTripCasting(str, typeof(string));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt16(rndGen), typeof(int));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt32(rndGen), typeof(int));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt64(rndGen), typeof(int));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt16(rndGen), typeof(int));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt32(rndGen), typeof(int));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt64(rndGen), typeof(int));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfGuid(rndGen), typeof(Guid));
this.DoRoundTripCasting(new Uri("http://bug/test?param=hello%0a"), typeof(Uri));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfChar(rndGen), typeof(char));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfBoolean(rndGen), typeof(bool));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDateTime(rndGen), typeof(DateTime));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen), typeof(DateTimeOffset));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDouble(rndGen), typeof(double));
this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDouble(rndGen), typeof(float));
this.DoRoundTripCasting(0.12345f, typeof(double));
this.DoRoundTripCasting(0.12345f, typeof(float));
}
private bool TestPrimitiveType(string typeName)
{
bool retValue = true;
bool specialCase = false;
int seed = 1;
Log.Info("Seed: {0}", seed);
Random rndGen = new Random(seed);
JsonPrimitive sourceJson = null;
JsonPrimitive sourceJson2;
object tempValue = null;
switch (typeName.ToLower())
{
case "boolean":
tempValue = PrimitiveCreator.CreateInstanceOfBoolean(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString().ToLower());
sourceJson2 = new JsonPrimitive((bool)tempValue);
break;
case "byte":
tempValue = PrimitiveCreator.CreateInstanceOfByte(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((byte)tempValue);
break;
case "char":
sourceJson2 = new JsonPrimitive((char)PrimitiveCreator.CreateInstanceOfChar(rndGen));
specialCase = true;
break;
case "datetime":
tempValue = PrimitiveCreator.CreateInstanceOfDateTime(rndGen);
sourceJson2 = new JsonPrimitive((DateTime)tempValue);
sourceJson = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
break;
case "decimal":
tempValue = PrimitiveCreator.CreateInstanceOfDecimal(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(((decimal)tempValue).ToString(NumberFormatInfo.InvariantInfo));
sourceJson2 = new JsonPrimitive((decimal)tempValue);
break;
case "double":
double tempDouble = PrimitiveCreator.CreateInstanceOfDouble(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempDouble.ToString("R", NumberFormatInfo.InvariantInfo));
sourceJson2 = new JsonPrimitive(tempDouble);
break;
case "guid":
sourceJson2 = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfGuid(rndGen));
specialCase = true;
break;
case "int16":
tempValue = PrimitiveCreator.CreateInstanceOfInt16(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((short)tempValue);
break;
case "int32":
tempValue = PrimitiveCreator.CreateInstanceOfInt32(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((int)tempValue);
break;
case "int64":
tempValue = PrimitiveCreator.CreateInstanceOfInt64(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((long)tempValue);
break;
case "sbyte":
tempValue = PrimitiveCreator.CreateInstanceOfSByte(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((sbyte)tempValue);
break;
case "single":
float fltValue = PrimitiveCreator.CreateInstanceOfSingle(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(fltValue.ToString("R", NumberFormatInfo.InvariantInfo));
sourceJson2 = new JsonPrimitive(fltValue);
break;
case "string":
do
{
tempValue = PrimitiveCreator.CreateInstanceOfString(rndGen);
} while (tempValue == null);
sourceJson2 = new JsonPrimitive((string)tempValue);
sourceJson = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
break;
case "uint16":
tempValue = PrimitiveCreator.CreateInstanceOfUInt16(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((ushort)tempValue);
break;
case "uint32":
tempValue = PrimitiveCreator.CreateInstanceOfUInt32(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((uint)tempValue);
break;
case "uint64":
tempValue = PrimitiveCreator.CreateInstanceOfUInt64(rndGen);
sourceJson = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
sourceJson2 = new JsonPrimitive((ulong)tempValue);
break;
case "uri":
Uri uri = null;
do
{
try
{
uri = PrimitiveCreator.CreateInstanceOfUri(rndGen);
}
catch (UriFormatException)
{
}
} while (uri == null);
sourceJson2 = new JsonPrimitive(uri);
specialCase = true;
break;
case "null":
sourceJson = (JsonPrimitive)JsonValue.Parse("null");
sourceJson2 = null;
break;
default:
sourceJson = null;
sourceJson2 = null;
break;
}
if (!specialCase)
{
// comparison between two constructors
if (!JsonValueVerifier.Compare(sourceJson, sourceJson2))
{
Log.Info("(JsonPrimitive)JsonValue.Parse(string) failed to match the results from default JsonPrimitive(obj)constructor for type {0}", typeName);
retValue = false;
}
if (sourceJson != null)
{
// test JsonValue.Load(TextReader)
JsonPrimitive newJson = null;
using (StringReader sr = new StringReader(sourceJson.ToString()))
{
newJson = (JsonPrimitive)JsonValue.Load(sr);
}
if (!JsonValueVerifier.Compare(sourceJson, newJson))
{
Log.Info("JsonValue.Load(TextReader) failed to function properly for type {0}", typeName);
retValue = false;
}
// test JsonValue.Load(Stream) is located in the JObjectFromGenoTypeLib test case
// test JsonValue.Parse(string)
newJson = null;
newJson = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString());
if (!JsonValueVerifier.Compare(sourceJson, newJson))
{
Log.Info("JsonValue.Parse(string) failed to function properly for type {0}", typeName);
retValue = false;
}
}
}
else
{
// test JsonValue.Load(TextReader)
JsonPrimitive newJson2 = null;
using (StringReader sr = new StringReader(sourceJson2.ToString()))
{
newJson2 = (JsonPrimitive)JsonValue.Load(sr);
}
if (!JsonValueVerifier.Compare(sourceJson2, newJson2))
{
Log.Info("JsonValue.Load(TextReader) failed to function properly for type {0}", typeName);
retValue = false;
}
// test JsonValue.Load(Stream) is located in the JObjectFromGenoTypeLib test case
// test JsonValue.Parse(string)
newJson2 = null;
newJson2 = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
if (!JsonValueVerifier.Compare(sourceJson2, newJson2))
{
Log.Info("JsonValue.Parse(string) failed to function properly for type {0}", typeName);
retValue = false;
}
}
return retValue;
}
private void DoRoundTripCasting(JsonValue jo, Type type)
{
bool result = false;
// Casting
if (jo.JsonType == JsonType.String)
{
JsonValue jstr = (string)jo;
if (type == typeof(DateTime))
{
Log.Info("{0} Value:{1}", type.Name, ((DateTime)jstr).ToString(DateTimeFormatInfo.InvariantInfo));
}
else if (type == typeof(DateTimeOffset))
{
Log.Info("{0} Value:{1}", type.Name, ((DateTimeOffset)jstr).ToString(DateTimeFormatInfo.InvariantInfo));
}
else if (type == typeof(Guid))
{
Log.Info("{0} Value:{1}", type.Name, (Guid)jstr);
}
else if (type == typeof(char))
{
Log.Info("{0} Value:{1}", type.Name, (char)jstr);
}
else if (type == typeof(Uri))
{
Log.Info("{0} Value:{1}", type.Name, ((Uri)jstr).AbsoluteUri);
}
else
{
Log.Info("{0} Value:{1}", type.Name, (string)jstr);
}
if (jo.ToString() == jstr.ToString())
{
result = true;
}
}
else if (jo.JsonType == JsonType.Object)
{
JsonObject jobj = new JsonObject((JsonObject)jo);
if (jo.ToString() == jobj.ToString())
{
result = true;
}
}
else if (jo.JsonType == JsonType.Number)
{
JsonPrimitive jprim = (JsonPrimitive)jo;
Log.Info("{0} Value:{1}", type.Name, jprim);
if (jo.ToString() == jprim.ToString())
{
result = true;
}
}
else if (jo.JsonType == JsonType.Boolean)
{
JsonPrimitive jprim = (JsonPrimitive)jo;
Log.Info("{0} Value:{1}", type.Name, (bool)jprim);
if (jo.ToString() == jprim.ToString())
{
result = true;
}
}
Assert.True(result);
}
}
}
|