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,39 +19,37 @@
package org.grails.web.converters.marshaller.json;

import java.text.Format;
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;
import org.grails.web.converters.marshaller.ObjectMarshaller;
import org.grails.web.json.JSONException;

/**
* JSON ObjectMarshaller which converts a Calendar Object to ISO-8601 format with Z suffix.
* JSON ObjectMarshaller which converts a Calendar Object to an RFC 3339 / ISO 8601
* UTC instant string (e.g. {@code 2024-06-15T14:30:45.123Z}).
*
* @since 7.0
*/
public class CalendarMarshaller implements ObjectMarshaller<JSON> {

private final Format formatter;
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.
* Default constructor — uses {@link DateTimeFormatter#ISO_INSTANT}.
*/
public CalendarMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("GMT"), Locale.US));
this(null);
}

public boolean supports(Object object) {
Expand All @@ -61,7 +59,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()) :
DateTimeFormatter.ISO_INSTANT.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,41 +19,38 @@
package org.grails.web.converters.marshaller.json;

import java.text.Format;
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;
import org.grails.web.converters.marshaller.ObjectMarshaller;
import org.grails.web.json.JSONException;

/**
* JSON ObjectMarshaller which converts a Date Object, conforming to the ECMA-Script-Specification
* Draft, to a String value.
* JSON ObjectMarshaller which converts a Date Object to an RFC 3339 / ISO 8601
* UTC instant string (e.g. {@code 2024-06-15T14:30:45.123Z}).
*
* @author Siegfried Puchbauer
* @since 1.1
*/
public class DateMarshaller implements ObjectMarshaller<JSON> {

private final Format formatter;
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.
* Default constructor — uses {@link DateTimeFormatter#ISO_INSTANT}.
*/
public DateMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("GMT"), Locale.US));
this(null);
}

public boolean supports(Object object) {
Expand All @@ -62,7 +59,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) :
DateTimeFormatter.ISO_INSTANT.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,36 +19,43 @@
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;
import org.grails.web.converters.marshaller.ObjectMarshaller;

/**
* XML ObjectMarshaller which converts a Date Object to an ISO 8601 offset
* date-time string in the system default zone (e.g. {@code 2024-06-15T14:30:45.123-04:00}).
*
* @author Siegfried Puchbauer
* @since 1.1
*/
public class DateMarshaller implements ObjectMarshaller<XML> {

private final Format formatter;
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ISO_OFFSET_DATE_TIME.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.
* Default constructor — uses {@link DateTimeFormatter#ISO_OFFSET_DATE_TIME}
* with the system default zone.
*/
public DateMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.S z"));
this(null);
}

public boolean supports(Object object) {
Expand All @@ -57,7 +64,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 sub-100 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()
}
}
Loading
Loading