// 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 System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using Xunit;
namespace System.Json
{
///
/// JsonPrimitive unit tests
///
public class JsonPrimitiveTests
{
const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffK";
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromInt16()
{
short[] values = new short[] { Int16.MinValue, Int16.MaxValue, 1 };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromInt32()
{
int[] values = new int[] { Int32.MinValue, Int32.MaxValue, 12345678 };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromInt64()
{
long[] values = new long[] { Int64.MinValue, Int64.MaxValue, 12345678901232L };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromUInt64()
{
ulong[] values = new ulong[] { UInt64.MinValue, UInt64.MaxValue, 12345678901232L };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromUInt32()
{
uint[] values = new uint[] { UInt32.MinValue, UInt32.MaxValue, 3234567890 };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromUInt16()
{
ushort[] values = new ushort[] { UInt16.MinValue, UInt16.MaxValue, 33333 };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromByte()
{
byte[] values = new byte[] { Byte.MinValue, Byte.MaxValue, 0x83 };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromSByte()
{
sbyte[] values = new sbyte[] { SByte.MinValue, SByte.MaxValue, -0x33 };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromFloat()
{
float[] values = new float[] { float.MinValue, float.MaxValue, 1.234f, float.PositiveInfinity, float.NegativeInfinity, float.NaN };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromDouble()
{
double[] values = new double[] { double.MinValue, double.MaxValue, 1.234, double.PositiveInfinity, double.NegativeInfinity, double.NaN };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromDecimal()
{
decimal[] values = new decimal[] { decimal.MinValue, decimal.MaxValue, 123456789.123456789m };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Number);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromBoolean()
{
bool[] values = new bool[] { true, false };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.Boolean);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromChar()
{
char[] values = new char[] { 'H', '\0', '\uffff' };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.String);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromString()
{
string[] values = new string[] { "Hello", "abcdef", "\r\t123\n32" };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.String);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromDateTime()
{
DateTime[] values = new DateTime[]
{
new DateTime(2000, 10, 16, 8, 0, 0, DateTimeKind.Utc),
new DateTime(2000, 10, 16, 8, 0, 0, DateTimeKind.Local),
};
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.String);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromUri()
{
Uri[] values = new Uri[] { new Uri("http://tempuri.org"), new Uri("foo/bar", UriKind.Relative) };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.String);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from values.
///
[Fact]
public void JsonPrimitiveFromGuid()
{
Guid[] values = new Guid[] { Guid.NewGuid(), Guid.Empty, Guid.NewGuid() };
for (int i = 0; i < values.Length; i++)
{
this.ValidateJson(new JsonPrimitive(values[i]), GetExpectedRepresentation(values[i]), JsonType.String);
this.TestReadAsRoundtrip(new JsonPrimitive(values[i]), values[i]);
}
}
///
/// Validates round-trip of values created from different types of values.
///
[Fact]
public void JsonPrimitiveFromObject()
{
List> values = new List>
{
new KeyValuePair