自定義JsonConverter,反序列化不符合規範的陣列屬性
如果json
字串不符合規範,可以通過自定義jsonconvert
解決
public class SingleValueArrayConverter<T> : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
object retVal = new Object();
if (reader.TokenType == JsonToken.StartObject)
{
T instance = (T)serializer.Deserialize(reader, typeof(T));
retVal = new List<T>() { instance };
}
else if (reader.TokenType == JsonToken.StartArray)
{
retVal = serializer.Deserialize(reader, objectType);
}
return retVal;
}
public override bool CanConvert(Type objectType)
{
return true;
}
}
自定義 jsonConverter的問題記錄
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ConsoleApplication1
{
public class A
{
public string Title = "A Title";
[JsonConverter(typeof(ConsoleApplication1.Program.custConverter))]
public List<answerBase> baseAnswerList = new List<answerBase>();
}
public class answerA : answerBase
{
public List<int> value = new List<int>() { 1, 2, 3 };
}
public class answerB : answerBase
{
public List<string> Correct = new List<string>() { "1111", "2222" };
public List<string> Incorrect = new List<string>() { "3333", "4444" };
}
class Program
{
static void Main(string[] args)
{
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
var test1 = new A();
test1.baseAnswerList.Add(new answerA());
var result = JsonConvert.SerializeObject(test1, jsonSerializerSettings);
var test2 = new A();
test2.baseAnswerList.Add(new answerB());
var result2 = JsonConvert.SerializeObject(test2, jsonSerializerSettings);
A a = (A)JsonConvert.DeserializeObject(result, jsonSerializerSettings);
A a1 = (A)JsonConvert.DeserializeObject(result2, jsonSerializerSettings);
List<A> testAList = new List<A>();
testAList.Add(test1);
testAList.Add(test2);
var resultList = JsonConvert.SerializeObject(testAList, jsonSerializerSettings);
var deserializeResult = JsonConvert.DeserializeObject(resultList, jsonSerializerSettings);
}
public class custConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool isError = false;
try
{
List<int> retValTest1 = new List<int>();
retValTest1 = (List<int>)serializer.Deserialize(reader, typeof(List<int>));
var t1 = new List<answerBase>();
var a1 = new answerA();
a1.value = retValTest1;
t1.Add(a1);
return t1;
}
catch
{
isError = true;
}
if (isError)
{
try
{
var retVal = new List<answerB>();
retVal = (List<answerB>)serializer.Deserialize(reader, typeof(List<answerB>));
var t = new List<answerBase>();
foreach (var item in retVal)
{
t.Add(new answerB()
{
Correct = item.Correct.Distinct().ToList(),
Incorrect = item.Incorrect.Distinct().ToList()
});
}
return t;
}
catch
{
isError = true;
}
}
return null;
}
//public override bool CanRead
//{
// get { return false; }
//}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
bool bValue = false;
answerBase temp = new answerBase();
if (((List<answerBase>)value).Count() > 0)
{
temp = ((List<answerBase>)value).First();
bValue = temp is answerA;
}
if (bValue)
{
var tempA = temp as answerA;
writer.WriteStartArray();
foreach (var item in tempA.value)
{
writer.WriteValue(item);
}
writer.WriteEndArray();
}
else
{
JToken t = JToken.FromObject(value);
t.WriteTo(writer);
}
}
}
}
}
相關文章
- 自定義規範
- [BUG反饋]模型屬性自定義函式提交不了模型函式
- JSON繼承JsonConverter序列化與反序列化重寫類屬性JSON繼承
- NumPy 陣列屬性陣列
- 自定義View:自定義屬性(自定義按鈕實現)View
- CSS 自定義屬性指北CSS
- data-* 自定義屬性
- Android自定義屬性Android
- js:陣列自定義排序JS陣列排序
- JavaScript 陣列 length 屬性JavaScript陣列
- Android自定義控制元件——自定義屬性Android控制元件
- 初識css自定義屬性CSS
- CSS 自定義屬性(變數)CSS變數
- 屬性動畫:如何自定義View動畫View
- easyui tree自定義屬性用法UI
- CSS自定義屬性Expression(轉)CSSExpress
- 4. 自定義控制元件(4) --- 自定義屬性控制元件
- Android自定義控制元件之自定義屬性Android控制元件
- 【譯】CSS 自定義屬性的策略指南CSS
- Android 自定義View:深入理解自定義屬性(七)AndroidView
- ubuntu下OpenLDAP新增自定義屬性UbuntuLDA
- 使用 CSS 自定義屬性(變數)CSS變數
- Android自定義View 屬性新增AndroidView
- 給自定義View新增xml屬性ViewXML
- HTML5的data-*自定義屬性HTML
- .net自定義控制元件下拉的屬性控制元件
- Lesson4——NumPy 陣列屬性陣列
- PHP時間與系統時間不符合的問題以及PHP預定義超全域性陣列PHP陣列
- Android自定義組合控制元件之自定義屬性Android控制元件
- android中自定義屬性重複定義Android
- 認識JavaScript陣列物件的length屬性JavaScript陣列物件
- Android 自定義View:屬性動畫(六)AndroidView動畫
- css自定義屬性和聚光燈效果CSS
- spring 自定義屬性解析器Spring
- android 自定義控制元件 自定義屬性詳細介紹Android控制元件
- Javascript自定義陣列刪除方法remove()JavaScript陣列REM
- CoffeeScript攻略4.5:定義陣列範圍陣列
- js實現的陣列自定義排序介紹JS陣列排序