Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用RestSharp反序列化json时,将int转换为位掩码/标志_C#_Json_Serialization_Restsharp - Fatal编程技术网

C# 使用RestSharp反序列化json时,将int转换为位掩码/标志

C# 使用RestSharp反序列化json时,将int转换为位掩码/标志,c#,json,serialization,restsharp,C#,Json,Serialization,Restsharp,我收到的REST请求包含一个32位整数。我想将其转换为基于以下枚举的一组标志: [Flags] public enum TowerStatus { NotUsed = 4194304, DireAncientTop = 2097152, DireAncientBottom = 1048576, DireBottomTier3 = 524288, DireBottomTier2 = 262144, DireBottomTier1 = 131072,

我收到的REST请求包含一个32位整数。我想将其转换为基于以下枚举的一组标志:

[Flags]
public enum TowerStatus
{
    NotUsed = 4194304,
    DireAncientTop = 2097152,
    DireAncientBottom = 1048576,
    DireBottomTier3 = 524288,
    DireBottomTier2 = 262144,
    DireBottomTier1 = 131072,
    DireMiddleTier3 = 65536,
    DireMiddleTier2 = 32768,
    DireMiddleTier1 = 16384,
    DireTopTier3 = 8192,
    DireTopTier2 = 4096,
    DireTopTier1 = 2048,
    RadiantAncientTop = 1024,
    RadiantAncientBottom = 512,
    RadiantBottomTier3 = 256,
    RadiantBottomTier2 = 128,
    RadiantBottomTier1 = 64,
    RadiantMiddleTier3 = 32,
    RadiantMiddleTier2 = 16,
    RadiantMiddleTier1 = 8,
    RadiantTopTier3 = 4,
    RadiantTopTier2 = 2,
    RadiantTopTier1 = 1
}
但我甚至不知道如何尝试将int反序列化为CLR对象


我使用的是RestSharp提供的默认JSON反序列化程序,但即使实现了自定义反序列化程序,我也不知道如何以与其他方式不同的方式反序列化一个值。

不清楚为什么要使用RestSharp在服务器上反序列化请求,JSON.NET通常处理得很好

例如,如果您有以下类:

public class MyModel
{
    public TowerStatus Foo { get; set; }
}
以及以下JSON输入:

string json = "{\"Foo\": 393216 }";
您可以将其反序列化回模型,并且将遵守枚举标志:

var model = JsonConvert.DeserializeObject<MyModel>(response);
Console.WriteLine(model.Foo);
// prints DireBottomTier1, DireBottomTier2
var model=JsonConvert.DeserializeObject(响应);
Console.WriteLine(model.Foo);
//打印DIREBOTTOTOMTIER1,DireBottomTier2
如果出于某种原因需要使用RestSharp进行反序列化,则可以编写自定义反序列化程序:

public class RestSharpJsonNetDeserializer : IDeserializer
{
    public RestSharpJsonNetDeserializer()
    {
        ContentType = "application/json";
    }

    public T Deserialize<T>(IRestResponse response)
    {
        return JsonConvert.DeserializeObject<T>(response.Content);
    }

    public string DateFormat { get; set; }
    public string RootElement { get; set; }
    public string Namespace { get; set; }
    public string ContentType { get; set; }
}
公共类RestSharpJsonNetDeserializer:IDeserializer
{
公共RestSharpJsonNetDeserializer()
{
ContentType=“应用程序/json”;
}
公共T反序列化(iRestreponse响应)
{
返回JsonConvert.DeserializeObject(response.Content);
}
公共字符串日期格式{get;set;}
公共字符串根元素{get;set;}
公共字符串命名空间{get;set;}
公共字符串ContentType{get;set;}
}
可以这样使用:

string json = "{\"Foo\": 393216 }";
var response = new RestResponse();
response.Content = json;
var deserializer = new RestSharpJsonNetDeserializer();
var model = deserializer.Deserialize<MyModel>(response);
string json=“{\'Foo\”:393216}”;
var响应=新的重新响应();
response.Content=json;
var反序列化器=新的RestSharpJsonNetDeserializer();
var模型=反序列化器。反序列化(响应);

我只是想澄清一下,你是说JSON.Net会将int反序列化为Enum标志而不做任何更改吗?我切换到使用JSON.Net,这解决了我所有的问题。