Skip to content

Commit

Permalink
#2253 - Incorrect JSON format for LocalDateTime. Includes Z suffix wh…
Browse files Browse the repository at this point in the history
…en it should not.
  • Loading branch information
rbygrave committed Jun 17, 2021
1 parent 37a3a51 commit 7af4e16
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.ebeaninternal.server.type;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.JsonConfig;

import java.io.IOException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
Expand Down Expand Up @@ -29,12 +32,37 @@ public long convertToMillis(LocalDateTime value) {

@Override
protected String toJsonNanos(LocalDateTime value) {
return String.valueOf(convertToMillis(value));
return value.toString();
}

@Override
protected String toJsonISO8601(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toString();
protected String toJsonISO8601(LocalDateTime value) {
return value.toString();
}

@Override
protected LocalDateTime fromJsonISO8601(String value) {
return LocalDateTime.parse(value);
}

@Override
public LocalDateTime jsonRead(JsonParser parser) throws IOException {
return LocalDateTime.parse(parser.getText());
}

@Override
public void jsonWrite(JsonGenerator writer, LocalDateTime value) throws IOException {
writer.writeString(value.toString());
}

@Override
public String formatValue(LocalDateTime value) {
return value.toString();
}

@Override
public LocalDateTime parse(String value) {
return LocalDateTime.parse(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package io.ebeaninternal.server.type;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import io.ebean.config.JsonConfig;
import org.junit.Test;

import java.io.StringWriter;
import java.sql.Timestamp;
import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class ScalarTypeLocalDateTimeTest {

ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS);
private final ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS);

private final JsonFactory factory = new JsonFactory();

// warm up
LocalDateTime warmUp = LocalDateTime.now();
private final LocalDateTime warmUp = LocalDateTime.now();

@Test
public void testNowToMillis() throws Exception {
public void testNowToMillis() {

warmUp.hashCode();

Expand All @@ -29,7 +32,7 @@ public void testNowToMillis() throws Exception {
}

@Test
public void testConvertToMillis() throws Exception {
public void testConvertToMillis() {

LocalDateTime now = LocalDateTime.now().withNano(123_000_000); // jdk11 workaround
long asMillis = type.convertToMillis(now);
Expand All @@ -39,7 +42,7 @@ public void testConvertToMillis() throws Exception {
}

@Test
public void testConvertFromTimestamp() throws Exception {
public void testConvertFromTimestamp() {

Timestamp now = new Timestamp(System.currentTimeMillis());

Expand Down Expand Up @@ -74,6 +77,24 @@ public void testToBeanType() throws Exception {
assertEquals(timestamp, timestamp1);
}


@Test
public void testJsonRaw() throws Exception {

final LocalDateTime of = LocalDateTime.of(2020, 5, 4, 13, 20, 40);

ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);

StringWriter writer = new StringWriter();
JsonGenerator generator = factory.createGenerator(writer);

typeIso.jsonWrite(generator, of);
generator.flush();

assertThat(of.toString()).isEqualTo("2020-05-04T13:20:40");
assertThat(writer.toString()).isEqualTo("\"2020-05-04T13:20:40\"");
}

@Test
public void testJson() throws Exception {

Expand Down

0 comments on commit 7af4e16

Please sign in to comment.