Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用mongoDB c#驱动程序将对象序列化为字符串值(好像对其调用ToString()_C#_Mongodb_Serialization_Bson - Fatal编程技术网

使用mongoDB c#驱动程序将对象序列化为字符串值(好像对其调用ToString()

使用mongoDB c#驱动程序将对象序列化为字符串值(好像对其调用ToString(),c#,mongodb,serialization,bson,C#,Mongodb,Serialization,Bson,我使用的是MongoDB C#驱动程序。 我有一个C语言的数据结构# 而不是 {TypeName: 'blabla', Properties: [{Val: 'value1'}, {Val: ' value2'}, {Val: ' value3'}] } 我如何才能做到这一点?您可以编写自定义序列化程序并向驱动程序注册它。需要注意的一点是,一旦注册了序列化程序,它将用于每个ResourceSpec实例 public class ResourceSpecSerializer : BsonBase

我使用的是MongoDB C#驱动程序。 我有一个C语言的数据结构#

而不是

{TypeName: 'blabla', Properties: [{Val: 'value1'}, {Val: ' value2'}, {Val: ' value3'}] }

我如何才能做到这一点?

您可以编写自定义序列化程序并向驱动程序注册它。需要注意的一点是,一旦注册了序列化程序,它将用于每个
ResourceSpec
实例

public class ResourceSpecSerializer : BsonBaseSerializer
{
    public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
        // Deserialize logic
    }

    public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
        // Serialize logic
    }
并使用
BsonSerializer.RegisterSerializer

BsonSerializer.RegisterSerializer(typeof(ResourceSpec), new ResourceSpecSerializer());
为了实现定制JSON,我建议仔细研究并具体介绍
JsonWriter

下面是一个使用
JsonWriter
的示例:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb); 

using(JsonWriter writer = new JsonTextWriter(sw))
{
    writer.Formatting = Formatting.Indented;

    writer.WriteStartObject();
    writer.WritePropertyName("CPU");
    writer.WriteValue("Intel");
    writer.WritePropertyName("PSU");
    writer.WriteValue("500W");
    writer.WritePropertyName("Drives");
    writer.WriteStartArray();
    writer.WriteValue("DVD read/writer");
    writer.WriteComment("(broken)");
    writer.WriteValue("500 gigabyte hard drive");
    writer.WriteValue("200 gigabype hard drive");
    writer.WriteEnd();
    writer.WriteEndObject();
}
将显示:

 {
    "CPU": "Intel",
    "PSU": "500W",
    "Drives": [
         "DVD read/writer"
         /*(broken)*/,
        "500 gigabyte hard drive",
        "200 gigabype hard drive"
     ]
 }
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb); 

using(JsonWriter writer = new JsonTextWriter(sw))
{
    writer.Formatting = Formatting.Indented;

    writer.WriteStartObject();
    writer.WritePropertyName("CPU");
    writer.WriteValue("Intel");
    writer.WritePropertyName("PSU");
    writer.WriteValue("500W");
    writer.WritePropertyName("Drives");
    writer.WriteStartArray();
    writer.WriteValue("DVD read/writer");
    writer.WriteComment("(broken)");
    writer.WriteValue("500 gigabyte hard drive");
    writer.WriteValue("200 gigabype hard drive");
    writer.WriteEnd();
    writer.WriteEndObject();
}
 {
    "CPU": "Intel",
    "PSU": "500W",
    "Drives": [
         "DVD read/writer"
         /*(broken)*/,
        "500 gigabyte hard drive",
        "200 gigabype hard drive"
     ]
 }