Skip to content
Open
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 @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any known performance problems with the java.time.DateTimeFormatter vs the FastDateTimeFormatter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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;
}

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")
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The date pattern has been changed from "yyyy-MM-dd HH:mm:ss.S z" (single 'S' for milliseconds) to "yyyy-MM-dd HH:mm:ss.SSS z" (triple 'S'). This changes the output format and is not backward compatible. A date with milliseconds value 5 would previously be formatted as "5" but will now be formatted as "005". This could break existing clients that parse these dates. Consider using the original pattern "yyyy-MM-dd HH:mm:ss.S z" to maintain backward compatibility.

Suggested change
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S z")

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to revert or justify and add documentation for this change.

.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;
}

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.web.converters.marshaller.json

import java.text.SimpleDateFormat

import grails.converters.JSON

import org.grails.web.json.JSONWriter

import spock.lang.Specification

class CalendarMarshallerSpec extends Specification {

void "supports returns true for Calendar instances"() {
given:
def marshaller = new CalendarMarshaller()

expect:
marshaller.supports(Calendar.getInstance())
marshaller.supports(new GregorianCalendar())
}

void "supports returns false for non-Calendar instances"() {
given:
def marshaller = new CalendarMarshaller()

expect:
!marshaller.supports(new Date())
!marshaller.supports("not a calendar")
!marshaller.supports(null)
}

void "default formatter produces ISO-8601 UTC format with Z suffix"() {
given:
def marshaller = new CalendarMarshaller()
def calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar.setTimeInMillis(1718461845123L)

when:
def result = marshalToString(marshaller, calendar)

then:
result == '["2024-06-15T14:30:45.123Z"]'
}

void "default formatter converts non-UTC calendar to UTC"() {
given:
def marshaller = new CalendarMarshaller()
def calendar = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"))
calendar.setTimeInMillis(1718461845123L)

when:
def result = marshalToString(marshaller, calendar)

then: "output is always UTC regardless of calendar timezone"
result == '["2024-06-15T14:30:45.123Z"]'
}

void "default formatter pads milliseconds to three digits"() {
given:
def marshaller = new CalendarMarshaller()
def calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar.setTimeInMillis(1704067200005L)

when:
def result = marshalToString(marshaller, calendar)

then:
result == '["2024-01-01T00:00:00.005Z"]'
}

void "legacy formatter is used when provided"() {
given:
def customFormat = new SimpleDateFormat("dd/MM/yyyy")
customFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
def marshaller = new CalendarMarshaller(customFormat)
def calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar.setTimeInMillis(1718461845123L)

when:
def result = marshalToString(marshaller, calendar)

then:
result == '["15/06/2024"]'
}

private static String marshalToString(CalendarMarshaller marshaller, Calendar calendar) {
def json = new JSON()
def stringWriter = new StringWriter()
json.writer = new JSONWriter(stringWriter)
json.writer.array()
marshaller.marshalObject(calendar, json)
json.writer.endArray()
stringWriter.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.web.converters.marshaller.json

import java.text.SimpleDateFormat

import grails.converters.JSON

import org.grails.web.json.JSONWriter

import spock.lang.Specification

class DateMarshallerSpec extends Specification {

void "supports returns true for Date instances"() {
given:
def marshaller = new DateMarshaller()

expect:
marshaller.supports(new Date())
}

void "supports returns false for non-Date instances"() {
given:
def marshaller = new DateMarshaller()

expect:
!marshaller.supports("not a date")
!marshaller.supports(42)
!marshaller.supports(null)
}

void "default formatter produces ISO-8601 UTC format with Z suffix"() {
given:
def marshaller = new DateMarshaller()
def date = new Date(1718461845123L)

when:
def result = marshalToString(marshaller, date)

then:
result == '["2024-06-15T14:30:45.123Z"]'
}

void "default formatter formats zero milliseconds correctly"() {
given:
def marshaller = new DateMarshaller()
// 2024-01-01T00:00:00.000 UTC
def date = new Date(1704067200000L)

when:
def result = marshalToString(marshaller, date)

then:
result == '["2024-01-01T00:00:00.000Z"]'
}

void "default formatter pads milliseconds to three digits"() {
given:
def marshaller = new DateMarshaller()
// 5 milliseconds past epoch second
def date = new Date(1704067200005L)

when:
def result = marshalToString(marshaller, date)

then:
result == '["2024-01-01T00:00:00.005Z"]'
}

void "legacy formatter is used when provided"() {
given:
def customFormat = new SimpleDateFormat("dd/MM/yyyy")
customFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
def marshaller = new DateMarshaller(customFormat)
def date = new Date(1718461845123L)

when:
def result = marshalToString(marshaller, date)

then:
result == '["15/06/2024"]'
}

private static String marshalToString(DateMarshaller marshaller, Date date) {
def json = new JSON()
def stringWriter = new StringWriter()
json.writer = new JSONWriter(stringWriter)
json.writer.array()
marshaller.marshalObject(date, json)
json.writer.endArray()
stringWriter.toString()
}
}
Loading
Loading