JSON序列增加额外字符作为“\'in响应,同时自定义序列化的自定义数据类型JsonConverter [英] JSON serializer adding extra character as '\'in response while serializing custom datatype in custom JsonConverter

查看:165
本文介绍了JSON序列增加额外字符作为“\'in响应,同时自定义序列化的自定义数据类型JsonConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义的JsonConverter(CustomInfoConverter)做在我的每一个自定义的点网型(CustomType)这是越来越解析某些操作。

I am using custom JsonConverter(CustomInfoConverter ) to do some manipulation in each of my custom dot net type(CustomType) which is getting parse.

下面是代码自定义JsonConverter:

Below is the code for custom JsonConverter:

public class CustomInfoConverter : JsonConverter
{

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(CustomType);
    }
    public override bool CanRead
    {
        get
        {
            return false;
        }
    }
    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var customType = (CustomType)value;
        if (customType == null || null== customType.TimeZone) return;
        //DateTime currentDateTime = customType.Date??DateTime.Now;
        DateTime currentDateTime = DateTime.SpecifyKind(customType.Date ?? DateTime.Now, DateTimeKind.Unspecified);

        DateTime userDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentDateTime, customType.TimeZone);
        customType.Date = userDateTime;
        writer.WriteValue(JsonConvert.SerializeObject(customType) );


    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }


}



这个问题我现在面临这里是多余的character'\'是越来越追加在此处,所以我的反应看起来像 JsonConvert.SerializeObject(customType)是序列化customType对象并再次默认序列是其序列(纠正我,如果我错了)。什么是这个

The issue I am facing here is extra character'\' is getting appended here, So my response looks like JsonConvert.SerializeObject(customType) is serializing customType object and again default serializer is serializing it(correct me if I am wrong). What would be solution for this?

   {
  "Data": [
    {
      "Type": "someThing",
      "Created": "{\"Date\":\"2015-11-05T01:09:21.6721171+05:30\",\"User\":{\"Upn\":\"james\",\"FirstName\":\"James\",\"LastName\":\"Johnson\",\"Id\":\"69cb471e-da83-48c3-8f60-d1bb29d41177\",\"Name\":\"Johnson\"},\"TimeZone\":{\"Id\":\"India Standard Time\",\"DisplayName\":\"(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi\",\"StandardName\":\"India Standard Time\",\"DaylightName\":\"India Daylight Time\",\"BaseUtcOffset\":\"05:30:00\",\"AdjustmentRules\":null,\"SupportsDaylightSavingTime\":false},\"Latitude\":null,\"Longitude\":null}",
    }
  ],

}

这里我才知道,这可能是因为重新JSON-ING,
那么,这将是该解决方案的?

From here I came to know that it may be because of re-json-ing, So, what would be the solution for this?

推荐答案

问题是这样的行:

writer.WriteValue(JsonConvert.SerializeObject(customType));



相反,你应该这样做是为了避免双序列:

Instead you should be doing this to avoid the double-serialization:

serializer.Serialize(writer, customType);



不过,由于您试图序列化是同一个对象,你的转换器把,上面会导致一个自引用循环。在这种情况下,你需要使用序列化的新实例。试试这个:

However, since you are trying to serialize the same object that your converter converts, the above will result in an self-referencing loop. In this case you need to use a new instance of the serializer. Try this instead:

new JsonSerializer().Serialize(writer, customType);

请注意:如果您正在使用任何其他转换器(或自定义设置),新的串行实例不会了解它们,除非你明确他们从外部串行复制。如果你需要做到这一点,你一定要做的的复制 CustomInfoConverter 到新的实例,否则您将结束与自引用循环再次的问题。要复制器,用下面的替换上面:

Note: if you are using any other converters (or custom settings), the new serializer instance will not know about them unless you specifically copy them from the outer serializer. If you need to do this, make sure you do not copy the CustomInfoConverter to the new instance or you will end up with the self-referencing loop problem again. To copy the converters, replace the above with the following:

JsonSerializer innerSerializer = new JsonSerializer();
foreach (var converter in serializer.Converters.Where(c => !(c is CustomInfoConverter)))
{
    innerSerializer.Converters.Add(converter);
}
innerSerializer.Serialize(writer, customType);

这篇关于JSON序列增加额外字符作为“\'in响应,同时自定义序列化的自定义数据类型JsonConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆