Skip to content

Commit

Permalink
Base deserializer and try-catch block removed, no need to try to wrap…
Browse files Browse the repository at this point in the history
… anything, expected pattern is to let exception pass through if any.
  • Loading branch information
MichalFoksa committed Jun 14, 2021
1 parent 1bd02ba commit 8f52e52
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.fasterxml.jackson.dataformat.avro.jsr310.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
Expand All @@ -25,7 +18,7 @@
*
* @param <T> The type of a instant class that can be deserialized.
*/
public class AvroInstantDeserializer<T extends Temporal> extends StdScalarDeserializer<T> {
public class AvroInstantDeserializer<T extends Temporal> extends AvroJavaTimeDeserializerBase <T> {

private static final long serialVersionUID = 1L;

Expand All @@ -40,30 +33,13 @@ public class AvroInstantDeserializer<T extends Temporal> extends StdScalarDeseri

protected final BiFunction<Instant, ZoneId, T> fromInstant;

protected AvroInstantDeserializer(Class<T> t, BiFunction<Instant, ZoneId, T> fromInstant) {
super(t);
protected AvroInstantDeserializer(Class<T> supportedType, BiFunction<Instant, ZoneId, T> fromInstant) {
super(supportedType);
this.fromInstant = fromInstant;
}

@SuppressWarnings("unchecked")
@Override
public T deserialize(JsonParser p, DeserializationContext context) throws IOException, JsonProcessingException {
final ZoneId defaultZoneId = context.getTimeZone().toZoneId().normalized();
switch (p.getCurrentToken()) {
case VALUE_NUMBER_INT:
return fromLong(p.getLongValue(), defaultZoneId);
default:
try {
return (T) context.handleUnexpectedToken(_valueClass, p);
} catch (JsonMappingException e) {
throw e;
} catch (IOException e) {
throw JsonMappingException.fromUnexpectedIOE(e);
}
}
}

private T fromLong(long longValue, ZoneId defaultZoneId) {
protected T fromLong(long longValue, ZoneId defaultZoneId) {
/**
* Number of milliseconds, independent of a particular time zone or calendar,
* from 1 January 1970 00:00:00.000 UTC.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fasterxml.jackson.dataformat.avro.jsr310.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.type.LogicalType;

import java.io.IOException;
import java.time.ZoneId;

public abstract class AvroJavaTimeDeserializerBase<T> extends StdScalarDeserializer<T> {

protected AvroJavaTimeDeserializerBase(Class<T> supportedType) {
super(supportedType);
}

@Override
public LogicalType logicalType() {
return LogicalType.DateTime;
}

@SuppressWarnings("unchecked")
@Override
public T deserialize(JsonParser p, DeserializationContext context) throws IOException {
final ZoneId defaultZoneId = context.getTimeZone().toZoneId().normalized();
switch (p.getCurrentToken()) {
case VALUE_NUMBER_INT:
return fromLong(p.getLongValue(), defaultZoneId);
}
return (T) context.handleUnexpectedToken(_valueClass, p);
}

protected abstract T fromLong(long longValue, ZoneId defaultZoneId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.fasterxml.jackson.dataformat.avro.jsr310.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;

/**
* Deserializer for {@link LocalDate} from and integer value.
Expand All @@ -16,7 +10,7 @@
*
* Deserialization from string is not supported.
*/
public class AvroLocalDateDeserializer extends StdScalarDeserializer<LocalDate> {
public class AvroLocalDateDeserializer extends AvroJavaTimeDeserializerBase<LocalDate> {

private static final long serialVersionUID = 1L;

Expand All @@ -26,24 +20,8 @@ protected AvroLocalDateDeserializer() {
super(LocalDate.class);
}

@SuppressWarnings("unchecked")
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext context) throws IOException, JsonProcessingException {
switch (p.getCurrentToken()) {
case VALUE_NUMBER_INT:
return fromLong(p.getLongValue());
default:
try {
return (LocalDate) context.handleUnexpectedToken(_valueClass, p);
} catch (JsonMappingException e) {
throw e;
} catch (IOException e) {
throw JsonMappingException.fromUnexpectedIOE(e);
}
}
}

private LocalDate fromLong(long longValue) {
protected LocalDate fromLong(long longValue, ZoneId defaultZoneId) {
/**
* Number of days from the unix epoch, 1 January 1970..
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package com.fasterxml.jackson.dataformat.avro.jsr310.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

/**
Expand All @@ -19,7 +13,7 @@
*
* Deserialization from string is not supported.
*/
public class AvroLocalDateTimeDeserializer extends StdScalarDeserializer<LocalDateTime> {
public class AvroLocalDateTimeDeserializer extends AvroJavaTimeDeserializerBase<LocalDateTime> {

private static final long serialVersionUID = 1L;

Expand All @@ -29,24 +23,8 @@ protected AvroLocalDateTimeDeserializer() {
super(LocalDateTime.class);
}

@SuppressWarnings("unchecked")
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext context) throws IOException, JsonProcessingException {
switch (p.getCurrentToken()) {
case VALUE_NUMBER_INT:
return fromLong(p.getLongValue());
default:
try {
return (LocalDateTime) context.handleUnexpectedToken(_valueClass, p);
} catch (JsonMappingException e) {
throw e;
} catch (IOException e) {
throw JsonMappingException.fromUnexpectedIOE(e);
}
}
}

private LocalDateTime fromLong(long longValue) {
protected LocalDateTime fromLong(long longValue, ZoneId defaultZoneId) {
/**
* Number of milliseconds in a local timezone, regardless of what specific time zone is considered local,
* from 1 January 1970 00:00:00.000.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package com.fasterxml.jackson.dataformat.avro.jsr310.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;

/**
* Deserializer for {@link LocalTime} from an integer value.
Expand All @@ -18,34 +11,18 @@
*
* Deserialization from string is not supported.
*/
public class AvroLocalTimeDeserializer extends StdScalarDeserializer<LocalTime> {
public class AvroLocalTimeDeserializer extends AvroJavaTimeDeserializerBase<LocalTime> {

private static final long serialVersionUID = 1L;

public static final AvroLocalTimeDeserializer INSTANCE = new AvroLocalTimeDeserializer();

protected AvroLocalTimeDeserializer() {
super(LocalDateTime.class);
super(LocalTime.class);
}

@SuppressWarnings("unchecked")
@Override
public LocalTime deserialize(JsonParser p, DeserializationContext context) throws IOException, JsonProcessingException {
switch (p.getCurrentToken()) {
case VALUE_NUMBER_INT:
return fromLong(p.getLongValue());
default:
try {
return (LocalTime) context.handleUnexpectedToken(_valueClass, p);
} catch (JsonMappingException e) {
throw e;
} catch (IOException e) {
throw JsonMappingException.fromUnexpectedIOE(e);
}
}
}

private LocalTime fromLong(long longValue) {
protected LocalTime fromLong(long longValue, ZoneId defaultZoneId) {
/**
* Number of milliseconds, with no reference to a particular calendar, time zone or date, after
* midnight, 00:00:00.000.
Expand Down

0 comments on commit 8f52e52

Please sign in to comment.