Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET - How do I include Type Name Handling for primitive C# types that are stored in an array/list as Object type

I am using Newtonsoft JSON.NET to serialize/deserialize stuff for me. But I have this list wherein they are of Object type:

var list = new List<Object>() 
{ 
    "hi there", 
    1,
    2.33  
};

When I serialize that with TypeNameHandling set to TypeNameHandling.All, I was expecting that it will also give a $type for each instance on the list but doesn't seem to be the case. Here is the actual output:

{
    "$type": "System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib",
    "$values": [
        "hi there",
        1,
        2.33
    ]
}

I need this to have specific Type Name Handling for those primitive types because if I add an Int32 value in to the list and when it comes back after deserializing it JSON.NET sets it as Int64. That is a big deal for me because I am trying to invoke some methods and to do that I need to compare the parameters and they MUST have the same types. Is there a way or a setting you can set in JSON.NET to achieve what I need?

I've seen this post but what it does is that he is trying to change the default behavior and always return Int32 which is not what I'm looking for.

Any help would be appreciated. Thanks

like image 923
Bairose Avatar asked Feb 24 '16 02:02

Bairose


People also ask

What does JsonConvert DeserializeObject do?

DeserializeObject Method. Deserializes the JSON to a . NET object.

Is JSON net deprecated?

Json was basically scrapped by Microsoft with the coming of . NET Core 3.0 in favor of its newer offering designed for better performance, System.

What is JsonProperty annotation C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

How do I deserialize a JSON string to an object in C#?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.


1 Answers

You can create a wrapper class for primitive types, included implicit operators as you want:

class TypeWrapper
{
    public object Value { get; set; }
    public string Type { get; set; }

    public static implicit operator TypeWrapper(long value)
    {
        return new TypeWrapper { Value = value, Type = typeof(long).FullName };
    }

    public static implicit operator long(TypeWrapper value)
    {
        return (long)value.Value;
    }

    public static implicit operator TypeWrapper(int value)
    {
        return new TypeWrapper { Value = value, Type = typeof(int).FullName };
    }

    public static implicit operator int(TypeWrapper value)
    {
        return (int)value.Value;
    }
}

Then you will have element' types when serialize data:

var data = new List<TypeWrapper> { 1, 2L };
var json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
Console.WriteLine(json);

// result: [{"Value":1,"Type":"System.Int32"},{"Value":2,"Type":"System.Int64"}]
like image 129
tdat00 Avatar answered Oct 04 '22 02:10

tdat00