List<String> excludes = json.getExcludes(clazz);
List<String> includes = json.getIncludes(clazz);
IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
GrailsDomainClass domainClass = (GrailsDomainClass)application.getArtefact(
DomainClassArtefactHandler.TYPE, ConverterUtil.trimProxySuffix(clazz.getName()));
BeanWrapper beanWrapper = new BeanWrapperImpl(value);
writer.object();
if(shouldInclude(includeExcludeSupport, includes, excludes, value, "class")) {
writer.key("class").value(domainClass.getClazz().getName());
}
GrailsDomainClassProperty id = domainClass.getIdentifier();
if(shouldInclude(includeExcludeSupport, includes, excludes, value, id.getName())) {
Object idValue = extractValue(value, id);
json.property(GrailsDomainClassProperty.IDENTITY, idValue);
}
if (shouldInclude(includeExcludeSupport, includes, excludes, value, GrailsDomainClassProperty.VERSION) && isIncludeVersion()) {
GrailsDomainClassProperty versionProperty = domainClass.getVersion();
Object version = extractValue(value, versionProperty);
json.property(GrailsDomainClassProperty.VERSION, version);
}
GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();
for (GrailsDomainClassProperty property : properties) {
if(!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName())) continue;
writer.key(property.getName());
if (!property.isAssociation()) {
// Write non-relation property
Object val = beanWrapper.getPropertyValue(property.getName());
json.convertAnother(val);
}
else {
Object referenceObject = beanWrapper.getPropertyValue(property.getName());
if (isRenderDomainClassRelations()) {
if (referenceObject == null) {
writer.valueNull();
}
else {
referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
if (referenceObject instanceof SortedMap) {
referenceObject = new TreeMap((SortedMap) referenceObject);
}
else if (referenceObject instanceof SortedSet) {
referenceObject = new TreeSet((SortedSet) referenceObject);
}
else if (referenceObject instanceof Set) {
referenceObject = new HashSet((Set) referenceObject);
}
else if (referenceObject instanceof Map) {
referenceObject = new HashMap((Map) referenceObject);
}
else if (referenceObject instanceof Collection) {
referenceObject = new ArrayList((Collection) referenceObject);
}
json.convertAnother(referenceObject);
}
}
else {
if (referenceObject == null) {
json.value(null);
}
else {
GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass();
// Embedded are now always fully rendered
if (referencedDomainClass == null || property.isEmbedded() || property.getType().isEnum()) {
json.convertAnother(referenceObject);
}
else if (property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) {
asShortObject(referenceObject, json, referencedDomainClass.getIdentifier(), referencedDomainClass);
}
else {
GrailsDomainClassProperty referencedIdProperty = referencedDomainClass.getIdentifier();
@SuppressWarnings("unused")
String refPropertyName = referencedDomainClass.getPropertyName();
if (referenceObject instanceof Collection) {
Collection o = (Collection) referenceObject;
writer.array();
for (Object el : o) {
asShortObject(el, json, referencedIdProperty, referencedDomainClass);