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
144 views
in Technique[技术] by (71.8m points)

c# - Overriding Default Primitive Type Handling in Json.Net

Is there a way to override the default deserialization behaviour of Json.net when handling primitive types? For example when deserializing the json array [3.14,10,"test"] to a type of object[] 3.14 will be of type double and 10 will be of type long. Is there anyway I can intercept or override this type decision so I could deserialize the values as decimal and int respectively?

I basically always want json integers to always return as int and floats to return as decimal. This will save me some having to inject double to decimal conversions in my code.

I've looked into extending Newtonsoft.Json.Serialization.DefaultContractResolver and implementing my own Newtonsoft.Json.JsonConverter but I have not discovered any way to implement this desired override.

Example code to reproduce

object[] variousTypes = new object[] {3.14m, 10, "test"};
string jsonString = JsonConvert.SerializeObject(variousTypes);
object[] asObjectArray = JsonConvert.DeserializeObject<object[]>(jsonString); // Contains object {double}, object {long}, object {string}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I think, this should work

public class MyReader : JsonTextReader
{
    public MyReader(string s) : base(new StringReader(s))
    {
    }

    protected override void SetToken(JsonToken newToken, object value)
    {
        object retObj = value;
        if (retObj is long) retObj = Convert.ChangeType(retObj, typeof(int));
        if (retObj is double) retObj = Convert.ChangeType(retObj, typeof(decimal));

        base.SetToken(newToken, retObj);
    }
}


object[] variousTypes = new object[] { 3.14m, 10, "test" };
string jsonString = JsonConvert.SerializeObject(variousTypes);

JsonSerializer serializer = new JsonSerializer();
var asObjectArray = serializer.Deserialize<object[]>(new MyReader(jsonString));

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

...