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


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还有一些其他常见的替代方案,比如fastJSONhttps://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�变量��activeType��仪器��BinaryOption�DigitalOption�TurboOption�杠杆仪表�BinaryOption�用户组ID���sortField�名称�排序方向�提升�限制14�offset00�查询�

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

应该使用索引(int(键还是字符串键?我们建议使用索引键实现更快的序列化和更紧凑的二进制表示而不是字符串键。参考

对象

[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);

最新更新