NewtonSoft.Json自定义JsonConverter反序列化到DateTime不工作 [英] NewtonSoft.Json custom JsonConverter deserialize to DateTime not working

查看:1356
本文介绍了NewtonSoft.Json自定义JsonConverter反序列化到DateTime不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化 Unix时间戳 DateTime 。在我的情况下,我需要做更多的检查,然后才能从时间戳记将属性设置为DateTime。如果我从 Newtonsoft.Json 中使用 DateTime ,则将其反序列化为 UTC 时间,我需要反序列化到特定的时区

I am trying to deserialize a Unix timestamp to a DateTime. In my case, I need to do much more checks before I can set a property to DateTime from a timestamp. If I use DateTime from Newtonsoft.Json it deserializes it to UTC time and I need to deserialize it to a specific timezone

问题是我无法获得正确的时间。看起来我的字符串 long 解析失败。如果我可以获得 long unix时间戳,我可以得到其余的逻辑工作

The problem is that I am not able to get the correct time. It seems like my string to long parsing is failing. If I can get the long unix timestamp, I can get the rest of the logic working

我有一个类命名为警报

class Alert
{
    // Some properties

    [JsonConverter(typeof(UnixTimestampJsonConverter))]
    public DateTime Created { get; set; }

    // Some more properties
}

UnixTimestampJsonConverter

class UnixTimestampJsonConverter : JsonConverter
{
    // Other override methods

    public override object ReadJson (JsonReader reader, Type objectType, 
        object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.EndObject)
            return null;

        if (reader.TokenType == JsonToken.StartObject) {
            long instance = serializer.Deserialize<long> (reader);
            return TimeUtils.GetCustomDateTime (instance);
        }

        return null;
    }
}

其中 TimeUtils.GetCustomDateTime实例)使用长的unixtimestamp并将其转换为特定时区的DateTime对象。

Where TimeUtils.GetCustomDateTime (instance) takes the long unixtimestamp and converts it into DateTime object of specific timezone.

我在一个PCL库中具有个人资料78 ,所以我有限制访问 System.TimeZoneInfo ,我正在使用PCL版本的 NodaTime 其他时区计算。

I am in a PCL library with Profile 78, so I have limited access to System.TimeZoneInfo and I am using PCL version of NodaTime for other timezone calculations.

如果有人有兴趣,这是Github上的项目 - MBTA Sharp

In case anyone is interested, this is the project on Github - MBTA Sharp

推荐答案

我很确定你需要做的是调用 serializer.Deserialize 。这样做会提高读者的正确性,你不应该做任何其他事情:

I'm pretty sure all you need to do is call serializer.Deserialize. Doing this will advance the reader correctly and you shouldn't need to do anything else:

public class UnixTimestampJsonConverter : JsonConverter
{
    public override object ReadJson(
        JsonReader reader,
        Type objectType,
        object existingValue,
        JsonSerializer serializer)
    {
        long ts = serializer.Deserialize<long>(reader);

        return TimeUtils.GetMbtaDateTime(ts);
    }

    public override  bool CanConvert(Type type)
    {
        return typeof(DateTime).IsAssignableFrom(type);
    }

    public override void WriteJson(
        JsonWriter writer,
        object value,
        JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanRead
    { 
        get { return true; } 
    }
}

示例: a href =https://dotnetfiddle.net/Fa8Zis =noreferrer> https://dotnetfiddle.net/Fa8Zis

Example: https://dotnetfiddle.net/Fa8Zis

这篇关于NewtonSoft.Json自定义JsonConverter反序列化到DateTime不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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