gpt4 book ai didi

java - Jackson 序列化和反序列化 DateTime From/To W​​CF DateTime

转载 作者:行者123 更新时间:2023-12-04 10:30:31 26 4
gpt4 key购买 nike

我使用 Jackson 来序列化和反序列化对象。我有 .NET WCF DateTime JSON 格式和 Time Zone 。并且 jackson 无法将 JSON 反序列化为对象。我找到了一些解决方案,但是有没有嵌入的 Jackson 或建议的解决方案如何做到这一点?

示例日期:

  • "/日期(12345678989+0000)/"
  • "/日期(12345678989-0000)/"

  • 也可以看看:
    How to parse .net DateTime received as json string into java's Date object

    最佳答案

    .NET 生成的格式处理日期时间的 JavaScriptSerializer形式/Date(number of ticks)/您需要实现自定义反序列化器。要了解什么是“滴答声”,让我们看看 documentation :

    Date object, represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

    The maximum supported date value is MaxValue (12/31/9999 11:59:59 PM) and the minimum supported date value is MinValue (1/1/0001 12:00:00 AM).



    我假设,在您的情况下,您也提供了一个偏移量,就像在示例中一样。

    使用 Java 8 Time包和以上知识,我们可以实现自定义反序列化器如下加上示例用法:
    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.databind.DeserializationContext;
    import com.fasterxml.jackson.databind.JsonDeserializer;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

    import java.io.IOException;
    import java.time.Instant;
    import java.time.OffsetDateTime;
    import java.time.ZoneOffset;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class JsonPathApp {

    public static void main(String[] args) throws Exception {
    String inputJson = "{\"date\":\"/Date(1583001930882+0100)/\"}";

    ObjectMapper mapper = new ObjectMapper();
    Epoch epoch = mapper.readValue(inputJson, Epoch.class);
    System.out.println(epoch.getDate());
    }
    }

    class Epoch {

    @JsonDeserialize(using = JavaScriptDateDeserializer.class)
    private OffsetDateTime date;

    public OffsetDateTime getDate() {
    return date;
    }

    public void setDate(OffsetDateTime date) {
    this.date = date;
    }
    }

    class JavaScriptDateDeserializer extends JsonDeserializer<OffsetDateTime> {

    private final Pattern JAVASCRIPT_DATE = Pattern.compile("/Date\\((-?\\d+)([+-]\\d+)\\)/");

    @Override
    public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    String value = p.getValueAsString();
    Matcher matcher = JAVASCRIPT_DATE.matcher(value);
    if (matcher.matches()) {
    String epoch = matcher.group(1);
    String offset = matcher.group(2);

    Instant instant = Instant.ofEpochMilli(Long.parseLong(epoch));

    return OffsetDateTime.ofInstant(instant, ZoneOffset.of(offset));
    }

    return null;
    }
    }

    上面的代码打印:
    2020-02-29T19:45:30.882+01:00

    序列化器可能如下所示:
    class JavaScriptDateSerializer extends JsonSerializer<OffsetDateTime> {

    @Override
    public void serialize(OffsetDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    StringBuilder builder = new StringBuilder(32);
    builder.append("/Date(");
    builder.append(value.toInstant().toEpochMilli());
    if (!value.getOffset().equals(ZoneOffset.UTC)) {
    builder.append(value.getOffset().toString());
    }
    builder.append(")/");

    gen.writeString(builder.toString());
    }
    }

    如果您总是在 UTC 中发送/接收日期,则需要正确处理时区你可以跳过这部分。

    也可以看看:
  • WCF and JSON Date format
  • JavaScript's Date
  • Unix epoch time to Java Date object
  • javascriptserializer date format issue
  • Good (Date)Times with Json.NET
  • C# DateTime.Ticks equivalent in Java
  • 关于java - Jackson 序列化和反序列化 DateTime From/To W​​CF DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60446822/

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