Assert.Equal(0, i.Length);
}
+ [ActiveIssue(38435)]
+ [Fact]
+ public static void ReadInitializedArrayTest()
+ {
+ string serialized = "{\"Values\":[1,2,3]}";
+ TestClassWithInitializedArray testClassWithInitializedArray = JsonSerializer.Parse<TestClassWithInitializedArray>(serialized);
+
+ Assert.Equal(1, testClassWithInitializedArray.Values[0]);
+ Assert.Equal(2, testClassWithInitializedArray.Values[1]);
+ Assert.Equal(3, testClassWithInitializedArray.Values[2]);
+ }
+
[Fact]
public static void ReadArrayWithEnums()
{
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Collections;
using System.Collections.Generic;
using Xunit;
Assert.Equal("Value", indexer.NonIndexerProp);
Assert.Equal(-1, indexer[0]);
}
+
+ [ActiveIssue(38414)]
+ [Fact]
+ public static void ReadSimpleStructWithSimpleClass()
+ {
+ SimpleStructWithSimpleClass testObject = new SimpleStructWithSimpleClass();
+ testObject.Initialize();
+
+ string json = JsonSerializer.ToString(testObject, testObject.GetType());
+ SimpleStructWithSimpleClass obj = JsonSerializer.Parse<SimpleStructWithSimpleClass>(json);
+ obj.Verify();
+ }
+
+ [ActiveIssue(38490)]
+ [Fact]
+ public static void ReadSimpleTestStructWithSimpleTestClass()
+ {
+ SimpleTestStruct testObject = new SimpleTestStruct();
+ testObject.Initialize();
+ testObject.MySimpleTestClass = new SimpleTestClass { MyString = "Hello", MyDouble = 3.14 } ;
+
+ string json = JsonSerializer.ToString(testObject);
+ SimpleTestStruct parsedObject = JsonSerializer.Parse<SimpleTestStruct>(json);
+ parsedObject.Verify();
+ }
+
+ [Fact]
+ public static void ReadSimpleTestClassWithSimpleTestStruct()
+ {
+ SimpleTestClass testObject = new SimpleTestClass();
+ testObject.Initialize();
+ testObject.MySimpleTestStruct = new SimpleTestStruct { MyInt64 = 64, MyString = "Hello", MyInt32Array = new int[] { 32 } };
+
+ string json = JsonSerializer.ToString(testObject);
+ SimpleTestClass parsedObject = JsonSerializer.Parse<SimpleTestClass>(json);
+ parsedObject.Verify();
+ }
+
+ [ActiveIssue(38414)]
+ [Fact]
+ public static void OuterClassHavingPropertiesDefinedAfterClassWithDictionaryTest()
+ {
+ OuterClassHavingPropertiesDefinedAfterClassWithDictionary testObject = new OuterClassHavingPropertiesDefinedAfterClassWithDictionary { MyInt = 10, MyIntArray = new int[] { 3 },
+ MyDouble= 3.14, MyList =new List<string> { "Hello" }, MyString = "World", MyInnerTestClass = new SimpleClassWithDictionary { MyInt = 2 } };
+ string json = JsonSerializer.ToString(testObject);
+
+ OuterClassHavingPropertiesDefinedAfterClassWithDictionary parsedObject = JsonSerializer.Parse<OuterClassHavingPropertiesDefinedAfterClassWithDictionary>(json);
+
+ Assert.Equal(3.14, parsedObject.MyDouble);
+ Assert.Equal(10, parsedObject.MyInt);
+ Assert.Equal("World", parsedObject.MyString);
+ Assert.Equal("Hello", parsedObject.MyList[0]);
+ Assert.Equal(3, parsedObject.MyIntArray[0]);
+ Assert.Equal(2, parsedObject.MyInnerTestClass.MyInt);
+ }
+
+ [ActiveIssue(38435)]
+ [Fact]
+ public static void ReadStructWithSimpleClassValueTest()
+ {
+ string json = "{\"MySimpleTestClass\":{\"MyInt32Array\":[1],\"MyStringToStringDict\":null,\"MyStringToStringIDict\":null},\"MyInt32Array\":[2]}";
+ SimpleTestStruct obj3 = JsonSerializer.Parse<SimpleTestStruct>(json);
+ }
}
}
public SampleEnum MyEnum { get; set; }
public SampleEnumInt16 MyInt16Enum { get; set; }
public SampleEnumInt64 MyInt64Enum { get; set; }
- public SampleEnumUInt32 MyUInt32Enum { get; set; }
public SampleEnumUInt16 MyUInt16Enum { get; set; }
+ public SampleEnumUInt32 MyUInt32Enum { get; set; }
public SampleEnumUInt64 MyUInt64Enum { get; set; }
+ public SimpleStruct MySimpleStruct { get; set; }
+ public SimpleTestStruct MySimpleTestStruct { get; set; }
public short[] MyInt16Array { get; set; }
public int[] MyInt32Array { get; set; }
public long[] MyInt64Array { get; set; }
@"""MyGuid"" : ""1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6""," +
@"""MyUri"" : ""https:\/\/github.com\/dotnet\/corefx""," +
@"""MyEnum"" : 2," + // int by default
- //@"""MyStringToStringKeyValuePair"" : {""Key"" : ""myKey"", ""Value"" : ""myValue""}," +
+ @"""MyInt64Enum"" : -9223372036854775808," +
+ @"""MyUInt64Enum"" : 18446744073709551615," +
+ //@"""MyStringToStringKeyValuePair"" : {""Key"" : ""myKey"", ""Value"" : ""myValue""}," +
@"""MyStringToStringIDict"" : {""key"" : ""value""}," +
@"""MyStringToStringGenericDict"" : {""key"" : ""value""}," +
@"""MyStringToStringGenericIDict"" : {""key"" : ""value""}," +
@"""MyStringToStringGenericIReadOnlyDict"" : {""key"" : ""value""}," +
@"""MyStringToStringImmutableDict"" : {""key"" : ""value""}," +
@"""MyStringToStringIImmutableDict"" : {""key"" : ""value""}," +
- @"""MyStringToStringImmutableSortedDict"" : {""key"" : ""value""}";
+ @"""MyStringToStringImmutableSortedDict"" : {""key"" : ""value""}," +
+ @"""MySimpleStruct"" : {""One"" : 11, ""Two"" : 1.9999, ""Three"" : 33}," +
+ @"""MySimpleTestStruct"" : {""MyInt64"" : 64, ""MyString"" :""Hello"", ""MyInt32Array"" : [32]}";
private const string s_partialJsonArrays =
@"""MyInt16Array"" : [1]," +
MyGuid = new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6");
MyUri = new Uri("https://github.com/dotnet/corefx");
MyEnum = SampleEnum.Two;
-
+ MyInt64Enum = SampleEnumInt64.MinNegative;
+ MyUInt64Enum = SampleEnumUInt64.Max;
MyInt16Array = new short[] { 1 };
MyInt32Array = new int[] { 2 };
MyInt64Array = new long[] { 3 };
MyGuidArray = new Guid[] { new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6") };
MyUriArray = new Uri[] { new Uri("https://github.com/dotnet/corefx") };
MyEnumArray = new SampleEnum[] { SampleEnum.Two };
+ MySimpleStruct = new SimpleStruct { One = 11, Two = 1.9999 };
+ MySimpleTestStruct = new SimpleTestStruct { MyInt64 = 64, MyString = "Hello", MyInt32Array = new int[] { 32 } };
MyInt16TwoDimensionArray = new int[2][];
MyInt16TwoDimensionArray[0] = new int[] { 10, 11 };
Assert.Equal(new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6"), MyGuid);
Assert.Equal(new Uri("https://github.com/dotnet/corefx"), MyUri);
Assert.Equal(SampleEnum.Two, MyEnum);
+ Assert.Equal(SampleEnumInt64.MinNegative, MyInt64Enum);
+ Assert.Equal(SampleEnumUInt64.Max, MyUInt64Enum);
+ Assert.Equal(11, MySimpleStruct.One);
+ Assert.Equal(1.9999, MySimpleStruct.Two);
+ Assert.Equal(64, MySimpleTestStruct.MyInt64);
+ Assert.Equal("Hello", MySimpleTestStruct.MyString);
+ Assert.Equal(32, MySimpleTestStruct.MyInt32Array[0]);
Assert.Equal((short)1, MyInt16Array[0]);
Assert.Equal((int)2, MyInt32Array[0]);
public object MyDateTime { get; set; }
public object MyGuid { get; set; }
public object MyEnum { get; set; }
+ public object MyStruct { get; set; }
public static readonly string s_json =
@"{" +
@"""MyDecimal"" : 3.3," +
@"""MyDateTime"" : ""2019-01-30T12:01:02.0000000Z""," +
@"""MyGuid"" : ""5BB9D872-DA8A-471E-AA70-08E19102683D""," +
- @"""MyEnum"" : 2" + // int by default
+ @"""MyEnum"" : 2," + // int by default
+ @"""MyStruct"" : { ""One"" : 1, ""Two"" : 3.14 }" +
@"}";
public static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json);
MyDateTime = new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc);
MyGuid = new Guid("5BB9D872-DA8A-471E-AA70-08E19102683D");
MyEnum = SampleEnum.Two;
+ MyStruct = new SimpleStruct { One = 1, Two = 3.14 };
}
public virtual void Verify()
Assert.IsType<JsonElement>(MyEnum);
Assert.Equal(JsonValueType.Number, ((JsonElement)MyEnum).Type);
Assert.Equal(SampleEnum.Two, (SampleEnum)((JsonElement)MyEnum).GetUInt32());
+ Assert.Equal(1, ((JsonElement)MyStruct).GetProperty("One").GetInt32());
+ Assert.Equal(3.14, ((JsonElement)MyStruct).GetProperty("Two").GetDouble());
}
}
}
namespace System.Text.Json.Serialization.Tests
{
- public class SimpleTestStruct : ITestClass
+ public struct SimpleTestStruct : ITestClass
{
public short MyInt16 { get; set; }
public int MyInt32 { get; set; }
public DateTime MyDateTime { get; set; }
public DateTimeOffset MyDateTimeOffset { get; set; }
public SampleEnum MyEnum { get; set; }
+ public SampleEnumInt64 MyInt64Enum { get; set; }
+ public SampleEnumUInt64 MyUInt64Enum { get; set; }
+ public SimpleStruct MySimpleStruct { get; set; }
+ public SimpleTestClass MySimpleTestClass { get; set; }
public short[] MyInt16Array { get; set; }
public int[] MyInt32Array { get; set; }
public long[] MyInt64Array { get; set; }
@"""MyDecimal"" : 3.3," +
@"""MyDateTime"" : ""2019-01-30T12:01:02.0000000Z""," +
@"""MyDateTimeOffset"" : ""2019-01-30T12:01:02.0000000+01:00""," +
- @"""MyEnum"" : 2"; // int by default
+ @"""MyEnum"" : 2," + // int by default
+ @"""MyInt64Enum"" : -9223372036854775808," +
+ @"""MyUInt64Enum"" : 18446744073709551615," +
+ @"""MySimpleStruct"" : {""One"" : 11, ""Two"" : 1.9999, ""Three"" : 33}";
private const string s_partialJsonArrays =
@"""MyInt16Array"" : [1]," +
MyDateTime = new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc);
MyDateTimeOffset = new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0));
MyEnum = SampleEnum.Two;
+ MyInt64Enum = SampleEnumInt64.MinNegative;
+ MyUInt64Enum = SampleEnumUInt64.Max;
+ MySimpleStruct = new SimpleStruct { One = 11, Two = 1.9999 };
MyInt16Array = new short[] { 1 };
MyInt32Array = new int[] { 2 };
Assert.Equal(new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc), MyDateTime);
Assert.Equal(new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)), MyDateTimeOffset);
Assert.Equal(SampleEnum.Two, MyEnum);
+ Assert.Equal(SampleEnumInt64.MinNegative, MyInt64Enum);
+ Assert.Equal(SampleEnumUInt64.Max, MyUInt64Enum);
+ Assert.Equal(11, MySimpleStruct.One);
+ Assert.Equal(1.9999, MySimpleStruct.Two);
Assert.Equal((short)1, MyInt16Array[0]);
Assert.Equal((int)2, MyInt32Array[0]);
Max = ulong.MaxValue
}
+ public struct SimpleStruct
+ {
+ public int One { get; set; }
+ public double Two { get; set; }
+ }
+
+ public struct SimpleStructWithSimpleClass: ITestClass
+ {
+ public short MyInt32 { get; set; }
+ public SimpleTestClass MySimpleClass { get; set; }
+ public int[] MyInt32Array { get; set; }
+
+ public static readonly string s_json =
+ @"{" +
+ @"""MySimpleClass"" : {""MyString"" : ""Hello"", ""MyDouble"" : 3.14}," +
+ @"""MyInt32"" : 32," +
+ @"""MyInt32Array"" : [32]" +
+ @"}";
+
+ public void Initialize()
+ {
+ MySimpleClass = new SimpleTestClass { MyString = "Hello", MyDouble = 3.14 };
+ MyInt32 = 32;
+ MyInt32Array = new int[] { 32 };
+ }
+
+ public void Verify()
+ {
+ Assert.Equal(32, MyInt32);
+ Assert.Equal(32, MyInt32Array[0]);
+ Assert.Equal("Hello", MySimpleClass.MyString);
+ Assert.Equal(3.14, MySimpleClass.MyDouble);
+ }
+ }
+
public class TestClassWithNull
{
public string MyString { get; set; }
}
}
+ public class TestClassWithInitializedArray
+ {
+ public int[] Values { get; set; }
+
+ public TestClassWithInitializedArray()
+ {
+ Values = Array.Empty<int>();
+ }
+ }
+
+ public class SimpleClassWithDictionary
+ {
+ public int MyInt { get; set; }
+ public Dictionary<string, string> MyDictionary { get; set; }
+ }
+ public class OuterClassHavingPropertiesDefinedAfterClassWithDictionary
+ {
+ public double MyDouble { get; set; }
+ public SimpleClassWithDictionary MyInnerTestClass { get; set; }
+ public int MyInt { get; set; }
+ public string MyString { get; set; }
+ public List<string> MyList { get; set; }
+ public int[] MyIntArray { get; set; }
+ }
+
public class TestClassWithStringArray : ITestClass
{
public string[] MyData { get; set; }