Json.Net – Serialize property name without quotes

It’s possible, but I advise against it as it would produce invalid JSON as Marcelo and Marc have pointed out in their comments.

Using the Json.NET library you can achieve this as follows:

[JsonObject(MemberSerialization.OptIn)]
public class ModalOptions
{
    [JsonProperty]
    public object href { get; set; }

    [JsonProperty]
    public object type { get; set; }
}

When serializing the object use the JsonSerializer type instead of the static JsonConvert type.

For example:

var options = new ModalOptions { href = "https://stackoverflow.com/questions/7553516/file.html", type = "full" };
var serializer = new JsonSerializer();
var stringWriter = new StringWriter();
using (var writer = new JsonTextWriter(stringWriter))
{
    writer.QuoteName = false;
    serializer.Serialize(writer, options);            
}
var json = stringWriter.ToString();

This will produce:

{href:"https://stackoverflow.com/questions/7553516/file.html",type:"full"}

If you set the QuoteName property of the JsonTextWriter instance to false the object names will no longer be quoted.

Leave a Comment