Skip to content

Commit

Permalink
version 1.3.0: update date time deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed Apr 18, 2024
1 parent 734f173 commit dd42e68
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.tvd12.ezyhttp.core.json;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.function.Consumer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand All @@ -19,6 +11,14 @@
import com.tvd12.ezyfox.io.EzyDates;
import com.tvd12.ezyfox.jackson.JacksonObjectMapperBuilder;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.function.Consumer;

public class ObjectMapperBuilder implements EzyBuilder<ObjectMapper> {

private Consumer<ObjectMapper> decorator;
Expand Down Expand Up @@ -131,7 +131,12 @@ public LocalTime deserialize(JsonParser p, DeserializationContext ctxt)
return EzyDates.millisToDateTime(p.getValueAsLong())
.toLocalTime();
} else if (p.currentTokenId() == JsonToken.VALUE_STRING.id()) {
return EzyDates.parseTime(p.getValueAsString());
String value = p.getValueAsString();
try {
return LocalTime.parse(value);
} catch (Exception e) {
return EzyDates.parseTime(value);
}
} else {
throw new IOException(
"can deserialize value: " + p.getValueAsString() + " to LocalTime"
Expand All @@ -153,7 +158,12 @@ public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt)
if (p.currentTokenId() == JsonToken.VALUE_NUMBER_INT.id()) {
return EzyDates.millisToDateTime(p.getValueAsLong());
} else if (p.currentTokenId() == JsonToken.VALUE_STRING.id()) {
return EzyDates.parseDateTime(p.getValueAsString());
String value = p.getValueAsString();
try {
return LocalDateTime.parse(value);
} catch (Exception e) {
return EzyDates.parseDateTime(value);
}
} else {
throw new IOException(
"can deserialize value: " + p.getValueAsString() + " to LocalDateTime"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.tvd12.ezyhttp.core.test.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tvd12.ezyfox.io.EzyDates;
import com.tvd12.ezyhttp.core.json.ObjectMapperBuilder;
import com.tvd12.test.assertion.Asserts;
import org.testng.annotations.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -9,13 +15,6 @@
import java.time.LocalTime;
import java.util.Date;

import org.testng.annotations.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tvd12.ezyfox.io.EzyDates;
import com.tvd12.ezyhttp.core.json.ObjectMapperBuilder;
import com.tvd12.test.assertion.Asserts;

public class ObjectMapperBuilderTest {

private final ObjectMapper sut = new ObjectMapperBuilder()
Expand Down Expand Up @@ -136,6 +135,18 @@ public void localTimeTest() {
Asserts.assertEquals(EzyDates.parseTime(source), actual);
}

@Test
public void localTimeWitMinuteTest() {
// given
String source = "20:00";

// when
LocalTime actual = sut.convertValue(source, LocalTime.class);

// then
Asserts.assertEquals(LocalTime.parse(source), actual);
}

@Test
public void localTimeLongTest() {
// given
Expand Down Expand Up @@ -170,6 +181,18 @@ public void localDateTimeTest() {
Asserts.assertEquals(EzyDates.parseDateTime(source), actual);
}

@Test
public void localDateTimeJavaTest() {
// given
String source = "2021-01-30T10:20:30";

// when
LocalDateTime actual = sut.convertValue(source, LocalDateTime.class);

// then
Asserts.assertEquals(LocalDateTime.parse(source), actual);
}

@Test
public void localDateLongTimeTest() {
// given
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>

<artifactId>ezyhttp-server-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>

<artifactId>ezyhttp-server-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>
<artifactId>ezyhttp-server-graphql</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>

<artifactId>ezyhttp-server-jetty</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>
<artifactId>ezyhttp-server-management</artifactId>
<name>ezyhttp-server-management</name>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>
<artifactId>ezyhttp-server-thymeleaf</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</parent>

<artifactId>ezyhttp-server-tomcat</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.6</version>
</parent>
<artifactId>ezyhttp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
<packaging>pom</packaging>

<name>ezyhttp</name>
Expand Down

0 comments on commit dd42e68

Please sign in to comment.