自定義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);
}
}
}
}
}
相關文章
- JSON繼承JsonConverter序列化與反序列化重寫類屬性JSON繼承
- [BUG反饋]模型屬性自定義函式提交不了模型函式
- NumPy 陣列屬性陣列
- data-* 自定義屬性
- CSS 自定義屬性指北CSS
- js:陣列自定義排序JS陣列排序
- JavaScript 陣列 length 屬性JavaScript陣列
- 初識css自定義屬性CSS
- ReactNative自定義元件及屬性React元件
- CSS 自定義屬性(變數)CSS變數
- 給Product新增自定義屬性
- 4. 自定義控制元件(4) --- 自定義屬性控制元件
- Android 自定義View:深入理解自定義屬性(七)AndroidView
- 【譯】CSS 自定義屬性的策略指南CSS
- React Native 自定義元件及屬性React Native元件
- ubuntu下OpenLDAP新增自定義屬性UbuntuLDA
- 自定義元件-資料、方法、屬性元件
- spring 自定義屬性解析器Spring
- 使用 CSS 自定義屬性(變數)CSS變數
- Lesson4——NumPy 陣列屬性陣列
- Android 自定義View:屬性動畫(六)AndroidView動畫
- Spring Cloud自定義引導屬性源SpringCloud
- Spring Boot讀取自定義外部屬性Spring Boot
- 處理陣列中某一屬性陣列
- 合併陣列物件中相同的屬性值陣列物件
- 屬性序列化自定義與字母表排序-JSON框架Jackson精解第3篇排序JSON框架
- CSS變數(自定義屬性)實踐指南CSS變數
- 使用CSS自定義屬性構建骨架屏CSS
- 自定義View:Paint的常用屬性介紹及使用ViewAI
- 帶你深入理解Android中的自定義屬性!!!Android
- java自定義序列化Java
- JS 陣列(Arrey)屬性以及方法詳解JS陣列
- 陣列物件按物件某個屬性排序陣列物件排序
- 【朝花夕拾】Android自定義View篇之(四)自定義View的三種實現方式及自定義屬性詳解AndroidView
- Python日誌記錄中新增自定義屬性Python
- SpringBoot入門(二):日誌及自定義屬性Spring Boot
- CSS Var 自定義屬性中使用 Scss 變數CSS變數
- C#規範整理·異常與自定義異常C#