Skip to content

Instantly share code, notes, and snippets.

@KenBonny
Created May 24, 2021 15:57
Show Gist options
  • Save KenBonny/de398822839c8f01409f7f7d935b1cd3 to your computer and use it in GitHub Desktop.
Save KenBonny/de398822839c8f01409f7f7d935b1cd3 to your computer and use it in GitHub Desktop.
using System;
using Newtonsoft.Json;
using Shouldly;
using Xunit;
namespace TestSolution.UnitTests
{
public class ImplicitConversionTests
{
private const string ExampleNumber = "+32 1234 567 890";
[Fact]
public void String_to_telephone()
{
Telephone number = ExampleNumber;
number.Number.ShouldBe(ExampleNumber);
}
[Fact]
public void Telephone_to_string()
{
Telephone number = new(ExampleNumber);
string original = number;
original.ShouldBe(ExampleNumber);
}
[Fact]
public void Telephone_to_number()
{
Telephone number = new(ExampleNumber);
int length = number;
length.ShouldBe(ExampleNumber.Length);
}
[Fact]
public void Deserialize_from_json()
{
const string json = "{\"Name\": \"Ken\", \"BirthDate\": \"1987-01-01\", \"Telephone\": \"+32 1234 567 890\"}";
var person = JsonConvert.DeserializeObject<Person>(json);
person.ShouldNotBeNull();
person.Name.ShouldBe("Ken");
person.BirthDate.ShouldBe(new DateTimeOffset(new DateTime(1987, 1, 1)));
person.Telephone.Number.ShouldBe("+32 1234 567 890");
}
[Fact]
public void Deserialize_extended_from_json()
{
const string json = "{\"Name\": \"Ken\", \"BirthDate\": \"1987-01-01\", \"Telephone\": {\"Number\": \"+32 1234 567 890\"}}";
var person = JsonConvert.DeserializeObject<Person>(json);
person.ShouldNotBeNull();
person.Name.ShouldBe("Ken");
person.BirthDate.ShouldBe(new DateTimeOffset(new DateTime(1987, 1, 1)));
person.Telephone.Number.ShouldBe("+32 1234 567 890");
}
[Fact]
public void Serialize_to_json()
{
const string expected = "{\"Name\":\"Ken\",\"BirthDate\":\"1987-01-01T00:00:00+01:00\",\"Telephone\":{\"Number\":\"+32 1234 567 890\"}}";
Person person = new()
{
Name = "Ken", BirthDate = new DateTime(1987, 1, 1), Telephone = "+32 1234 567 890"
};
var json = JsonConvert.SerializeObject(person);
json.ShouldBe(expected);
}
[Fact]
public void Serialize_with_converter_to_json()
{
const string expected = "{\"Name\":\"Ken\",\"BirthDate\":\"1987-01-01T00:00:00+01:00\",\"Telephone\":\"+32 1234 567 890\"}";
Person person = new()
{
Name = "Ken", BirthDate = new DateTime(1987, 1, 1), Telephone = "+32 1234 567 890"
};
var json = JsonConvert.SerializeObject(person, new TelephoneJsonConverter());
json.ShouldBe(expected);
}
}
class Person
{
public string Name { get; set; }
public DateTimeOffset BirthDate { get; set; }
public Telephone Telephone { get; set; }
}
class Telephone
{
public string Number { get; }
public Telephone(string number)
{
Number = number;
}
public override string ToString() => Number;
public static implicit operator Telephone(string number) => new(number);
public static implicit operator string(Telephone number) => number.ToString();
public static implicit operator int(Telephone number) => number.Number.Length;
}
class TelephoneJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (value is not Telephone number)
return;
writer.WriteValue(number.ToString());
}
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type type) => type == typeof(Telephone);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment