【问题标题】:How to use MessagePack-CSharp with string JSON? Not is CLI, is neuecc/MessagePack-CSharp如何将 MessagePack-CSharp 与字符串 JSON 一起使用?不是 CLI,是 neuecc/MessagePack-CSharp
【发布时间】:2020-09-05 09:50:49
【问题描述】:
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,因为它更快。

【问题讨论】:

    标签: c# json msgpack


    【解决方案1】:

    看起来 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));
    

    它的输出是: ��operationName�GetAssets�variables�activeType�instruments�BinaryOption�DigitalOption�TurboOption�leverageInstrument�BinaryOption�userGroupID�sortField�Name�sortDirection�Ascending�limit14�offset00�query�

    【讨论】:

      【解决方案2】:

      我找不到一个很好的解释为什么它不起作用。但是有一种方法可以让它工作,而不是使用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; }
      }
      

      序列化

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

      【讨论】:

      • 值得记住的是,如果这确实有效,它将从 JSON 转换为 MessagePack,然后从 MessagePack 转换为 .Net 对象。这可能比直接从 JSON 到 .Net 慢得多。 Op 提到他们想使用 MessagePack 来提高性能,这就是我评论的原因。
      猜你喜欢
      • 1970-01-01
      • 2013-12-07
      • 2023-03-29
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      相关资源
      最近更新 更多