public void ShouldWriteJsonAndDeserializeIntoSameObject() { MathDomain original = new MathDomain(); ValueDomainSegment vds = new ValueDomainSegment(1024); RangeDomainSegment rds = new RangeDomainSegment(new Random800_90(), 0, 1024, 128); original.AddSegment(vds); original.AddSegment(rds); var serializedObject = JsonConvert.SerializeObject( original, new JsonSerializerSettings { Converters = new List <JsonConverter> { new DomainConverter() }, NullValueHandling = NullValueHandling.Ignore } ); var deserializedObject = JsonConvert.DeserializeObject <MathDomain>( serializedObject, new DomainConverter() ); var vdsReObjectified = deserializedObject.DomainSegments.OfType <ValueDomainSegment>().First(); var rdsReObjectified = deserializedObject.DomainSegments.OfType <RangeDomainSegment>().First(); Assert.AreEqual(original.DomainSegments.Count(), deserializedObject.DomainSegments.Count(), "count"); Assert.AreEqual(vds.ToString(), vdsReObjectified.ToString(), nameof(vds)); Assert.AreEqual(rds.ToString(), rdsReObjectified.ToString(), nameof(rds)); }
public void ShouldWriteJsonAndDeserializeIntoSameObject() { MathDomain original = new MathDomain(); ValueDomainSegment vds = new ValueDomainSegment(1024); RangeDomainSegment rds = new RangeDomainSegment(new Random800_90(), 0, 1024, 128); original.AddSegment(vds); original.AddSegment(rds); var serializedObject = JsonSerializer.Serialize(original, _jsonSerializerOptions); var deserializedObject = JsonSerializer.Deserialize <MathDomain>(serializedObject, _jsonSerializerOptions); Assert.IsNotNull(deserializedObject, $"{nameof(deserializedObject)} was null."); var vdsReObjectified = deserializedObject.DomainSegments.OfType <ValueDomainSegment>().First(); var rdsReObjectified = deserializedObject.DomainSegments.OfType <RangeDomainSegment>().First(); Assert.AreEqual(original.DomainSegments.Count(), deserializedObject.DomainSegments.Count(), "count"); Assert.AreEqual(vds.ToString(), vdsReObjectified.ToString(), nameof(vds)); Assert.AreEqual(rds.ToString(), rdsReObjectified.ToString(), nameof(rds)); }