2017-03-17 6 views
1

JsonConverterバイト[]

var serialized = JsonConvert.SerializeObject(obj, settings); 

{ 
"HomeId": "2925362b", 
"UserId": "9ea43c30", 
"Name": "Name 1", 
"Description1": "VABlAHMAdABlAA==", //===> Converts to Base64 (OK) 
"User": { 
    "UserId": "9ea43c30", 
    "Email": "[email protected]", 
    "Perfil": { 
     "UserId": "9ea43c30", 
     "Name": "Name 2", 
     "Description2": "dABlAHMAdABlAA==", //===> Converts to Base64 (OK) 
    } 
} 

}

//

var deserialized = JsonConvert.DeserializeObject<T>(serialized, settings); // T is class Home 

{ 
"HomeId": "2925362b", 
"UserId": "9ea43c30", 
"Name": "Name 1", 
"Description1": "VABlAHMAdABlAA==", //===> Don't Convert from Base64, (**Does not call the ReadJson function of ByteArrayConverter**) 
"User": { 
    "UserId": "9ea43c30", 
    "Email": "[email protected]", 
    "Perfil": { 
     "UserId": "9ea43c30", 
     "Name": "Name 2", 
     "Description2": "dABlAHMAdABlAA==", //===> Convert from Base64 (OK) 
    } 
} 

}

設定

 var settings = new JsonSerializerSettings 
     { 
      TypeNameHandling = TypeNameHandling.Objects, 
      ReferenceLoopHandling = ReferenceLoopHandling.Ignore, 
      Formatting = Formatting.Indented 
     }; 
     settings.Converters.Add(new ByteArrayConverter()); 

それはバイトをバックに変換されませんので、説明1のプロパティは、ByteArrayConverterクラスのReadJson関数に入らないをデシリアライズ[]、別の無効な[]バイトを生成しています....

すべてのIDこの問題のea?

+5

は、なぜあなたはすべてのコンバータを使用している見つけることができるようあなたがByteArrayConverter必要はありませんか? Json.NETは、基本的にバイト配列のためのbase64をサポートしています。[Serialization Guide:Primitive Types](http://www.newtonsoft.com/json/help/html/serializationguide.htm) – dbc

+0

こんにちは、上記のように使用しないと$ type: "System.Byte []、mscorlib" $値: "VABlAHMAdABlAA =="という情報でバイト[]型をシリアル化しますが、デシリアライズするとエラーが表示されます:値の解析中に予期しない文字が発生しました – Anpeiron

+1

FWIW、あなたが指定した同じ設定を使用してコンバータなしで私のためにうまくいきました。 –

答えて

0

古い質問が、直列化復元中に次のようなJSONの設定を使用して、直列化復元中に上記のエラーを回避するために明確化

when I do not use as above, it serializes a byte[] type with the information: $ type: "System.Byte [], mscorlib" $ Value: "VABlAHMAdABlAA ==", but when I go deserialize it shows an error: Unexpected character encountered while parsing value

を必要としています。

TypeNameHandling = TypeNameHandling.None 

TypeNameHandling設定JSONをシリアライズする際種別情報を含み、詳しくはJSON

をデシリアライズする際作成型が作成されるように型情報を読み取る読み取り:バイトのTypeNameHandling Enumeration

シリアライゼーションを[]でありますBase-64文字列に変換し、逆シリアル化すると再びbyte []を生成します。

@Brianロジャースのコメントで与えられ、

@dbcあなたはa complete working example using your code with modification online

関連する問題