Skip to content

Commit

Permalink
Started implementation
Browse files Browse the repository at this point in the history
Started implementation
  • Loading branch information
rgomez90 committed Feb 20, 2021
1 parent 3558d59 commit 7b64ddc
Show file tree
Hide file tree
Showing 15 changed files with 753 additions and 0 deletions.
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

0 comments on commit 7b64ddc

Please sign in to comment.