Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date format in collections / maps. #46

Merged
merged 1 commit into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ public JsonbConfigProperties(JsonbConfig jsonbConfig) {

private JsonbDateFormatter initDateFormatter(Locale locale) {
final String dateFormat = getGlobalConfigJsonbDateFormat();
//In case of java.time singleton formats will be used inside related (de)serializers,
if (JsonbDateFormat.DEFAULT_FORMAT.equals(dateFormat) || JsonbDateFormat.TIME_IN_MILLIS.equals(dateFormat)) {
return new JsonbDateFormatter(dateFormat, locale.toLanguageTag());
}
//if possible create shared instance of java.time formatter.
return new JsonbDateFormatter(DateTimeFormatter.ofPattern(dateFormat, locale), dateFormat, locale.toLanguageTag());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.yasson.internal.serializer;

import org.eclipse.yasson.internal.JsonbContext;
import org.eclipse.yasson.internal.Unmarshaller;
import org.eclipse.yasson.internal.properties.MessageKeys;
import org.eclipse.yasson.internal.properties.Messages;
Expand Down Expand Up @@ -48,11 +49,16 @@ public AbstractDateTimeDeserializer(Class<T> clazz, JsonBindingModel model) {

@Override
public T deserialize(String jsonValue, Unmarshaller unmarshaller, Type rtType) {
final JsonbDateFormatter formatter = getJsonbDateFormatter();
final JsonbDateFormatter formatter = getJsonbDateFormatter(unmarshaller.getJsonbContext());
if (JsonbDateFormat.TIME_IN_MILLIS.equals(formatter.getFormat())) {
return fromInstant(Instant.ofEpochMilli(Long.parseLong(jsonValue)));
} else if (formatter.getDateTimeFormatter() != null) {
return parseWithFormatterInternal(jsonValue, formatter.getDateTimeFormatter());
} else {
DateTimeFormatter configDateTimeFormatter = unmarshaller.getJsonbContext().getConfigProperties().getConfigDateFormatter().getDateTimeFormatter();
if (configDateTimeFormatter != null) {
return parseWithFormatterInternal(jsonValue, configDateTimeFormatter);
}
}
final boolean strictIJson = unmarshaller.getJsonbContext().getConfigProperties().isStrictIJson();
if (strictIJson) {
Expand All @@ -65,11 +71,11 @@ public T deserialize(String jsonValue, Unmarshaller unmarshaller, Type rtType) {
}
}

protected JsonbDateFormatter getJsonbDateFormatter() {
protected JsonbDateFormatter getJsonbDateFormatter(JsonbContext context) {
if (getModel() != null && getModel().getCustomization() != null && getModel().getCustomization().getDeserializeDateFormatter() != null) {
return getModel().getCustomization().getDeserializeDateFormatter();
}
return JsonbDateFormatter.getDefault();
return context.getConfigProperties().getConfigDateFormatter();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public AbstractDateTimeSerializer(JsonBindingModel model) {
@Override
public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) {
final JsonbContext jsonbContext = ((Marshaller) ctx).getJsonbContext();
final JsonbDateFormatter formatter = getJsonbDateFormatter();
final JsonbDateFormatter formatter = getJsonbDateFormatter(jsonbContext);
if (model.getContext() == JsonContext.JSON_OBJECT) {
generator.write(model.getWriteName(), toJson(obj, formatter, jsonbContext));
} else {
Expand All @@ -70,18 +70,23 @@ public String toJson(T object, JsonbDateFormatter formatter, JsonbContext jsonbC
return String.valueOf(toInstant(object).toEpochMilli());
} else if (formatter.getDateTimeFormatter() != null) {
return formatWithFormatter(object, formatter.getDateTimeFormatter());
} else {
DateTimeFormatter configDateTimeFormatter = jsonbContext.getConfigProperties().getConfigDateFormatter().getDateTimeFormatter();
if (configDateTimeFormatter != null) {
return formatWithFormatter(object, configDateTimeFormatter);
}
}
if (jsonbContext.getConfigProperties().isStrictIJson()) {
return formatStrictIJson(object);
}
return formatDefault(object, jsonbContext.getConfigProperties().getLocale(formatter.getLocale()));
}

protected JsonbDateFormatter getJsonbDateFormatter() {
protected JsonbDateFormatter getJsonbDateFormatter(JsonbContext context) {
if (model != null && model.getCustomization() != null && model.getCustomization().getSerializeDateFormatter() != null) {
return model.getCustomization().getSerializeDateFormatter();
}
return JsonbDateFormatter.getDefault();
return context.getConfigProperties().getConfigDateFormatter();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,52 @@ public void testSimpleTimeZone() {
Assert.assertEquals(LocalDateTime.now().atZone(ZoneId.of("America/Los_Angeles")).getOffset().getTotalSeconds() * 1000,
result.getValue().getOffset(System.currentTimeMillis()));
}

@Test
public void testDateInMap() {

JsonbConfig config = new JsonbConfig()
.withDateFormat("yyyy", Locale.ENGLISH);

Jsonb jsonb = JsonbBuilder.create(config);
LocalDate localDate = LocalDate.of(2017, 9, 14);

DateInMapPojo pojo = new DateInMapPojo();
pojo.setLocalDate(localDate);
pojo.setDateMap(new HashMap<>());
pojo.getDateMap().put("first", localDate);

String json = jsonb.toJson(pojo);

Assert.assertEquals("{\"dateMap\":{\"first\":\"2017\"},\"localDate\":\"2017\"}", json);

config = new JsonbConfig()
.withDateFormat("dd.MM.yyyy", Locale.ENGLISH);
jsonb = JsonbBuilder.create(config);
DateInMapPojo result = jsonb.fromJson("{\"dateMap\":{\"first\":\"01.01.2017\"},\"localDate\":\"01.01.2017\"}", DateInMapPojo.class);
Assert.assertEquals(LocalDate.of(2017,1,1), result.localDate);
Assert.assertEquals(LocalDate.of(2017,1,1), result.dateMap.get("first"));
}


public static class DateInMapPojo {
private LocalDate localDate;
private Map<String, LocalDate> dateMap;

public LocalDate getLocalDate() {
return localDate;
}

public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}

public Map<String, LocalDate> getDateMap() {
return dateMap;
}

public void setDateMap(Map<String, LocalDate> dateMap) {
this.dateMap = dateMap;
}
}
}