Adding deserializer for ZonedDateTime

Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>
This commit is contained in:
Matthias Wessendorf 2018-11-12 10:43:27 +01:00
parent c0f32ed7df
commit 02ede16de4
3 changed files with 48 additions and 2 deletions

View File

@ -35,14 +35,12 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -15,6 +15,7 @@
*/
package io.cloudevents.impl;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.cloudevents.CloudEvent;
import java.io.Serializable;
@ -137,6 +138,7 @@ public class DefaultCloudEventImpl<T> implements CloudEvent<T>, Serializable {
this.eventTypeVersion = eventTypeVersion;
}
@JsonDeserialize(using = ZonedDateTimeDeserializer.class)
void setEventTime(ZonedDateTime eventTime) {
this.eventTime = eventTime;
}

View File

@ -0,0 +1,46 @@
/**
* Copyright 2018 The CloudEvents Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cloudevents.impl;
import java.io.IOException;
import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class ZonedDateTimeDeserializer extends StdDeserializer<ZonedDateTime> {
public ZonedDateTimeDeserializer() {
this(null);
}
public ZonedDateTimeDeserializer(Class<?> vc) {
super(vc);
}
@Override
public ZonedDateTime deserialize(JsonParser jsonparser, DeserializationContext ctxt) throws IOException {
// not serializing timezone data yet
try {
return ZonedDateTime.parse(jsonparser.getText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
} catch (DateTimeException e) {
throw new IllegalArgumentException("could not parse");
}
}
}