private boolean doWriteObjectBody()

in johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java [344:436]


    private boolean doWriteObjectBody(final Object object, final Collection<String> ignored,
                                      final JsonPointerTracker jsonPointer, final JsonGenerator generator)
            throws IllegalAccessException, InvocationTargetException {
        final Class<?> objectClass = object.getClass();
        final Mappings.ClassMapping classMapping = mappings.findOrCreateClassMapping(objectClass);
        if (classMapping == null) {
            throw new MapperException("No mapping for " + objectClass.getName());
        }

        if (jsonPointers == null) {
            if (classMapping.deduplicateObjects || config.isDeduplicateObjects()) {
                jsonPointers = new HashMap<>();
                jsonPointers.putIfAbsent(object, jsonPointer == null ? "/" : jsonPointer.toString());
            } else {
                jsonPointers = emptyMap();
            }
        } else if (isDedup()) {
            jsonPointers.putIfAbsent(object, jsonPointer == null ? "/" : jsonPointer.toString());
        }

        if (classMapping.writer != null) {
            writeWithObjectConverter(new DynamicMappingGenerator.SkipEnclosingWriteEnd(this, null, generator), classMapping.writer, object);
            return false;
        }
        if (classMapping.adapter != null) {
            doWriteObjectBody(classMapping.adapter.from(object), ignored, jsonPointer, generator);
            return true;
        }

        if (classMapping.serializedPolymorphicProperties != null) {
            for (Map.Entry<String, String> polymorphicProperty : classMapping.serializedPolymorphicProperties) {
                generator.write(polymorphicProperty.getKey(), polymorphicProperty.getValue());
            }
        }

        for (final Map.Entry<String, Mappings.Getter> getterEntry : classMapping.getters.entrySet()) {
            final Mappings.Getter getter = getterEntry.getValue();
            if (ignored != null && ignored.contains(getterEntry.getKey())) {
                continue;
            }
            if (getter.version >= 0 && config.getVersion() >= 0 && config.getVersion() < getter.version) {
                continue;
            }

            final Object value = getter.reader.read(object);
            if (JsonValue.class.isInstance(value)) {
                generator.write(getterEntry.getKey(), JsonValue.class.cast(value));
                continue;
            }

            if (value == null) {
                if (!getter.reader.isNillable(!config.isSkipNull())) {
                    continue;
                } else {
                    generator.writeNull(getterEntry.getKey());
                    continue;
                }
            }

            final Object val = getter.converter == null ? value : getter.converter.from(value);

            String valJsonPointer = jsonPointers.get(val);
            if (valJsonPointer != null) {
                // write the JsonPointer instead
                generator.write(getterEntry.getKey(), valJsonPointer);
            } else {
                writeValue(val.getClass(),
                        getter.dynamic,
                        getter.primitive,
                        getter.array,
                        getter.collection,
                        getter.map,
                        getter.itemConverter,
                        getterEntry.getKey(),
                        val,
                        getter.objectConverter,
                        getter.ignoreNested,
                        isDedup() ? new JsonPointerTracker(jsonPointer, getterEntry.getKey()) : null,
                        generator);
            }
        }

        // @JohnzonAny doesn't respect comparator since it is a map and not purely in the model we append it after and
        // sorting is up to the user for this part (TreeMap if desired)
        if (classMapping.anyGetter != null) {
            final Map<String, Object> any = Map.class.cast(classMapping.anyGetter.reader.read(object));
            if (any != null) {
                writeMapBody(any, null);
            }
        }

        return true;
    }