4

In .NET Core 5.0, using System.Text.Json.JsonSerializer Deserialize(someJsonFile) i get:

System.Text.Json.JsonException: 'The JSON value could not be converted to System.Guid. Path: $.someGuid | ..

which is expected, since the the someGuid property is of type System.Guid and the value of someGuid in the JSON file/string is:

{
  "someGuid": ""
}

which can not be deserialized properly .. (since it's not Guid.Empty)..

To my question.

What's a good and generic implementation to validate the Json before deserializing it (in general)? like TryParse or JsonDocument.Parse? sure, try-catch but that's dirty (imho).

btw: i don't want to use Newtonsoft

thanks for your suggestions (and critics of course).

2
  • Seems it's on the road-map. There are 3rd party solutions available. Commented Apr 21, 2021 at 15:16
  • I think you're stuck with try/catch or implementing your own converters for each type and some post-deserialisation validation code. Commented Apr 21, 2021 at 15:26

1 Answer 1

1

I created a custom converter using the example is this answer: The JSON value could not be converted to System.Int32

public class StringToGuidConverter : JsonConverter<Guid>
    {
        public override Guid Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.String)
            {
                ReadOnlySpan<byte> span = reader.ValueSpan;
                if (Utf8Parser.TryParse(span, out Guid guid, out int bytesConsumed) && span.Length == bytesConsumed)
                {
                    return guid;
                }

                if (Guid.TryParse(reader.GetString(), out guid))
                {
                    return guid;
                }
            }

            return Guid.Empty;
        }

        public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString());
        }
    }

In my case my model to deserialize to can't take Nullable Guid so I return an empty GUID and then validate this in my logic.

Because I'm create a web api using .Net standard, I can't register this in the services at the startup class. But you can register the custom converter using the JsonSerializerOptions property when calling the Deserialize method like this:

var options = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    Converters = { new NERDS.API.Helpers.StringToGuidConverter() }
};

StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string json = reader.ReadToEnd();
return JsonSerializer.Deserialize<T>(json, options);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.