private void renderResource()

in grails-views-gson/src/main/groovy/grails/plugin/json/view/api/internal/DefaultJsonApiViewHelper.groovy [169:313]


    private void renderResource(Object object, Writer out, Map arguments, String basePath) {
        PersistentEntity entity = findEntity(object)

        if (entity == null) {
            throw new IllegalArgumentException("Rendering non persistent entities is not supported")
        }

        List<String> includes = getIncludes(arguments)
        List<String> excludes = getExcludes(arguments)
        boolean includeAssociations = includeAssociations(arguments)

        out.write(JsonOutput.OPEN_BRACE)

        writeKeyValue(out, 'type', entity.decapitalizedName)

        PersistentProperty identity = entity.identity
        String idName = identity?.name

        if(idName != null) {
            out.write(JsonOutput.COMMA)
            writeKeyValue(out, 'id', idGenerator.render(object, identity))
        }

        if (entity.persistentProperties) {
            List<PersistentProperty> attributes = getAttributes(entity)
            List<Association> relationships = getRelationships(entity)

            if (attributes) {
                out.write(JsonOutput.COMMA)
                out.write(generator.toJson("attributes"))
                out.write(JsonOutput.COLON)
                out.write(JsonOutput.OPEN_BRACE)

                boolean firstAttribute = true
                for (persistentProperty in attributes) {
                    if (!includeExcludeSupport.shouldInclude(includes, excludes, "${basePath}${persistentProperty.name}".toString())) continue

                    if (!firstAttribute) {
                        out.write(JsonOutput.COMMA)
                    }

                    out.write(generator.toJson(persistentProperty.name))
                    out.write(JsonOutput.COLON)

                    Object prop = ((GroovyObject) object).getProperty(persistentProperty.name)
                    if (persistentProperty instanceof Embedded) {
                        renderEmbeddedEntity(prop, (Association)persistentProperty, out, "${basePath}${persistentProperty.name}.".toString(), includes, excludes)
                    } else if (persistentProperty instanceof EmbeddedCollection && prop instanceof Iterable) {
                        out.write(JsonOutput.OPEN_BRACKET)
                        Iterator iterator = ((Iterable) prop).iterator()
                        while (iterator.hasNext()) {
                            def o = iterator.next()
                            renderEmbeddedEntity(o, (Association)persistentProperty, out, "${basePath}${persistentProperty.name}.".toString(), includes, excludes)
                            if (iterator.hasNext()) {
                                out.write(JsonOutput.COMMA)
                            }
                        }
                        out.write(JsonOutput.CLOSE_BRACKET)
                    } else {
                        out.write(generator.toJson(((GroovyObject) object).getProperty(persistentProperty.name)))
                    }

                    firstAttribute = false
                }
                out.write(JsonOutput.CLOSE_BRACE)
            }
            if (relationships && includeAssociations) {

                out.write(JsonOutput.COMMA)
                out.write(generator.toJson("relationships"))
                out.write(JsonOutput.COLON)
                out.write(JsonOutput.OPEN_BRACE)
                boolean firstRelationship = true

                for (association in relationships) {
                    if (!includeExcludeSupport.shouldInclude(includes, excludes, "${basePath}${association.name}".toString())) continue

                    def value = ((GroovyObject) object).getProperty(association.name)
                    if (!firstRelationship) {
                        out.write(JsonOutput.COMMA)
                    }
                    firstRelationship = false
                    out.write(generator.toJson(association.name))
                    out.write(JsonOutput.COLON)
                    out.write(JsonOutput.OPEN_BRACE)

                    if (association instanceof ToOne && value != null) {
                        renderRelationshipLinks(value).writeTo(out)
                        out.write(JsonOutput.COMMA)
                    }

                    out.write(generator.toJson("data"))
                    out.write(JsonOutput.COLON)
                    PersistentEntity associatedEntity = association.associatedEntity
                    if (association instanceof ToMany && Iterable.isAssignableFrom(association.type)) {
                        out.write(JsonOutput.OPEN_BRACKET)
                        if (value != null) {
                            Iterator iterator = ((Iterable) value).iterator()
                            String type = associatedEntity.decapitalizedName

                            while (iterator.hasNext()) {
                                def o = iterator.next()
                                out.write(JsonOutput.OPEN_BRACE)
                                writeKeyValue(out, 'type', type)
                                out.write(JsonOutput.COMMA)
                                writeKeyValue(out, 'id', idGenerator.render(o, associatedEntity.identity))
                                out.write(JsonOutput.CLOSE_BRACE)
                                if (iterator.hasNext()) {
                                    out.write(JsonOutput.COMMA)
                                }
                            }
                        }

                        out.write(JsonOutput.CLOSE_BRACKET)

                    } else {
                        if (value != null) {
                            out.write(JsonOutput.OPEN_BRACE)

                            out.write(generator.toJson("type"))
                            out.write(JsonOutput.COLON)
                            out.write(generator.toJson(associatedEntity.decapitalizedName))
                            out.write(JsonOutput.COMMA)

                            out.write(generator.toJson("id"))
                            out.write(JsonOutput.COLON)
                            out.write(generator.toJson(idGenerator.render(value, associatedEntity.identity)))

                            out.write(JsonOutput.CLOSE_BRACE)
                        } else {
                            NULL_OUTPUT.writeTo(out)
                        }
                    }
                    out.write(JsonOutput.CLOSE_BRACE)
                }
                out.write(JsonOutput.CLOSE_BRACE)
            }
        }

        if (basePath != "") {
            out.write(JsonOutput.COMMA)
            renderRelationshipLinks(object).writeTo(out)
        }
        out.write(JsonOutput.CLOSE_BRACE)
    }