I want to achieve deserialization, serialization and deserialization in ReadJson() by using JsonSerializer class.
Problem:
CanConvert(() is called when the source code serializer.Deserialize(jsonReader,objectType ); is executed,
and is in an infinite loop.
Expected behavior:
How to convert the type of finalObject to type objectType,and don't cause any CanConvert() call loop.
The sample code is below.Please help.
//sample code
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Test
{
public class WPJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
//object→json
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var jToken = JToken.FromObject(value, serializer);
jToken.WriteTo(writer);
}
//json→object
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//String serializedJson;
JToken token = JToken.Load(reader);
string jsonString = token.ToString();
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(jsonString));
var deserializeObject = serializer.Deserialize(jsonTextReader);
var serializedToken = JToken.FromObject(deserializeObject);
string serializedJson = serializedToken.ToString();
var stringReader = new StringReader(serializedJson);
var jsonReader = new JsonTextReader(stringReader);
var finalObject = serializer.Deserialize(jsonReader,objectType );
return finalObject;
}
}
I want to achieve deserialization, serialization and deserialization in ReadJson() by using JsonSerializer class.
Problem: CanConvert(() is called when the source code serializer.Deserialize(jsonReader,objectType ); is executed, and is in an infinite loop.
Expected behavior: How to convert the type of finalObject to type objectType,and don't cause any CanConvert() call loop. The sample code is below.Please help.
//sample code using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; using Newtonsoft.Json; using Newtonsoft.Json.Linq;
namespace Test { public class WPJsonConverter : JsonConverter { public override bool CanConvert(Type objectType) { return true; }
}