2

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).

Christian Casutt
  • 2,334
  • 4
  • 29
  • 38
  • Seems it's on the [road-map](https://github.com/dotnet/runtime/issues/29887). There are 3rd party solutions available. – Andy Apr 21 '21 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. – DavidG Apr 21 '21 at 15:26

1 Answers1

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);
Sergio A.
  • 403
  • 2
  • 9