NewtonSoft Json DeserializeObject Empty Guide Field

I am using ASP.NET MVC C # with jQuery KnockoutJs HTML CSS interface.

I have a modal contact form on my HTML page. The idea is that if I create a new contact, the modal form appears with empty values, including an empty hidden id field.

If I edit the contact, then the modal form will appear with the fields filled in, including this hidden id field.

In my controller, I intend to do this:

public JsonResult Contact(string values)
{
    var contact = JsonConvert.DeserializeObject<contact>(values);

    if (contact.Id.Equals(Guid.Empty))
    {
         // create a new contact in the database
    } else
    {
        // update an existing one
    }
}

However, I get an error can't convert "" to type Guid

How to get around this with NewtonSoft Json, I looked here in Custom JsonConverter and it seems to be going along the right lines, however I'm not sure where to go with this.

+3
source
1

, , - .

/// <summary>
/// Converts a <see cref="Guid"/> to and from its <see cref="System.String"/> representation.
/// </summary>
public class GuidConverter : JsonConverter
{
    /// <summary>
    /// Determines whether this instance can convert the specified object type.
    /// </summary>
    /// <param name="objectType">Type of the object.</param>
    /// <returns>Returns <c>true</c> if this instance can convert the specified object type; otherwise <c>false</c>.</returns>
    public override bool CanConvert(Type objectType)
    {
        return objectType.IsAssignableFrom(typeof(Guid));
    }

    /// <summary>
    /// Reads the JSON representation of the object.
    /// </summary>
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
    /// <param name="objectType">Type of the object.</param>
    /// <param name="existingValue">The existing value of object being read.</param>
    /// <param name="serializer">The calling serializer.</param>
    /// <returns>The object value.</returns>
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        try
        {
            return serializer.Deserialize<Guid>(reader);
        }
        catch
        {
            return Guid.Empty;
        }
    }

    /// <summary>
    /// Writes the JSON representation of the object.
    /// </summary>
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
    /// <param name="value">The value.</param>
    /// <param name="serializer">The calling serializer.</param>
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }
}

:

class Contact
{
    [JsonConverter(typeof(GuidConverter))]
    public Guid Id { get; set; }
}

:

var contact = JsonConvert.DeserializeObject<contact>(values, new GuidConverter());

, JSON :

{
    "id": "",
    "etc": "..."
}

, , , :

{
    "id": null,
    "etc": "..."
}
+6

All Articles