JSON。NET -如何将存储在数组/列表中的基元c#类型的类型命名处理作为对象类型

时间:2022-11-26 16:24:26

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

我正在使用Newtonsoft JSON。NET为我序列化/反序列化东西。但是我有这个列表,它们是对象类型的:

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:

当我用TypeNameHandling设置为TypeNameHandling时。所有的,我期望它也会为列表中的每个实例提供一个$type,但似乎不是这样。这里是实际输出:

{
    "$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?

我需要对这些原始类型具有特定的类型名处理,因为如果我向列表中添加一个Int32值,当它在反序列化JSON之后返回时。NET将其设置为Int64。这对我来说是一件大事,因为我正在尝试调用一些方法,并且我需要对参数进行比较,并且它们必须具有相同的类型。是否有一种方法或设置可以在JSON中设置。NET实现了我所需要的吗?

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.

我看过这篇文章,但它所做的是他试图改变默认行为并总是返回in 32,这不是我要找的。

Any help would be appreciated. Thanks

如有任何帮助,我们将不胜感激。谢谢

1 个解决方案

#1


1  

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"}]

#1


1  

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"}]