codeconsole commented on code in PR #16414:
URL: https://github.com/apache/grails-core/pull/16414#discussion_r4221306887


##########
grails-converters/src/main/groovy/org/grails/web/converters/jackson/DomainClassSerializer.java:
##########
@@ -0,0 +1,441 @@
+/*
+ *  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.jackson;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import tools.jackson.core.JsonGenerator;
+import tools.jackson.core.JsonToken;
+import tools.jackson.core.type.WritableTypeId;
+import tools.jackson.databind.JacksonSerializable;
+import tools.jackson.databind.SerializationContext;
+import tools.jackson.databind.ValueSerializer;
+import tools.jackson.databind.introspect.BeanPropertyDefinition;
+import tools.jackson.databind.jsontype.TypeSerializer;
+import tools.jackson.databind.ser.std.StdSerializer;
+import tools.jackson.databind.util.NameTransformer;
+
+import org.springframework.beans.BeanWrapper;
+import org.springframework.beans.BeanWrapperImpl;
+
+import org.grails.core.exceptions.GrailsConfigurationException;
+import org.grails.datastore.mapping.model.PersistentEntity;
+import org.grails.datastore.mapping.model.PersistentProperty;
+import org.grails.datastore.mapping.model.config.GormProperties;
+import org.grails.datastore.mapping.model.types.Association;
+import org.grails.datastore.mapping.model.types.ManyToOne;
+import org.grails.datastore.mapping.model.types.OneToOne;
+import org.grails.datastore.mapping.reflect.NameUtils;
+import org.grails.datastore.mapping.reflect.ReflectionUtils;
+
+/**
+ * Serializes domain class instances as {@code grails.converters.JSON} renders 
them: an object of the id, the version
+ * and the class name when they are included, and the persistent properties. 
An associated domain class instance is a
+ * reference of its id, such as {@code {"id":1}}, or, when rendering deep, the 
instance in full. Embedded instances and
+ * collections of basic values are written in full.
+ *
+ * <p>Property values are written with {@link 
JsonGenerator#writePOJO(Object)}, so a mapper writes them as it writes any
+ * other value, and a converter renders them with its marshallers. When 
rendering deep, an instance that is already
+ * being written, further up, is written as a reference.
+ *
+ * <p>A property annotated with {@code @JsonIgnore}, or with {@code 
@JsonProperty(access = WRITE_ONLY)}, on its field
+ * or getter, is not written. The {@link DomainClassRendering} decides about 
the others.
+ *
+ * <p>Written by a mapper, rather than by the JSON converter, an instance's 
property names are those Jackson gives the
+ * class's properties, so the mapper's naming strategy, {@code @JsonNaming} 
and {@code @JsonProperty} names apply. The
+ * properties are written in the converter's order, whether or not the mapper 
sorts properties. The serializer writes
+ * the type id of a class with {@code @JsonTypeInfo}, and can write an 
instance's properties into an enclosing object,
+ * for a property annotated with {@code @JsonUnwrapped}.
+ *
+ * @since 9.0
+ */
+public final class DomainClassSerializer extends StdSerializer<Object> {
+
+    private static final String RENDERING_INSTANCES = 
DomainClassSerializer.class.getName() + ".instances";
+
+    private static final ClassValue<Set<String>> IGNORED_PROPERTIES = new 
ClassValue<>() {
+        @Override
+        protected Set<String> computeValue(Class<?> type) {
+            Set<String> ignored = new HashSet<>();
+            for (Class<?> current = type; current != null && current != 
Object.class; current = current.getSuperclass()) {
+                for (Field field : current.getDeclaredFields()) {
+                    if (ignoredForSerialization(field)) {
+                        ignored.add(field.getName());
+                    }
+                }
+                for (Method method : current.getDeclaredMethods()) {
+                    if (ReflectionUtils.isGetter(method.getName(), 
method.getParameterTypes()) &&
+                            ignoredForSerialization(method)) {
+                        
ignored.add(NameUtils.getPropertyNameForGetterOrSetter(method.getName()));
+                    }
+                }
+            }
+            return Set.copyOf(ignored);
+        }
+    };
+
+    private final DomainClassRendering rendering;
+
+    private final NameTransformer unwrapper;
+
+    private final Map<Class<?>, Names> namesByType = new ConcurrentHashMap<>();
+
+    /**
+     * @param rendering how to render domain class instances
+     */
+    public DomainClassSerializer(DomainClassRendering rendering) {
+        this(rendering, null);
+    }
+
+    private DomainClassSerializer(DomainClassRendering rendering, 
NameTransformer unwrapper) {
+        super(Object.class);
+        this.rendering = rendering;
+        this.unwrapper = unwrapper;
+    }
+
+    /**
+     * @return how this serializer renders domain class instances
+     */
+    public DomainClassRendering getRendering() {
+        return rendering;
+    }
+
+    /**
+     * A value that a mapper writes as a domain class instance rendered in a 
particular way, whether or not the mapper
+     * has a {@link DomainClassJacksonModule}.
+     *
+     * @param object a domain class instance
+     * @param rendering how to render it
+     * @return the value to write
+     */
+    public static Object value(Object object, DomainClassRendering rendering) {
+        return new JacksonSerializable.Base() {
+            @Override
+            public void serialize(JsonGenerator generator, 
SerializationContext context) {
+                write(object, generator, context, rendering);
+            }
+
+            @Override
+            public void serializeWithType(JsonGenerator generator, 
SerializationContext context,
+                    TypeSerializer typeSerializer) {
+                serialize(generator, context);
+            }
+        };
+    }
+
+    @Override
+    public ValueSerializer<Object> unwrappingSerializer(NameTransformer 
nameTransformer) {
+        return new DomainClassSerializer(rendering,
+                unwrapper == null ? nameTransformer : 
NameTransformer.chainedTransformer(nameTransformer, unwrapper));
+    }
+
+    @Override
+    public boolean isUnwrappingSerializer() {
+        return unwrapper != null;
+    }
+
+    @Override
+    public void serialize(Object value, JsonGenerator gen, 
SerializationContext ctxt) {
+        Object object = rendering.unwrap(value);
+        boolean wrapped = unwrapper == null;
+        if (wrapped) {
+            gen.writeStartObject(object);
+        }
+        writeContents(object, gen, ctxt);
+        if (wrapped) {
+            gen.writeEndObject();
+        }
+    }
+
+    @Override
+    public void serializeWithType(Object value, JsonGenerator gen, 
SerializationContext ctxt, TypeSerializer typeSer) {
+        if (unwrapper != null) {
+            serialize(value, gen, ctxt);
+            return;
+        }
+        Object object = rendering.unwrap(value);
+        WritableTypeId typeId = typeSer.writeTypePrefix(gen, ctxt, 
typeSer.typeId(object, JsonToken.START_OBJECT));
+        writeContents(object, gen, ctxt);
+        typeSer.writeTypeSuffix(gen, ctxt, typeId);
+    }
+
+    /**
+     * Writes an instance's properties, or a reference to it when it is 
already being written further up.
+     */
+    private void writeContents(Object object, JsonGenerator gen, 
SerializationContext ctxt) {
+        Names names = names(object.getClass(), ctxt);
+        Set<Object> instances = renderingInstances(ctxt);
+        if (!instances.add(object)) {
+            writeReferenceProperties(object, entity(object, rendering), gen, 
rendering, names);

Review Comment:
   Fixed in 4a20e8dff3. `serialize` and `serializeWithType` now detect the 
cycle before anything is written, and a cycle reference goes through 
`writeReference(...)`. So `rendering.writeReference(...)` decides first, as it 
does for associations. On the typed path it's asked before the type prefix is 
written.
   
   The spec renders your deep `Partner` cycle with a rendering whose 
`writeReference` records what it's offered, and checks that the hook gets the 
cycle reference.
   
   Inside a `@JsonUnwrapped` property, a cycle still writes the reference's 
properties directly. The hook writes a whole value, and there's no place for 
one in the enclosing object.



##########
grails-converters/src/main/groovy/org/grails/web/converters/jackson/DomainClassSerializer.java:
##########
@@ -0,0 +1,441 @@
+/*
+ *  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.jackson;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import tools.jackson.core.JsonGenerator;
+import tools.jackson.core.JsonToken;
+import tools.jackson.core.type.WritableTypeId;
+import tools.jackson.databind.JacksonSerializable;
+import tools.jackson.databind.SerializationContext;
+import tools.jackson.databind.ValueSerializer;
+import tools.jackson.databind.introspect.BeanPropertyDefinition;
+import tools.jackson.databind.jsontype.TypeSerializer;
+import tools.jackson.databind.ser.std.StdSerializer;
+import tools.jackson.databind.util.NameTransformer;
+
+import org.springframework.beans.BeanWrapper;
+import org.springframework.beans.BeanWrapperImpl;
+
+import org.grails.core.exceptions.GrailsConfigurationException;
+import org.grails.datastore.mapping.model.PersistentEntity;
+import org.grails.datastore.mapping.model.PersistentProperty;
+import org.grails.datastore.mapping.model.config.GormProperties;
+import org.grails.datastore.mapping.model.types.Association;
+import org.grails.datastore.mapping.model.types.ManyToOne;
+import org.grails.datastore.mapping.model.types.OneToOne;
+import org.grails.datastore.mapping.reflect.NameUtils;
+import org.grails.datastore.mapping.reflect.ReflectionUtils;
+
+/**
+ * Serializes domain class instances as {@code grails.converters.JSON} renders 
them: an object of the id, the version
+ * and the class name when they are included, and the persistent properties. 
An associated domain class instance is a
+ * reference of its id, such as {@code {"id":1}}, or, when rendering deep, the 
instance in full. Embedded instances and
+ * collections of basic values are written in full.
+ *
+ * <p>Property values are written with {@link 
JsonGenerator#writePOJO(Object)}, so a mapper writes them as it writes any
+ * other value, and a converter renders them with its marshallers. When 
rendering deep, an instance that is already
+ * being written, further up, is written as a reference.
+ *
+ * <p>A property annotated with {@code @JsonIgnore}, or with {@code 
@JsonProperty(access = WRITE_ONLY)}, on its field
+ * or getter, is not written. The {@link DomainClassRendering} decides about 
the others.
+ *
+ * <p>Written by a mapper, rather than by the JSON converter, an instance's 
property names are those Jackson gives the
+ * class's properties, so the mapper's naming strategy, {@code @JsonNaming} 
and {@code @JsonProperty} names apply. The
+ * properties are written in the converter's order, whether or not the mapper 
sorts properties. The serializer writes
+ * the type id of a class with {@code @JsonTypeInfo}, and can write an 
instance's properties into an enclosing object,
+ * for a property annotated with {@code @JsonUnwrapped}.
+ *
+ * @since 9.0
+ */
+public final class DomainClassSerializer extends StdSerializer<Object> {
+
+    private static final String RENDERING_INSTANCES = 
DomainClassSerializer.class.getName() + ".instances";
+
+    private static final ClassValue<Set<String>> IGNORED_PROPERTIES = new 
ClassValue<>() {
+        @Override
+        protected Set<String> computeValue(Class<?> type) {
+            Set<String> ignored = new HashSet<>();
+            for (Class<?> current = type; current != null && current != 
Object.class; current = current.getSuperclass()) {
+                for (Field field : current.getDeclaredFields()) {
+                    if (ignoredForSerialization(field)) {
+                        ignored.add(field.getName());
+                    }
+                }
+                for (Method method : current.getDeclaredMethods()) {
+                    if (ReflectionUtils.isGetter(method.getName(), 
method.getParameterTypes()) &&
+                            ignoredForSerialization(method)) {
+                        
ignored.add(NameUtils.getPropertyNameForGetterOrSetter(method.getName()));
+                    }
+                }
+            }
+            return Set.copyOf(ignored);
+        }
+    };
+
+    private final DomainClassRendering rendering;
+
+    private final NameTransformer unwrapper;
+
+    private final Map<Class<?>, Names> namesByType = new ConcurrentHashMap<>();
+
+    /**
+     * @param rendering how to render domain class instances
+     */
+    public DomainClassSerializer(DomainClassRendering rendering) {
+        this(rendering, null);
+    }
+
+    private DomainClassSerializer(DomainClassRendering rendering, 
NameTransformer unwrapper) {
+        super(Object.class);
+        this.rendering = rendering;
+        this.unwrapper = unwrapper;
+    }
+
+    /**
+     * @return how this serializer renders domain class instances
+     */
+    public DomainClassRendering getRendering() {
+        return rendering;
+    }
+
+    /**
+     * A value that a mapper writes as a domain class instance rendered in a 
particular way, whether or not the mapper
+     * has a {@link DomainClassJacksonModule}.
+     *
+     * @param object a domain class instance
+     * @param rendering how to render it
+     * @return the value to write
+     */
+    public static Object value(Object object, DomainClassRendering rendering) {
+        return new JacksonSerializable.Base() {
+            @Override
+            public void serialize(JsonGenerator generator, 
SerializationContext context) {
+                write(object, generator, context, rendering);
+            }
+
+            @Override
+            public void serializeWithType(JsonGenerator generator, 
SerializationContext context,
+                    TypeSerializer typeSerializer) {
+                serialize(generator, context);
+            }
+        };
+    }
+
+    @Override
+    public ValueSerializer<Object> unwrappingSerializer(NameTransformer 
nameTransformer) {
+        return new DomainClassSerializer(rendering,
+                unwrapper == null ? nameTransformer : 
NameTransformer.chainedTransformer(nameTransformer, unwrapper));
+    }
+
+    @Override
+    public boolean isUnwrappingSerializer() {
+        return unwrapper != null;
+    }
+
+    @Override
+    public void serialize(Object value, JsonGenerator gen, 
SerializationContext ctxt) {
+        Object object = rendering.unwrap(value);
+        boolean wrapped = unwrapper == null;
+        if (wrapped) {
+            gen.writeStartObject(object);
+        }
+        writeContents(object, gen, ctxt);
+        if (wrapped) {
+            gen.writeEndObject();
+        }
+    }
+
+    @Override
+    public void serializeWithType(Object value, JsonGenerator gen, 
SerializationContext ctxt, TypeSerializer typeSer) {
+        if (unwrapper != null) {

Review Comment:
   Fixed in 4a20e8dff3. With `FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS` on, which is 
the default, the unwrapping variant's `serializeWithType` now calls 
`reportBadDefinition` with `UnwrappingBeanSerializer`'s message. With the 
feature disabled, it writes the properties without the type id. The spec covers 
both, with `@JsonUnwrapped Dog dog`.



##########
grails-converters/src/main/groovy/org/grails/web/converters/configuration/ConvertersConfigurationInitializer.java:
##########
@@ -111,26 +132,75 @@ private void initJSONConfiguration() {
 
         List<ObjectMarshaller<JSON>> marshallers = new ArrayList<>();
         marshallers.addAll(getPreviouslyConfiguredMarshallers(JSON.class));
-        marshallers.add(new 
org.grails.web.converters.marshaller.json.ArrayMarshaller());
-        marshallers.add(new 
org.grails.web.converters.marshaller.json.ByteArrayMarshaller());
-        marshallers.add(new 
org.grails.web.converters.marshaller.json.CollectionMarshaller());
-        marshallers.add(new 
org.grails.web.converters.marshaller.json.MapMarshaller());
-        marshallers.add(new 
org.grails.web.converters.marshaller.json.SimpleEnumMarshaller());
 
         Config grailsConfig = getGrailsConfig();
+        ProxyHandler proxyHandler = getProxyHandler();
+        boolean legacy = 
grailsConfig.getProperty(SETTING_CONVERTERS_JSON_LEGACY, Boolean.class, false);
+        ConvertersConfigurationHolder.setLegacyJson(legacy);
+        if (legacy) {
+            LOG.warn(SETTING_CONVERTERS_JSON_LEGACY + " renders JSON as Grails 
8 did. It is deprecated, and will be removed in Grails 10.");
+            addLegacyJsonMarshallers(marshallers, grailsConfig, proxyHandler);
+        }
+        else {
+            addJsonMarshallers(marshallers, grailsConfig, proxyHandler);
+        }
 
-        marshallers.add(new 
org.grails.web.converters.marshaller.ProxyUnwrappingMarshaller<>());
+        DefaultConverterConfiguration<JSON> cfg = new 
DefaultConverterConfiguration<>(marshallers, proxyHandler);
+        cfg.setEncoding(grailsConfig.getProperty(SETTING_CONVERTERS_ENCODING, 
"UTF-8"));
+        String defaultCirRefBehaviour = 
grailsConfig.getProperty(SETTING_CONVERTERS_CIRCULAR_REFERENCE_BEHAVIOUR, 
"DEFAULT");
+        
cfg.setCircularReferenceBehaviour(Converter.CircularReferenceBehaviour.valueOf(
+                
grailsConfig.getProperty(SETTING_CONVERTERS_JSON_CIRCULAR_REFERENCE_BEHAVIOUR, 
String.class,
+                      defaultCirRefBehaviour, 
Converter.CircularReferenceBehaviour.allowedValues())));
+
+        Boolean defaultPrettyPrint = 
grailsConfig.getProperty(SETTING_CONVERTERS_PRETTY_PRINT, Boolean.class, false);
+        Boolean prettyPrint = 
grailsConfig.getProperty(SETTING_CONVERTERS_JSON_PRETTY_PRINT, Boolean.class, 
defaultPrettyPrint);
+        cfg.setPrettyPrint(prettyPrint);
+        
cfg.setCacheObjectMarshallerByClass(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_CACHE_OBJECTS,
 Boolean.class, true));
+
+        registerObjectMarshallersFromApplicationContext(cfg, JSON.class);
 
+        ConvertersConfigurationHolder.setDefaultConfiguration(JSON.class, new 
ChainedConverterConfiguration<>(cfg, proxyHandler));
+    }
+
+    private void addJsonMarshallers(List<ObjectMarshaller<JSON>> marshallers, 
Config grailsConfig, ProxyHandler proxyHandler) {
         if 
("javascript".equals(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_DATE, 
String.class, "default", Arrays.asList("javascript", "default")))) {
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Using Javascript JSON Date Marshaller.");
             }
             marshallers.add(new 
org.grails.web.converters.marshaller.json.JavascriptDateMarshaller());
         }
+        // domain classes, and proxies of them, are claimed first: the 
application's JsonMapper may have a serializer for
+        // them, and they are rendered with the converter's includes, excludes 
and deep setting
+        marshallers.add(new 
org.grails.web.converters.marshaller.ProxyUnwrappingMarshaller<>());
+        marshallers.add(domainClassMarshaller(grailsConfig, proxyHandler));
+        // dates and times and the other single values the application's 
JsonMapper has a serializer for are written
+        // by the mapper, as Spring Boot writes them
+        marshallers.add(new 
org.grails.web.converters.marshaller.json.JsonMapperValueMarshaller());

Review Comment:
   Fixed in 26f1dda9ff. `SimpleEnumMarshaller` has a new 
`SimpleEnumMarshaller(boolean constantsWithBodies)` constructor:
   - The new mode registers `new SimpleEnumMarshaller(true)`, whose `supports` 
is `object instanceof Enum`.
   - Legacy keeps `new SimpleEnumMarshaller()` and `isEnum()`, so a body 
constant still renders as a bean there, as in 8.0.x.
   
   The spec renders a body constant with a `@JsonValue` (`"ACTIVE"`) and one 
without (`"CIRCLE"`). The guide's table has a row for body constants.



##########
grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java:
##########
@@ -297,65 +322,194 @@ public JSONWriter value(double d) {
      * @return this
      */
     public JSONWriter value(long l) {
-        return append(Long.toString(l));
+        return write(() -> generator.writeNumber(l), l);
     }
 
     /**
-     * Append a number value
+     * Append a number value.
      *
-     * @param number
-     * @return
+     * @param number A Number.
+     * @return this
      */
     public JSONWriter value(Number number) {
-        return number != null ? append(number.toString()) : valueNull();
+        return number != null ? write(() -> writeNumber(number), number) : 
valueNull();
     }
 
+    /**
+     * Append the value <code>null</code>.
+     *
+     * @return this
+     */
     public JSONWriter valueNull() {
-        return append(nullWritable);
+        return write(generator::writeNull, null);
     }
 
-    static Writable nullWritable = new NullWritable();
-
-    private static class NullWritable implements Writable {
-        @Override
-        public Writer writeTo(Writer out) throws IOException {
-            out.write("null");
-            return out;
+    /**
+     * Append an object value: a {@link JsonMapperValue} as its mapper writes 
it, {@code null} (and
+     * {@link JSONObject#NULL}), numbers and booleans as JSON literals, a 
{@code Date} as a JavaScript
+     * {@code new Date(...)} (for the javascript JSON date format), a {@link 
JSONElement} as the JSON text it writes, a
+     * {@code String} as a JSON string, and any other value as the quoted, 
JSON encoded text of {@link JSONObject}.
+     *
+     * @param o The object to append.
+     * @return this
+     */
+    public JSONWriter value(Object o) {
+        if (o == null || o.equals(null)) {
+            return valueNull();
+        }
+        if (o instanceof JsonMapperValue mapperValue && 
mapperValue.getJsonMapper() == jsonMapper) {
+            return write(() -> jsonMapper.writeValue(generator, 
mapperValue.getValue(), mapperValue.getNestedValueWriter()), o);
+        }
+        if (o instanceof Number number) {
+            return value(number);
+        }
+        if (o instanceof Boolean b) {
+            return value(b.booleanValue());
+        }
+        if (o instanceof Date date) {
+            return write(() -> generator.writeRawValue("new Date(" + 
date.getTime() + ")"), o);
+        }
+        if (o instanceof JSONElement element) {
+            return write(() -> generator.writeRawValue(text(element)), o);
+        }
+        if (o.getClass() == String.class || o.getClass() == 
StringBuilder.class || o.getClass() == StringBuffer.class) {
+            return write(() -> generator.writeString(o.toString()), o);
         }
+        return write(() -> generator.writeRawValue(quoted(o)), o);
     }
 
     /**
-     * Append an object value.
+     * Writes a value nested in a {@link JsonMapperValue} that this writer is 
writing, such as a value that a Jackson
+     * serializer writes with {@link JsonGenerator#writePOJO(Object)}, from 
the value's nested value writer. The writer
+     * accepts one value, as at the start of a JSON text, and then returns to 
the state it was in.
      *
-     * @param o The object to append. It can be null, or a Boolean, Number,
-     *          String, JSONObject, or JSONArray.
-     * @return this
+     * @param writeValue writes one value to this writer
+     * @throws JSONException if {@code writeValue} does not write one complete 
value
+     * @since 9.0
      */
-    public JSONWriter value(Object o) {
-        return o != null ? append(new QuotedWritable(o)) : valueNull();
+    public void writeNested(Runnable writeValue) {
+        Mode outerMode = this.mode;
+        Stack<Mode> outerStack = this.stack;
+        boolean outerComma = this.comma;
+        this.mode = INIT;
+        this.stack = new Stack<>();
+        this.nesting++;
+        try {
+            writeValue.run();
+            if (this.mode != DONE) {
+                throw new JSONException("Incomplete nested value: expected 
mode of DONE but was " + this.mode);
+            }
+        }
+        finally {
+            this.mode = outerMode;
+            this.stack = outerStack;
+            this.comma = outerComma;
+            this.nesting--;
+        }
     }
 
-    private static class QuotedWritable implements Writable {
-        Object o;
+    /**
+     * The location of the nested value that {@link #writeNested(Runnable)} is 
about to write, relative to the
+     * {@link JsonMapperValue} it is nested in, such as {@code .inner} or 
{@code [0]}.
+     */
+    String nestedPath() {
+        return generator != null ? NestedValueSerializer.nestedPath(generator) 
: "";
+    }
 
-        QuotedWritable(Object o) {
-            this.o = o;
+    /**
+     * Writes the JSON text buffered so far to the {@link Writer}, without 
flushing the {@code Writer}.
+     *
+     * @since 9.0
+     */
+    public void flush() {
+        if (generator != null) {
+            generate(generator::flush);
         }
+    }
 
-        @Override
-        public Writer writeTo(Writer out) throws IOException {
-            JSONObject.writeValue(out, o);
-            return out;
+    private JSONWriter write(Runnable writeValue, Object value) {
+        if (this.mode == INIT) {
+            generate(writeValue);
+            this.mode = DONE;
+            closeIfDone();
+            return this;
         }
-
-        public String toString() {
-            return String.valueOf(o);
+        if (this.mode == OBJECT || this.mode == ARRAY) {
+            generate(writeValue);
+            if (this.mode == OBJECT) {
+                this.mode = KEY;
+            }
+            this.comma = true;
+            return this;
         }
+        throw new JSONException("Value out of sequence: expected mode to be 
OBJECT or ARRAY when writing '" + value + "' but was " + this.mode);
     }
 
     /**
-     * Enumeration of the possible modes of the JSONWriter
+     * Once the JSON text is complete, closes the generator, which writes the 
rest of the text to the {@link Writer}
+     * and returns its buffers to Jackson.
      */
+    private void closeIfDone() {
+        if (this.mode == DONE && this.nesting == 0) {
+            generate(generator::close);
+        }
+    }
+
+    private void writeNumber(Number number) {
+        if (number instanceof Integer || number instanceof Short || number 
instanceof Byte) {
+            generator.writeNumber(number.intValue());
+        }
+        else if (number instanceof Long) {
+            generator.writeNumber(number.longValue());
+        }
+        else if (number instanceof Double) {
+            generator.writeNumber(number.doubleValue());
+        }
+        else if (number instanceof Float) {
+            generator.writeNumber(number.floatValue());
+        }
+        else if (number instanceof BigDecimal bigDecimal) {
+            generator.writeNumber(bigDecimal);
+        }
+        else if (number instanceof BigInteger bigInteger) {
+            generator.writeNumber(bigInteger);
+        }
+        else {
+            jsonMapper.writeValue(generator, number);

Review Comment:
   Fixed in 26f1dda9ff, as you suggested:
   - In the new mode, `JSON.value` writes only `Integer`, `Long`, `Short`, 
`Byte`, `Double`, `Float`, `BigDecimal` and `BigInteger` directly.
   - Any other `Number`, the atomics included, goes through marshaller lookup, 
so `JsonMapperValueMarshaller` writes it with the mapper.
   - Legacy still sends every `Number` to `writer.value(Number)`, as 8.0.x did.
   - The `String.valueOf` fallback stays for `writer.value(Number)` called 
directly, so the `AtomicInteger` test still doesn't loop.
   
   The spec's `Money` with a module serializer renders `"$5"` as a map value, 
`"$6"` inside a value a module serializer writes, and `"$7"` as a single value.



##########
grails-converters/src/main/groovy/org/grails/web/converters/jackson/DomainClassSerializer.java:
##########
@@ -0,0 +1,321 @@
+/*
+ *  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.jackson;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import tools.jackson.core.JsonGenerator;
+import tools.jackson.databind.JacksonSerializable;
+import tools.jackson.databind.SerializationContext;
+import tools.jackson.databind.jsontype.TypeSerializer;
+import tools.jackson.databind.ser.std.StdSerializer;
+
+import org.springframework.beans.BeanWrapper;
+import org.springframework.beans.BeanWrapperImpl;
+
+import org.grails.core.exceptions.GrailsConfigurationException;
+import org.grails.datastore.mapping.model.PersistentEntity;
+import org.grails.datastore.mapping.model.PersistentProperty;
+import org.grails.datastore.mapping.model.config.GormProperties;
+import org.grails.datastore.mapping.model.types.Association;
+import org.grails.datastore.mapping.model.types.ManyToOne;
+import org.grails.datastore.mapping.model.types.OneToOne;
+import org.grails.datastore.mapping.reflect.NameUtils;
+import org.grails.datastore.mapping.reflect.ReflectionUtils;
+
+/**
+ * Serializes domain class instances as {@code grails.converters.JSON} renders 
them: an object of the id, the version
+ * and the class name when they are included, and the persistent properties. 
An associated domain class instance is a
+ * reference of its id, such as {@code {"id":1}}, or, when rendering deep, the 
instance in full. Embedded instances and
+ * collections of basic values are written in full.
+ *
+ * <p>Property values are written with {@link 
JsonGenerator#writePOJO(Object)}, so a mapper writes them as it writes any
+ * other value, and a converter renders them with its marshallers. When 
rendering deep, an instance that is already
+ * being written, further up, is written as a reference.
+ *
+ * <p>A property annotated with {@code @JsonIgnore}, or with {@code 
@JsonProperty(access = WRITE_ONLY)}, on its field
+ * or getter, is not written. The {@link DomainClassRendering} decides about 
the others.
+ *
+ * @since 9.0
+ */
+public final class DomainClassSerializer extends StdSerializer<Object> {
+
+    private static final String RENDERING_INSTANCES = 
DomainClassSerializer.class.getName() + ".instances";
+
+    private static final ClassValue<Set<String>> IGNORED_PROPERTIES = new 
ClassValue<>() {
+        @Override
+        protected Set<String> computeValue(Class<?> type) {
+            Set<String> ignored = new HashSet<>();
+            for (Class<?> current = type; current != null && current != 
Object.class; current = current.getSuperclass()) {
+                for (Field field : current.getDeclaredFields()) {
+                    if (ignoredForSerialization(field)) {
+                        ignored.add(field.getName());
+                    }
+                }
+                for (Method method : current.getDeclaredMethods()) {
+                    if (ReflectionUtils.isGetter(method.getName(), 
method.getParameterTypes()) &&
+                            ignoredForSerialization(method)) {
+                        
ignored.add(NameUtils.getPropertyNameForGetterOrSetter(method.getName()));
+                    }
+                }
+            }
+            return Set.copyOf(ignored);
+        }
+    };
+
+    private final DomainClassRendering rendering;
+
+    /**
+     * @param rendering how to render domain class instances
+     */
+    public DomainClassSerializer(DomainClassRendering rendering) {
+        super(Object.class);
+        this.rendering = rendering;
+    }
+
+    /**
+     * @return how this serializer renders domain class instances
+     */
+    public DomainClassRendering getRendering() {
+        return rendering;
+    }
+
+    /**
+     * A value that a mapper writes as a domain class instance rendered in a 
particular way, whether or not the mapper
+     * has a {@link DomainClassJacksonModule}.
+     *
+     * @param object a domain class instance
+     * @param rendering how to render it
+     * @return the value to write
+     */
+    public static Object value(Object object, DomainClassRendering rendering) {
+        return new JacksonSerializable.Base() {
+            @Override
+            public void serialize(JsonGenerator generator, 
SerializationContext context) {
+                write(object, generator, context, rendering);
+            }
+
+            @Override
+            public void serializeWithType(JsonGenerator generator, 
SerializationContext context,
+                    TypeSerializer typeSerializer) {
+                serialize(generator, context);
+            }
+        };
+    }
+
+    @Override
+    public void serialize(Object value, JsonGenerator gen, 
SerializationContext ctxt) {
+        Object object = rendering.unwrap(value);
+        Set<Object> instances = renderingInstances(ctxt);
+        if (!instances.add(object)) {
+            writeReference(object, entity(object, rendering), gen, rendering);
+            return;
+        }
+        try {
+            write(object, gen, ctxt, rendering);
+        }
+        finally {
+            instances.remove(object);
+        }
+    }
+
+    private static void write(Object value, JsonGenerator gen, 
SerializationContext ctxt,
+            DomainClassRendering rendering) {
+        Object object = rendering.unwrap(value);
+        PersistentEntity entity = entity(object, rendering);
+        Set<String> ignored = IGNORED_PROPERTIES.get(object.getClass());
+        gen.writeStartObject(object);
+        if (rendering.isIncludeClass() && rendering.includes(object, "class")) 
{
+            gen.writeStringProperty("class", object.getClass().getName());
+        }
+        // a composite key has no identity, and is not written
+        PersistentProperty identity = entity.getIdentity();
+        if (identity != null && !ignored.contains(identity.getName()) && 
rendering.includes(object, identity.getName())) {
+            Object id = rendering.propertyValue(object, identity);
+            if (id != null) {
+                gen.writeName(identity.getName());
+                gen.writePOJO(id);
+            }
+        }
+        if (rendering.isIncludeVersion() && 
!ignored.contains(GormProperties.VERSION) &&
+                rendering.includes(object, GormProperties.VERSION)) {
+            Object version = rendering.propertyValue(object, 
entity.getVersion());
+            if (version != null) {
+                gen.writeName(GormProperties.VERSION);
+                gen.writePOJO(version);
+            }
+        }
+        BeanWrapper bean = new BeanWrapperImpl(object);
+        for (PersistentProperty property : entity.getPersistentProperties()) {
+            if (property.equals(entity.getVersion()) || 
ignored.contains(property.getName()) ||
+                    !rendering.includes(object, property.getName())) {
+                continue;
+            }
+            gen.writeName(property.getName());

Review Comment:
   Fixed in 4a20e8dff3, with the referenced entity's mapped id name. On the 
mapper, a reference is named as the referenced class names its properties, on 
both the association and the cycle path. Under `UPPER_CAMEL_CASE`, both write 
`{"Id":…}`, and since the id goes through the same name map, 
`@JsonProperty('key')` on the id names it `key` in references too. The 
converter path still writes `"id"`.
   
   The spec covers `UPPER_CAMEL_CASE` through a to-one association, a list, a 
map association and a deep cycle.



-- 
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]

Reply via email to