Created
September 28, 2018 05:32
-
-
Save ssippe/a3faa95c9c6807471be637a7ff22c187 to your computer and use it in GitHub Desktop.
json serialization and immuatability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using Newtonsoft.Json; | |
using Shouldly; | |
using Xunit; | |
namespace json | |
{ | |
public class UnitTest1 | |
{ | |
public class Person | |
{ | |
public Person(string firstname, string lastname, DateTime dateOfBirth) | |
{ | |
Firstname = firstname; | |
Lastname = lastname; | |
DateOfBirth = dateOfBirth; | |
} | |
public string Firstname { get; } | |
public string Lastname { get; } | |
public string Fullname => $"{Firstname} {Lastname}"; | |
public DateTime DateOfBirth { get; } | |
public override string ToString() | |
{ | |
return $"{Fullname} {DateOfBirth:dd-MMM-yyyy}"; | |
} | |
} | |
[Fact] | |
public void Test1() | |
{ | |
var p1 = new Person("James", "Bond", new DateTime(1950, 1, 1)); | |
var p1Json = JsonConvert.SerializeObject(p1); | |
Debug.WriteLine(p1Json); | |
p1Json.ShouldBe(@"{""Firstname"":""James"",""Lastname"":""Bond"",""Fullname"":""James Bond"",""DateOfBirth"":""1950-01-01T00:00:00""}"); | |
var p2Json = @"{""Lastname"":""Lieter"",""Firstname"":""Felix"",""DateOfBirth"":""1951-01-01T00:00:00""}"; | |
var p2 = JsonConvert.DeserializeObject<Person>(p2Json); | |
Debug.WriteLine("p2=" + p2); | |
p2.ToString().ShouldBe("Felix Lieter 01-Jan-1951"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment