Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -19,11 +19,10 @@
package org.grails.web.converters.marshaller.json;

import java.text.Format;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang3.time.FastDateFormat;

import grails.converters.JSON;
import org.grails.web.converters.exceptions.ConverterException;
Expand All @@ -37,21 +36,25 @@
*/
public class CalendarMarshaller implements ObjectMarshaller<JSON> {

private final Format formatter;
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
.withZone(ZoneOffset.UTC);

private final Format legacyFormatter;

/**
* Constructor with a custom formatter.
* @param formatter the formatter
*/
public CalendarMarshaller(Format formatter) {
this.formatter = formatter;
this.legacyFormatter = formatter;
}

/**
* Default constructor.
*/
public CalendarMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("GMT"), Locale.US));
this.legacyFormatter = null;
Comment thread
codeconsole marked this conversation as resolved.
Outdated
Comment thread
codeconsole marked this conversation as resolved.
Outdated
}

public boolean supports(Object object) {
Expand All @@ -61,7 +64,10 @@ public boolean supports(Object object) {
public void marshalObject(Object object, JSON converter) throws ConverterException {
try {
Calendar calendar = (Calendar) object;
converter.getWriter().value(formatter.format(calendar.getTime()));
String formatted = legacyFormatter != null ?
legacyFormatter.format(calendar.getTime()) :
DEFAULT_FORMATTER.format(calendar.toInstant());
converter.getWriter().value(formatted);
}
catch (JSONException e) {
throw new ConverterException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package org.grails.web.converters.marshaller.json;

import java.text.Format;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang3.time.FastDateFormat;

import grails.converters.JSON;
import org.grails.web.converters.exceptions.ConverterException;
Expand All @@ -39,21 +38,25 @@
*/
public class DateMarshaller implements ObjectMarshaller<JSON> {

private final Format formatter;
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
.withZone(ZoneOffset.UTC);

private final Format legacyFormatter;

/**
* Constructor with a custom formatter.
* @param formatter the formatter
*/
public DateMarshaller(Format formatter) {
this.formatter = formatter;
this.legacyFormatter = formatter;
}

/**
* Default constructor.
*/
public DateMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("GMT"), Locale.US));
this.legacyFormatter = null;
Comment thread
codeconsole marked this conversation as resolved.
Outdated
}

public boolean supports(Object object) {
Expand All @@ -62,7 +65,11 @@ public boolean supports(Object object) {

public void marshalObject(Object object, JSON converter) throws ConverterException {
try {
converter.getWriter().value(formatter.format(object));
Date date = (Date) object;
String formatted = legacyFormatter != null ?
legacyFormatter.format(date) :
DEFAULT_FORMATTER.format(date.toInstant());
converter.getWriter().value(formatted);
}
catch (JSONException e) {
throw new ConverterException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package org.grails.web.converters.marshaller.xml;

import java.text.Format;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.apache.commons.lang3.time.FastDateFormat;

import grails.converters.XML;
import org.grails.web.converters.ConverterUtil;
import org.grails.web.converters.exceptions.ConverterException;
Expand All @@ -34,21 +34,25 @@
*/
public class DateMarshaller implements ObjectMarshaller<XML> {

private final Format formatter;
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")
Comment thread
codeconsole marked this conversation as resolved.
Outdated
.withZone(ZoneId.systemDefault());

private final Format legacyFormatter;

/**
* Constructor with a custom formatter.
* @param formatter the formatter
*/
public DateMarshaller(Format formatter) {
this.formatter = formatter;
this.legacyFormatter = formatter;
}

/**
* Default constructor.
*/
public DateMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.S z"));
this.legacyFormatter = null;
Comment thread
codeconsole marked this conversation as resolved.
Outdated
}

public boolean supports(Object object) {
Expand All @@ -57,7 +61,11 @@ public boolean supports(Object object) {

public void marshalObject(Object object, XML xml) throws ConverterException {
try {
xml.chars(formatter.format(object));
Date date = (Date) object;
String formatted = legacyFormatter != null ?
legacyFormatter.format(date) :
DEFAULT_FORMATTER.format(date.toInstant());
xml.chars(formatted);
}
catch (Exception e) {
throw ConverterUtil.resolveConverterException(e);
Expand Down
Loading