Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

.net - JSON.Net Seralize values don't have quotation marks

The following code:

var json = JsonConvert.SerializeObject(fooObject,
                                                   new JsonSerializerSettings
                                                       {
                                                           NullValueHandling = NullValueHandling.Ignore,

                                                       });

Seralizes a json (string) object of:

{ "fooA":0, "fooB":0, "fooC":false, "fooD":false, "fooE":0, "fooF":0, }

The values are not in quotation marks, such as "fooA":"0". This is the behavior that I want.

Is there a way to enforce this behavior?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In JSON format, numbers and booleans do not have quotes around them, while strings do (see JSON.org).

If you want quotes around all your primitives, you have a few options:

  1. Change the properties of the object you are serializing to be of type string.
  2. Put your values into a Dictionary<string, string> (or a DTO) and serialize that instead.
  3. Make a custom JsonConverter to do the conversion. This option has the advantage that it can apply globally so that you don't have to change all your classes.

The first two options are pretty self-explanatory. If you opted to go with a converter, the code might look something like this:

class PrimitiveToStringConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType.IsPrimitive;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString().ToLower());
    }

    public override bool CanRead
    {
        get { return false; }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

To use it, simply pass an instance of the converter to the JsonConvert.SerializeObject method.

string json = JsonConvert.SerializeObject(foo, new PrimitiveToStringConverter());

Demo:

class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Foo
        {
            Int = 6,
            Bool = true,
            Float = 3.14159
        };

        string json = JsonConvert.SerializeObject(foo, new PrimitiveToStringConverter());
        Console.WriteLine(json);
    }
}

class Foo
{
    public int Int { get; set; }
    public bool Bool { get; set; }
    public double Float { get; set; }
}

Output:

{"Int":"6","Bool":"true","Float":"3.14159"}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...