JSON.NET 似乎不想将 GUID 序列化为本机 BSON UUID,而是将 GUID 输出为字符串。
JObject
我已经测试了从(在序列化之前验证JValue.Type == JTokenType.Guid
)和匿名类型( )生成 BSON,new { myUuid = new Guid() }
在这两种情况下都没有运气。
如何强制 JSON.NET 将 GUID 序列化为本机 BSON UUID(16 字节二进制值;子类型 = 0x04)?
示例代码:
var source = new
{
myInteger = 123L,
myFloat = 10.0f,
myString = "hello",
myUuid = Guid.NewGuid(),
myBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 },
myDatetime = DateTime.UtcNow,
};
MemoryStream buffer = new MemoryStream();
BsonWriter writer = new BsonWriter(buffer);
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, source);
byte[] myBson = buffer.ToArray();