Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
C# BSon异常newtonsoft.json_C#_Json.net - Fatal编程技术网

C# BSon异常newtonsoft.json

C# BSon异常newtonsoft.json,c#,json.net,C#,Json.net,给定 公开课 { [JsonProperty(PropertyName=“Ver”)] 公共字符串ver{get;set;} [JsonProperty(PropertyName=“SomethingElse”)] 公共对象somethingElse{get;set;}//将爆炸 //public SomethingElse SomethingElse{get;set;}//不会 } 公共类某物 { [JsonProperty(PropertyName=“uuid”)] 公共Guid uuid

给定

公开课
{
[JsonProperty(PropertyName=“Ver”)]
公共字符串ver{get;set;}
[JsonProperty(PropertyName=“SomethingElse”)]
公共对象somethingElse{get;set;}//将爆炸
//public SomethingElse SomethingElse{get;set;}//不会
}
公共类某物
{
[JsonProperty(PropertyName=“uuid”)]
公共Guid uuid{get;set;}
}
如果某事物的成员somethingelse是对象而不是类型本身,则尝试序列化然后反序列化某事物将无法将Guid类型的对象强制转换为Byte[]异常

很抱歉没有提供详细信息。上面是一个简化的类,可以序列化,但不能反序列化对象。下面的匹配情况更接近,唯一的区别是我们需要的字典是,它也会以完全相同的方式失败。该示例使用JSOn,而不是BSON。序列化似乎有效,只是反序列化在JSonWriter中引发异常,无法将类型为“System.Guid”的对象强制转换为类型为“System.Byte[]”,第552行

case JsonToken.Bytes:

WriteValue((byte[])reader.Value);<-- is trying to case a Guid to byte array 
case JsonToken.Bytes:

WriteValue((字节[])reader.Value);所以你很惊讶它想要在这里显式地键入它的属性?这是一个问题吗?您需要在这里接受任何旧对象吗?您故意含糊其辞的代码示例不会帮助我们帮助您。您有json的示例吗?那么实际的类定义呢?
    static void Main(string[] args)
    {
           const string path = @"C:\zzz_bson.dat";
           WriteBson(path);
           ReadBson(path);
    }

    private static void ReadBson(string path)
    {
        var jsonSerializer = new JsonSerializer();
        var file = File.OpenRead(path);
        var binaryReader = new BinaryReader(file);
        var bsonReader = new BsonReader(binaryReader);
        var something = jsonSerializer.Deserialize(bsonReader); 
        file.Close();
    }

    private static void WriteBson(string path)
    {
        //var something = new Dictionary<string, Guid> { { "uuid", new Guid("8afbc8b8-5449-4c70-abd4-3e07046d0f61") } };  //fails
        var something = new Dictionary<string, string> { { "uuid", new Guid("8afbc8b8-5449-4c70-abd4-3e07046d0f61").ToString() } }; // works
        var jsonSerializer = new JsonSerializer();
        var file = File.OpenWrite(path);
        var binaryWriter = new BinaryWriter(file);
        var bsonWriter = new BsonWriter(binaryWriter);
        jsonSerializer.Serialize(bsonWriter, something);
        file.Close();
    }