gpt4 book ai didi

c# - 如何正确覆盖对象 [] 的 ReadJson 方法?

转载 作者:行者123 更新时间:2023-12-05 05:22:18 24 4
gpt4 key购买 nike

我正在尝试为对象[] 创建一个 json 转换器来定义我想要的类型(例如 int32 而不是 int64)。

这是我到现在为止得到的:

对象:

[JsonObject]
public class tipo
{
public string teste { get; set; }

[JsonConverter(typeof(TestConverter))]
public object[] metodos { get; set; }
}

主要内容:

    string call = JsonConvert.SerializeObject(new tipo { teste="nota",
metodos = new object[] { 10, (decimal)10.5, "Teste123" } });

tipo t = JsonConvert.DeserializeObject<tipo>(call);

JsonConverter:

namespace Newtonsoft.Json
{
public class TestConverter : JsonConverter
{
#region Overrides of JsonConverter

public override bool CanWrite { get { return false; } }

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)
{
List<object> resultado = new List<object>();

string propertyName = reader.Value as string;
while (reader.Read())
{
object value = null;

switch (reader.TokenType)
{
case JsonToken.Integer:
value = Convert.ToInt32(reader.Value);
resultado.Add(value);
break;
case JsonToken.Float:
value = Convert.ToDecimal(reader.Value);
resultado.Add(value);
break;
case JsonToken.String:
value = Convert.ToString(reader.Value);
resultado.Add(value);
break;
case JsonToken.Boolean:
value = Convert.ToBoolean(reader.Value);
resultado.Add(value);
break;
case JsonToken.Null:
value = null;
resultado.Add(value);
break;
case JsonToken.Date:
value = Convert.ToDateTime(reader.Value);
resultado.Add(value);
break;
case JsonToken.Bytes:
value = Convert.ToByte(reader.Value);
resultado.Add(value);
break;
default:
Console.WriteLine("Default case");
Console.WriteLine(reader.TokenType.ToString());
break;
}
}

return resultado.ToArray();
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(object[]);
}

#endregion
}
}

我检查了输出,一切正常,但是它在反序列化中返回了以下错误:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Unexpected end when deserializing object. Path '', line 1, position 47.

知道如何解决这个问题吗?

谢谢!!!

最佳答案

解决了这个问题,现在我有了一个可以转换为我想要的格式的通用转换器:

public class TestConverter : JsonConverter
{
#region Overrides of JsonConverter

public override bool CanWrite { get { return false; } }

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 value = new object();

if (reader.TokenType != JsonToken.Null)
{
if (reader.TokenType == JsonToken.StartArray)
{
JToken token = JToken.Load(reader);

List<object> resultado = new List<object>();

foreach (var Value in token)
{
switch (Value.Type)
{
case JTokenType.Integer:
value = Convert.ToInt32(Value);
resultado.Add(value);
break;
case JTokenType.Float:
value = Convert.ToDecimal(Value);
resultado.Add(value);
break;
case JTokenType.String:
value = Convert.ToString(Value);
resultado.Add(value);
break;
case JTokenType.Boolean:
value = Convert.ToBoolean(Value);
resultado.Add(value);
break;
case JTokenType.Null:
value = null;
resultado.Add(value);
break;
case JTokenType.Date:
value = Convert.ToDateTime(Value);
resultado.Add(value);
break;
case JTokenType.Bytes:
value = Convert.ToByte(Value);
resultado.Add(value);
break;
default:
Console.WriteLine("Default case");
Console.WriteLine(reader.TokenType.ToString());
break;
}
}
value = resultado.ToArray();
}
else
{
JValue jValue = new JValue(reader.Value);
switch (reader.TokenType)
{
case JsonToken.Integer:
value = Convert.ToInt32(reader.Value);
break;
case JsonToken.Float:
value = Convert.ToDecimal(reader.Value);
break;
case JsonToken.String:
value = Convert.ToString(reader.Value);
break;
case JsonToken.Boolean:
value = Convert.ToBoolean(reader.Value);
break;
case JsonToken.Null:
value = null;
break;
case JsonToken.Date:
value = Convert.ToDateTime(reader.Value);
break;
case JsonToken.Bytes:
value = Convert.ToByte(reader.Value);
break;
default:
Console.WriteLine("Default case");
Console.WriteLine(reader.TokenType.ToString());
break;
}
}
}
return value;
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(object);
}

#endregion
}

关于c# - 如何正确覆盖对象 [] 的 ReadJson 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40730629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com