Skip to content

WIP: Add serialization support for System.Text.Json #905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

namespace UnitsNet.Serialization.SystemTextJson.Tests.Infrastructure
{
public sealed class TestObject
{
public Frequency? NullableFrequency { get; set; }
public Frequency NonNullableFrequency { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;

namespace UnitsNet.Serialization.SystemTextJson.Tests.Infrastructure
{
public sealed class TestObjectWithIComparable
{
public IComparable Value { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;

namespace UnitsNet.Serialization.SystemTextJson.Tests.Infrastructure
{
public sealed class TestObjectWithThreeIComparable
{
public IComparable Value1 { get; set; }

public IComparable Value2 { get; set; }

public IComparable Value3 { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;

namespace UnitsNet.Serialization.SystemTextJson.Tests.Infrastructure
{
public sealed class TestObjectWithValueAndUnitAsIComparable : IComparable
{
public double Value { get; set; }
public string Unit { get; set; }

public int CompareTo(object obj)
{
return ((IComparable)Value).CompareTo(obj);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;

namespace UnitsNet.Serialization.SystemTextJson.Tests.Infrastructure
{
public sealed class TestObjectWithValueAsIComparable : IComparable
{
public int Value { get; set; }

public int CompareTo(object obj)
{
return ((IComparable)Value).CompareTo(obj);
}

// Needed for verifying that the deserialized object is the same, should not affect the serialization code
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return Value.Equals(((TestObjectWithValueAsIComparable)obj).Value);
}

public override int GetHashCode()
{
return Value.GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System.Text.Json;

namespace UnitsNet.Serialization.SystemTextJson.Tests.Infrastructure
{
public abstract class UnitsNetJsonBaseTest
{
private readonly JsonSerializerOptions _jsonSerializerSettings;

protected UnitsNetJsonBaseTest()
{
_jsonSerializerSettings = new JsonSerializerOptions {WriteIndented = true};
_jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverterFactory());
//_jsonSerializerSettings.Converters.Add(new UnitsNetIQuantityJsonConverter());
//_jsonSerializerSettings.Converters.Add(new UnitsNetIComparableJsonConverter());
}

protected string SerializeObject(object obj)
{
return JsonSerializer.Serialize(obj, _jsonSerializerSettings).Replace("\r\n", "\n");
}

protected T DeserializeObject<T>(string json)
{ return JsonSerializer.Deserialize<T>(json, _jsonSerializerSettings);
}
}
}
8 changes: 8 additions & 0 deletions UnitsNet.Serialization.SystemTextJson.Tests/TestObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UnitsNet.Serialization.SystemTextJson.Tests
{
public sealed class TestObject
{
public Frequency? NullableFrequency { get; set; }
public Frequency NonNullableFrequency { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UnitsNet.Serialization.SystemTextJson\UnitsNet.Serialization.SystemTextJson.csproj" />
<ProjectReference Include="..\UnitsNet\UnitsNet.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json;

namespace UnitsNet.Serialization.SystemTextJson.Tests
{
public abstract class UnitsNetJsonBaseTest
{
private readonly JsonSerializerOptions _jsonSerializerSettings;

protected UnitsNetJsonBaseTest()
{
_jsonSerializerSettings = new JsonSerializerOptions {WriteIndented = true};
_jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverterFactory());
//_jsonSerializerSettings.Converters.Add(new UnitsNetIQuantityJsonConverter());
//_jsonSerializerSettings.Converters.Add(new UnitsNetIComparableJsonConverter());
}

protected string SerializeObject(object obj)
{
return JsonSerializer.Serialize(obj, _jsonSerializerSettings).Replace("\r\n", "\n");
}

protected T DeserializeObject<T>(string json)
{ return JsonSerializer.Deserialize<T>(json, _jsonSerializerSettings);
}
}
}
Loading