将 BSON 流解码为人类可读的字符串



我正在编写一个C#工具,该工具从Oracle DB中检索和聚合一些数据,并以干净易读的格式显示。我遇到了这个 VARCHAR 专栏,其中包含一个 BSON 流。我想解码为人类可读的东西(JSON 应该可以完成这项工作)。我正在尝试使用 Newtonsoft.Json.Bson 来反序列化此流。我正在使用以下代码:

byte[] rawData = new byte[] { 226, 6, 0, 0, 3, 68, 101, 102, 97, 117, 108, 116, 82, 101, 118, 101, 114, 115, 101, 82, 101, 113, 117, 101, 115, 116, 52, 73, 84, 84, 73, 0, 193, 6, 0, 0, 3, 111, 114, 100, 101, 114, 80, 97, 114, 97, 109, 115, 0, 175, 6, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 108, 105, 115, 116, 0, 144, 6, 0, 0, 0, 144, 6, 0, 0, 4, 108, 105, 115, 116, 0, 133, 6, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 105, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 78, 0, 0, 0, 0, 78, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 11, 0, 0, 0, 51, 51, 52, 49, 50, 56, 52, 52, 57, 52, 0, 2, 99, 111, 100, 101, 0, 7, 0, 0, 0, 77, 83, 73, 83, 68, 78, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 163, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 136, 0, 0, 0, 0, 136, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 65, 0, 0, 0, 51, 55, 56, 50, 122, 85, 68, 68, 89, 67, 56, 57, 114, 104, 99, 76, 78, 57, 48, 101, 71, 56, 74, 113, 52, 122, 66, 75, 87, 97, 119, 50, 69, 69, 67, 120, 106, 105, 57, 118, 115, 110, 103, 86, 117, 79, 86, 73, 71, 77, 88, 106, 48, 77, 79, 53, 120, 83, 55, 65, 84, 50, 114, 56, 0, 2, 99, 111, 100, 101, 0, 11, 0, 0, 0, 77, 101, 115, 115, 97, 103, 101, 83, 101, 113, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 111, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 84, 0, 0, 0, 0, 84, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 7, 0, 0, 0, 79, 112, 67, 111, 100, 101, 0, 2, 99, 111, 100, 101, 0, 17, 0, 0, 0, 67, 95, 79, 80, 69, 82, 65, 84, 73, 79, 78, 95, 67, 79, 68, 69, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 115, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 88, 0, 0, 0, 0, 88, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 9, 0, 0, 0, 79, 112, 82, 101, 97, 115, 111, 110, 0, 2, 99, 111, 100, 101, 0, 19, 0, 0, 0, 67, 95, 79, 80, 69, 82, 65, 84, 73, 79, 78, 95, 82, 69, 65, 83, 79, 78, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 116, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 89, 0, 0, 0, 0, 89, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 13, 0, 0, 0, 67, 108, 105, 101, 110, 116, 83, 117, 98, 83, 121, 115, 0, 2, 99, 111, 100, 101, 0, 16, 0, 0, 0, 67, 95, 67, 76, 73, 69, 78, 84, 95, 83, 85, 66, 83, 89, 83, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 125, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 98, 0, 0, 0, 0, 98, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 14, 0, 0, 0, 67, 108, 105, 101, 110, 116, 70, 105, 110, 73, 110, 102, 111, 0, 2, 99, 111, 100, 101, 0, 24, 0, 0, 0, 67, 95, 67, 76, 73, 69, 78, 84, 95, 70, 73, 78, 65, 78, 67, 73, 65, 76, 95, 73, 78, 70, 79, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 117, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 90, 0, 0, 0, 0, 90, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 15, 0, 0, 0, 50, 48, 49, 56, 49, 50, 50, 56, 49, 53, 51, 57, 50, 50, 0, 2, 99, 111, 100, 101, 0, 15, 0, 0, 0, 79, 80, 69, 82, 65, 84, 73, 79, 78, 95, 68, 65, 84, 69, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 108, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 81, 0, 0, 0, 0, 81, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 16, 0, 0, 0, 50, 50, 50, 48, 49, 51, 51, 52, 49, 50, 56, 52, 52, 57, 52, 0, 2, 99, 111, 100, 101, 0, 5, 0, 0, 0, 73, 77, 83, 73, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 95, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 68, 0, 0, 0, 0, 68, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 3, 0, 0, 0, 51, 56, 0, 2, 99, 111, 100, 101, 0, 5, 0, 0, 0, 80, 82, 79, 70, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 94, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 67, 0, 0, 0, 0, 67, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 2, 0, 0, 0, 80, 0, 2, 99, 111, 100, 101, 0, 5, 0, 0, 0, 84, 89, 80, 69, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 93, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 66, 0, 0, 0, 0, 66, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 2, 0, 0, 0, 48, 0, 2, 99, 111, 100, 101, 0, 4, 0, 0, 0, 68, 79, 77, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 94, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 67, 0, 0, 0, 0, 67, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 2, 0, 0, 0, 49, 0, 2, 99, 111, 100, 101, 0, 5, 0, 0, 0, 83, 86, 73, 83, 0, 0, 0, 3, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 107, 0, 0, 0, 5, 99, 111, 109, 112, 108, 101, 120, 112, 114, 111, 112, 101, 114, 116, 121, 0, 80, 0, 0, 0, 0, 80, 0, 0, 0, 2, 99, 111, 109, 112, 108, 101, 120, 116, 121, 112, 101, 0, 17, 0, 0, 0, 87, 79, 67, 111, 109, 109, 111, 110, 79, 98, 106, 80, 97, 114, 97, 109, 0, 2, 118, 97, 108, 117, 101, 0, 6, 0, 0, 0, 79, 75, 73, 68, 83, 0, 2, 99, 111, 100, 101, 0, 14, 0, 0, 0, 79, 70, 70, 69, 82, 73, 78, 71, 95, 67, 79, 68, 69, 0, 0, 0, 0, 0, 0, 0, 0 };
MemoryStream memoryStream = new MemoryStream(rawData);
using (BsonDataReader bsonDataReader = new BsonDataReader(memoryStream))
{
    JsonSerializer jsonSerializer = new JsonSerializer();
    String test = jsonSerializer.Deserialize(bsonDataReader).ToString();
}

(为了简洁起见,字节数组被粗暴地初始化了)执行此代码会在"test"变量中生成以下输出:

{
  "DefaultReverseRequest4ITTI": {
    "orderParams": {
      "complexpropertylist": "kAYAAARsaXN0AIUGAAADY29tcGxleHByb3BlcnR5AGkAAAAFY29tcGxleHByb3BlcnR5AE4AAAAATgAAAAJjb21wbGV4dHlwZQARAAAAV09Db21tb25PYmpQYXJhbQACdmFsdWUACwAAADMzNDEyODQ0OTQAAmNvZGUABwAAAE1TSVNETgAAAANjb21wbGV4cHJvcGVydHkAowAAAAVjb21wbGV4cHJvcGVydHkAiAAAAACIAAAAAmNvbXBsZXh0eXBlABEAAABXT0NvbW1vbk9ialBhcmFtAAJ2YWx1ZQBBAAAAMzc4MnpVRERZQzg5cmhjTE45MGVHOEpxNHpCS1dhdzJFRUN4amk5dnNuZ1Z1T1ZJR01YajBNTzV4UzdBVDJyOAACY29kZQALAAAATWVzc2FnZVNlcQAAAANjb21wbGV4cHJvcGVydHkAbwAAAAVjb21wbGV4cHJvcGVydHkAVAAAAABUAAAAAmNvbXBsZXh0eXBlABEAAABX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nZhbHVlAAMAAAAzOAACY29kZQAFAAAAUFJPRgAAAANjb21wbGV4cHJvcGVydHkAXgAAAAVjb21wbGV4cHJvcGVydHkAQwAAAABDAAAAAmNvbXBsZXh0eXBlABEAAABXT0NvbW1vbk9ialBhcmFtAAJ2YWx1ZQACAAAAUAACY29kZQAFAAAAVFlQRQAAAANjb21wbGV4cHJvcGVydHkAXQAAAAVjb21wbGV4cHJvcGVydHkAQgAAAABCAAAAAmNvbXBsZXh0eXBlABEAAABXT0NvbW1vbk9ialBhcmFtAAJ2YWx1ZQACAAAAMAACY29kZQAEAAAARE9NAAAAA2NvbXBsZXhwcm9wZXJ0eQBeAAAABWNvbXBsZXhwcm9wZXJ0eQBDAAAAAEMAAAACY29tcGxleHR5cGUAEQAAAFdPQ29tbW9uT2JqUGFyYW0AAnZhbHVlAAIAAAAxAAJjb2RlAAUAAABTVklTAAAAA2NvbXBsZXhwcm9wZXJ0eQBrAAAABWNvbXBsZXhwcm9wZXJ0eQBQAAAAAFAAAAACY29tcGxleHR5cGUAEQAAAFdPQ29tbW9uT2JqUGFyYW0AAnZhbHVlAAYAAABPS0lEUwACY29kZQAOAAAAT0ZGRVJJTkdfQ09ERQAAAAAA"
    }
  }
}

根据我的理解,发生这种情况是因为"complexpropertylist"字段本身包含一个字节数组(可能是base64编码的?那么,如何告诉反序列化程序递归处理每个子节点呢?

任何帮助都真诚地感谢。

使用 MongoDB.Bson nuget 包,您可以使用以下内容:

using MongoDB.Bson;
using MongoDB.Bson.Serialization;
TestType testObj = new("abc");
// Serialization
byte[] testObjAsBsonArray = testObj.ToBson();
// Set nominal type to object. If the nominal type differs from the actual type then type information is included in the bson.
byte[] testObjAsBsonArrayWithTypeInfo = testObj.ToBson<object>();
string testObjAsJsonString = testObj.ToJson();
BsonDocument testObjAsBsonDocument = testObj.ToBsonDocument();
// Deserialization
TestType fromArray = BsonSerializer.Deserialize<TestType>(testObjAsBsonArray);
// fromArrayWithTypeInfo.GetType() equals typeof(TestType)
object fromArrayWithTypeInfo = BsonSerializer.Deserialize<object>(testObjAsBsonArrayWithTypeInfo);
TestType fromJson = BsonSerializer.Deserialize<TestType>(testObjAsJsonString);
TestType fromBsonDocument = BsonSerializer.Deserialize<TestType>(testObjAsBsonDocument);
public record TestType(string TestValue);

相关内容

  • 没有找到相关文章

最新更新