jdaugherty commented on code in PR #16411: URL: https://github.com/apache/grails-core/pull/16411#discussion_r4124385375
########## grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/MonthMarshaller.java: ########## @@ -0,0 +1,49 @@ +/* + * 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.time.Month; + +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 Month to its number, 1 for January through 12 for December, + * the same as Spring Boot's default Jackson rendering. It is registered ahead of + * {@link SimpleEnumMarshaller}, which would otherwise render the enum name. + * + * @since 8.0 + */ +public class MonthMarshaller implements ObjectMarshaller<JSON> { + + public boolean supports(Object object) { + return object instanceof Month; + } + + public void marshalObject(Object object, JSON converter) throws ConverterException { + try { + converter.getWriter().value(((Month) object).getValue()); Review Comment: Two points the reply doesn't cover: - **Apps that followed the 7.0.2 deprecation notice already render `"SEPTEMBER"`.** 7.0.x reads `grails.converters.json.enum.format: simple`, the setting §8 now tells people they can remove. Those apps render `Month` as `"SEPTEMBER"` in `grails.converters.JSON` today, as JSON views do. For them, and for every JSON views user, `9` is a new change. It also breaks, for this one enum, the §8 contract we announced ("enums are serialized as simple string values"). The XML converter still writes `SEPTEMBER`, so the same object would render as `9` in JSON and `SEPTEMBER` in XML. - **#16414 is an open PR against 9.0.x, not a decided direction.** Whether 9.0 renders `grails.converters.JSON` through Boot's `JsonMapper` is a 9.0 question, and a PR still under review shouldn't settle 8.0 behavior. Please keep `Month` rendering as `"SEPTEMBER"` in 8.0: - Drop the `Month` registration from `ConvertersConfigurationInitializer` and `JsonViewTemplateEngine`. - Keep `monthValueConverter`, which binds `9`, `"9"` and `"SEPTEMBER"` and is a good change either way. I tried this locally. The only failures are the assertions that pin `Month` as a number, in `JsonDateTimeRenderingSpec`, `DateTimeRenderingSpec` and `DateTimeHelperRenderingSpec`. `MonthBindingSpec` and `DateTimeRoundTripBindingSpec` still pass. On the docs side, the `Month` rows in §76.2, the `Month` snippet in §76.4, and the `Month` entries in `defaultRenderers.adoc` and `jsonConfiguration.adoc` come out. The §76.4 enum note would then cover `Month` too: Grails renders every enum by `name()`, where Jackson 3 writes `Month` as its number. ########## grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/SqlTimeMarshaller.java: ########## @@ -0,0 +1,50 @@ +/* + * 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.sql.Time; + +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 {@link java.sql.Time} to its wall-clock time in the + * JVM default time zone ({@code HH:mm:ss}, from {@link Time#toString()}), the same as Spring Boot's + * default Jackson rendering. It is registered ahead of {@link DateMarshaller}, which would + * otherwise render the {@code Time} as a full date and time. + * + * @since 8.0 + */ +public class SqlTimeMarshaller implements ObjectMarshaller<JSON> { + + public boolean supports(Object object) { + return object instanceof Time; + } + + public void marshalObject(Object object, JSON converter) throws ConverterException { + try { + converter.getWriter().value(object.toString()); Review Comment: Confirmed: binding `"1970-01-01T08:48:46.000Z"` to a `java.sql.Time` property is a `typeMismatch` (I ran it), so the 7.x form never round-tripped. I'm fine keeping `toString()`. One precision point for §76.2 (line 4210): "binds back to the same `java.sql.Time`" holds only for a `Time` on 1970-01-01 with zero milliseconds, like the `Time.valueOf('01:48:46')` in the test. `new Time(1759909726407L)` (2025-10-08T07:48:46.407Z) binds back as that time of day on 1970-01-01, without the milliseconds, as with Jackson. "binds back to the same time of day, to the second" would be accurate. ########## grails-views-gson/src/main/groovy/grails/plugin/json/view/JsonViewGeneratorConfiguration.groovy: ########## @@ -29,9 +29,20 @@ class JsonViewGeneratorConfiguration { Boolean escapeUnicode = false + /** + * The {@link java.text.SimpleDateFormat} pattern for {@link Date} and {@link Calendar} values and map keys. In the + * default {@link #timeZone} it writes a UTC instant with millisecond precision, such as + * {@code 2024-06-15T14:30:45.123Z}, as Spring Boot does. + */ String dateFormat = /yyyy-MM-dd'T'HH:mm:ss.SSSX/ Review Comment: Restoring the 7.x default, which I asked for, also restores a 7.x bug. With a single `X`, `SimpleDateFormat` writes only the hours of the offset ("any fraction of an hour is ignored"). So with a configured `timeZone` whose offset is not a whole number of hours, the rendered instant is wrong. I rendered `2025-10-08T07:48:46.407Z` through a views generator in each zone and parsed the result back: | `timeZone` | written with `X` (this default) | parses back as | written with `XXX` | |---|---|---|---| | `Asia/Kolkata` | `2025-10-08T13:18:46.407+05` | `08:18:46.407Z`, 30 minutes late | `2025-10-08T13:18:46.407+05:30` | | `Asia/Kathmandu` | `2025-10-08T13:33:46.407+05` | `08:33:46.407Z`, 45 minutes late | `2025-10-08T13:33:46.407+05:45` | | `America/New_York` | `2025-10-08T03:48:46.407-04` | correct | `2025-10-08T03:48:46.407-04:00` | | `GMT` (default) | `2025-10-08T07:48:46.407Z` | correct | `2025-10-08T07:48:46.407Z` | Please default to `yyyy-MM-dd'T'HH:mm:ss.SSSXXX`. The default `GMT` output doesn't change, every zone parses back to the right instant, and the `XXX` column matches `JsonMapper` with the same default time zone character for character. Whole-hour zones change from `-04` to `-04:00`, which needs a §76.2 row. With that change locally, the only failing test is the `America/New_York` row of `DateTimeHelperRenderingSpec` ("a configured timeZone writes … as Grails 7 did"). Please also add a half-hour row there, such as `Asia/Kolkata`, since only whole-hour zones are covered. The `-04` form also appears in: - the Time Zone and Date Format sections of `jsonConfiguration.adoc`; - the `defaultValue` in `additional-spring-configuration-metadata.json`; - the last §76.4 note. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
