public void GuidIdSerializeToJSON(GuidStronglyTypedIdSerializationTestData inTestData) { // GUIDS are random, two sets of test data have fixed, non-random guids, the rest are random if (inTestData.SerializedTestData.StartsWith("\"0000", System.StringComparison.InvariantCulture) || inTestData.SerializedTestData.StartsWith("\"01234", System.StringComparison.InvariantCulture)) { // SerializationFixtureSystemTextJson.Serializer.Serialize(inTestData.InstanceTestData).Should().Be(inTestData.SerializedTestData); JsonSerializer.Serialize(inTestData.InstanceTestData, SerializationFixture.JsonSerializerOptions).Should().Be(inTestData.SerializedTestData); } else { //SerializationFixtureSystemTextJson.Serializer.Serialize(inTestData.InstanceTestData).Should().MatchRegex("^[0-9A-Fa-f]{8}-?([0-9A-Fa-f]{4}-?){3}[0-9A-Fa-f]{12}$"); JsonSerializer.Serialize(inTestData.InstanceTestData, SerializationFixture.JsonSerializerOptions).Should().MatchRegex("^\"[0-9A-Fa-f]{8}-?([0-9A-Fa-f]{4}-?){3}[0-9A-Fa-f]{12}\"$"); } }
public void GuidIdDeserializeFromJSON(GuidStronglyTypedIdSerializationTestData inTestData) { if (String.IsNullOrEmpty(inTestData.SerializedTestData)) { Action act = () => JsonSerializer.Deserialize <GuidStronglyTypedId>(inTestData.SerializedTestData, SerializationFixture.JsonSerializerOptions); act.Should().Throw <System.Text.Json.JsonException>() .WithMessage("The input does not contain any JSON tokens.*"); } else if (inTestData.SerializedTestData.StartsWith("\"0000", System.StringComparison.InvariantCulture) || inTestData.SerializedTestData.StartsWith("\"01234", System.StringComparison.InvariantCulture)) { //SerializationFixtureSystemTextJson.Serializer.Deserialize<GuidStronglyTypedId>(inTestData.SerializedTestData).Should().BeEquivalentTo(inTestData.InstanceTestData); var stronglyTypedId = JsonSerializer.Deserialize <GuidStronglyTypedId>(inTestData.SerializedTestData, SerializationFixture.JsonSerializerOptions); stronglyTypedId.Should().BeEquivalentTo(inTestData.InstanceTestData); } else { // ToDo: validate that strings that don't match a Guid throw an exception } }