如何将MessagePack-CSharp与字符串JSON一起使用?不是CLI,而是neuecc / MessagePack-CSharp

问题描述

using MessagePack;
[MessagePackObject]
public class CPegar_ids
{
    [Key(0)]
    public string operationName { get; set; }
    [Key(1)]
    public Variables variables { get; set; }
    [Key(2)]
    public string query { get; set; }
}

[MessagePackObject]
public class Variables
{
    [Key(0)]
    public object activeType { get; set; }
    [Key(1)]
    public string[] instruments { get; set; }
    [Key(2)]
    public string LeverageInstrument { get; set; }
    [Key(3)]
    public int userGroupID { get; set; }
    [Key(4)]
    public string sortField { get; set; }
    [Key(5)]
    public string sortDirection { get; set; }
    [Key(6)]
    public int limit { get; set; }
    [Key(7)]
    public int offset { get; set; }
}
string json_data = @"
{
  ""operationName"": ""GetAssets"",""variables"": {
    ""activeType"": null,""instruments"": [
      ""BinaryOption"",""DigitalOption"",""FxOption"",""TurboOption""
    ],""LeverageInstrument"": ""BinaryOption"",""userGroupID"": 193,""sortField"": ""Name"",""sortDirection"": ""Ascending"",""limit"": 20,""offset"": 0
  },""query"": """"
}
";

var ob_ids = MessagePackSerializer.Deserialize<CPegar_ids>(Encoding.UTF8.GetBytes(json_data ));

Console.WriteLine($" IDS OB: {ob_ids.GetType()}");

https://github.com/neuecc/MessagePack-CSharp

我正在使用HttpWebRequest下载JSON,这将返回一个var字符串。我想使用此字符串对MessagePackSerializer进行反序列化。我尝试了几种不同的方法,使用Utf8Json可以做到,但是使用此MessagePack却不能。我想使用MessagePack,因为它快得多。

解决方法

看起来MessageBack有自己的表示法,不是JSON。但是您正在尝试将Json反序列化为它们的自定义表示法,但由于明显的原因而失败。他们似乎通过使用更多的unicode代替JSON之类的标准字符来使它紧凑而紧凑。

请参阅https://msgpack.org/index.html

这就是为什么您不打算放入JSON字符串并尝试反序列化它的原因。如果您正在寻找更快的JSON选项,还有Newtonsoft Json.NET的其他一些常见替代方法,例如fastJSON https://github.com/mgholam/fastJSON

反转示例代码,我们可以得到序列化值的示例:

var myObject = new CPegar_ids {
    operationName = "GetAssets",variables = new Variables {
        activeType = null,instruments = new string[] {
            "BinaryOption","DigitalOption","TurboOption"
        },leverageInstrument = "BinaryOption",userGroupID = 193,sortField = "Name",sortDirection = "Ascending",limit = 20,offset = 0
    },query = ""
};

var bytes = MessagePackSerializer.Serialize(myObject);
Console.WriteLine(Encoding.UTF8.GetString(bytes));

其输出是: ``操作名称''``获取资产''``变量''``活动类型''``工具''``BinaryOption''``数字选项''``TurboOption''``杠杆工具''````BinaryOption''``userGroupID''``sortField''``Name''sortDirection``Ascending``limit14``offset00``查询。

,

我找不到很好的解释为什么它不起作用。但是有一种方法可以使它起作用,而不是使用Key(int index)属性。我们将使用Key(string propertyName)属性。

您应该使用索引键(int)还是字符串键?我们推荐 使用索引键来加快序列化和更紧凑的二进制文件 表示比字符串键。 Reference.

对象

[MessagePackObject]
public class CPegar_ids
{
    [Key("operationName")]
    public string operationName { get; set; }
    [Key("variables")]
    public Variables variables { get; set; }
    [Key("query")]
    public string query { get; set; }
}

[MessagePackObject]
public class Variables
{
    [Key("activeType")]
    public object activeType { get; set; }
    [Key("instruments")]
    public string[] instruments { get; set; }
    [Key("leverageInstrument")]
    public string leverageInstrument { get; set; }
    [Key("userGroupID")]
    public int userGroupID { get; set; }
    [Key("sortField")]
    public string sortField { get; set; }
    [Key("sortDirection")]
    public string sortDirection { get; set; }
    [Key("limit")]
    public int limit { get; set; }
    [Key("offset")]
    public int offset { get; set; }
}

SERIALIZATION

var jsonByteArray = MessagePackSerializer.ConvertFromJson(File.ReadAllText("json1.json"));
CPegar_ids  ob_ids = MessagePackSerializer.Deserialize<CPegar_ids>(jsonByteArray);