public void DeserializeCustomUnitAsPredefined_ShouldReturnValidResult() { // arrange var someUnit = new AngleUnit( name: "some unit", abbreviation: "su", symbol: "?", unitsPerTurn: (number)123.456m); string json = @"{ 'unit': 'su' }"; var converter = new AngleUnitJsonConverter( serializationFormat: LinearUnitJsonSerializationFormat.PredefinedAsString, tryReadCustomPredefinedUnit: (string value, out AngleUnit predefinedUnit) => { if (value == someUnit.Abbreviation) { predefinedUnit = someUnit; return(true); } predefinedUnit = default(AngleUnit); return(false); }); // act var result = JsonConvert.DeserializeObject <SomeUnitOwner <AngleUnit> >(json, converter); // assert result.Unit.Should().Be(someUnit); }
public void SerializePredefinedUnit_ShouldReturnValidJson(LinearUnitJsonSerializationFormat serializationFormat, string expectedJson) { // arrange var unit = AngleUnit.Degree; var converter = new AngleUnitJsonConverter(serializationFormat); // act string actualJson = JsonConvert.SerializeObject(new SomeUnitOwner <AngleUnit> { Unit = unit }, converter); // assert actualJson.Should().Be(expectedJson); }
public void DeserializePredefinedUnitString_ShouldReturnValidResult(AngleUnit expectedUnit) { // arrange string json = $@"{{ 'unit': '{expectedUnit.Abbreviation}' }}"; var converter = new AngleUnitJsonConverter(); // act var result = JsonConvert.DeserializeObject <SomeUnitOwner <AngleUnit> >(json, converter); // assert result.Unit.Should().Be(expectedUnit); }
public void DeserializeCustomUnit_ShouldReturnValidResult() { // arrange string json = @"{ 'name': 'some unit', 'abbreviation': 'su', 'symbol': '?', 'unitsPerTurn': 123.456 }"; var converter = new AngleUnitJsonConverter(); // act var result = JsonConvert.DeserializeObject <AngleUnit>(json, converter); // assert result.Name.Should().Be("some unit"); result.Abbreviation.Should().Be("su"); result.Symbol.Should().Be("?"); result.UnitsPerTurn.Should().Be((number)123.456m); }
public void SerializeAndDeserialize_ShouldBeIdempotent(LinearUnitJsonSerializationFormat serializationFormat) { // arrange var obj = new SomeUnitOwner <AngleUnit> { Unit = Fixture.Create <AngleUnit>() }; var converter = new AngleUnitJsonConverter(serializationFormat); // act string serializedObj1 = JsonConvert.SerializeObject(obj, converter); var deserializedObj1 = JsonConvert.DeserializeObject <SomeUnitOwner <AngleUnit> >(serializedObj1, converter); string serializedObj2 = JsonConvert.SerializeObject(deserializedObj1, converter); var deserializedObj2 = JsonConvert.DeserializeObject <SomeUnitOwner <AngleUnit> >(serializedObj2, converter); // assert deserializedObj1.Unit.Should().Be(obj.Unit); deserializedObj2.Unit.Should().Be(obj.Unit); deserializedObj2.Unit.Should().Be(deserializedObj1.Unit); serializedObj2.Should().Be(serializedObj1); }