Providing an order to properties of JSON to improve human readability

This commit is contained in:
Luca Frosini 2023-04-27 12:20:57 +02:00
parent 7b582be333
commit 93e3768a64
1 changed files with 37 additions and 14 deletions

View File

@ -30,6 +30,7 @@ import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
@ -375,33 +376,55 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
}
private void analizeProperty(OElement element, String key, ObjectNode objectNode) throws ResourceRegistryException {
Object object = element.getProperty(key);
if(object == null) {
objectNode.replace(key, null);
return;
}
JsonNode jsonNode = getPropertyForJson(key, object);
if(jsonNode != null) {
objectNode.replace(key, jsonNode);
}
}
private JsonNode createSelfJsonNode() throws ResourceRegistryException {
try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
OElement element = getElement();
Set<String> keys = element.getPropertyNames();
for(String key : keys) {
Object object = element.getProperty(key);
if(object == null) {
objectNode.replace(key, null);
continue;
}
JsonNode jsonNode = getPropertyForJson(key, object);
if(jsonNode != null) {
objectNode.replace(key, jsonNode);
}
}
objectNode.put(Element.TYPE_PROPERTY, getTypeName());
Collection<String> superClasses = getCachedType().getSuperTypes();
ArrayNode arrayNode = objectMapper.valueToTree(superClasses);
objectNode.replace(Element.SUPERTYPES_PROPERTY, arrayNode);
OElement element = getElement();
Set<String> keys = element.getPropertyNames();
/* Add first these key to provide an order in Json */
List<String> keysToAddFirst = new ArrayList<>();
keysToAddFirst.add(IdentifiableElement.UUID_PROPERTY);
keysToAddFirst.add(IdentifiableElement.METADATA_PROPERTY);
keysToAddFirst.add(Relation.PROPAGATION_CONSTRAINT_PROPERTY);
for(String key : keysToAddFirst) {
if(keys.contains(key)) {
analizeProperty(element, key, objectNode);
}
}
for(String key : keys) {
if(keysToAddFirst.contains(key)) {
// the property has been already added
continue;
}
analizeProperty(element, key, objectNode);
}
return objectNode;
} catch(ResourceRegistryException e) {
throw e;