JSON object built from TreeNode implemented

This commit is contained in:
Francesco Mangiacrapa 2022-08-04 10:57:18 +02:00
parent c2e4846e58
commit 3ab1f5c89c
2 changed files with 91 additions and 269 deletions

View File

@ -167,6 +167,12 @@
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version> <version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.gcube.application</groupId> <groupId>org.gcube.application</groupId>
<artifactId>geoportal-client</artifactId> <artifactId>geoportal-client</artifactId>

View File

@ -32,17 +32,16 @@ public class FormDataObjectToJSON {
private static final String JSON_$_POINTER = "$"; private static final String JSON_$_POINTER = "$";
private static final Logger LOG = LoggerFactory.getLogger(FormDataObjectToJSON.class); private static final Logger LOG = LoggerFactory.getLogger(FormDataObjectToJSON.class);
/** /**
* Convert. * Convert.
* *
* @param tree_Node the tree node * @param tree_Node the tree node
* @param theRootDocument the the root document
* @return the JSON object * @return the JSON object
* @throws JSONException the JSON exception * @throws JSONException the JSON exception
*/ */
public JSONObject convert(Tree_Node<GeoNaFormDataObject> tree_Node, JSONObject theRootDocument) throws JSONException { public JSONObject convert(Tree_Node<GeoNaFormDataObject> tree_Node, JSONObject theRootDocument)
throws JSONException {
//JSONObject theDocument = JSONObjecOrdered.instance();
if (tree_Node == null) if (tree_Node == null)
return theRootDocument; return theRootDocument;
@ -52,132 +51,75 @@ public class FormDataObjectToJSON {
theRootDocument = JSONObjecOrdered.instance(); theRootDocument = JSONObjecOrdered.instance();
} }
Configuration configuration = Configuration.builder() Configuration configuration = Configuration.builder().jsonProvider(new JsonOrgJsonProvider()).build();
.jsonProvider(new JsonOrgJsonProvider())
.build();
for (Tree_Node<GeoNaFormDataObject> treeNodeChild : tree_Node.getChildren()) { for (Tree_Node<GeoNaFormDataObject> treeNodeChild : tree_Node.getChildren()) {
GeoNaFormDataObject gnaFO = treeNodeChild.getData(); GeoNaFormDataObject gnaFO = treeNodeChild.getData();
//Building the JSON section
// Reading data and profile
List<GenericDatasetBean> listGDB = gnaFO.getListGDB(); List<GenericDatasetBean> listGDB = gnaFO.getListGDB();
GcubeProfileDV profile = gnaFO.getGcubeProfileDV(); GcubeProfileDV profile = gnaFO.getGcubeProfileDV();
LOG.debug("The profile is: " + profile); LOG.debug("The profile is: " + profile);
// Building JSON/section full PATH and section name
String fullJSONPath = ""; String fullJSONPath = "";
String parentFullName = profile.getParentName() == null ? "" : profile.getParentName(); String parentFullName = profile.getParentName() == null ? "" : profile.getParentName();
String theSectionName = profile.getSectionName();
if(profile.getSectionName().compareTo(JSON_$_POINTER)==0 || profile.getSectionName().compareTo(JSON_$_POINTER+".")==0) { if (theSectionName.compareTo(JSON_$_POINTER) == 0 || theSectionName.compareTo(JSON_$_POINTER + ".") == 0) {
fullJSONPath = JSON_$_POINTER; fullJSONPath = JSON_$_POINTER;
theSectionName = "";
} else { } else {
fullJSONPath = String.format("%s%s", parentFullName.endsWith(".")?parentFullName:parentFullName+".", profile.getSectionName()); fullJSONPath = String.format("%s%s",
parentFullName.endsWith(".") ? parentFullName : parentFullName + ".", theSectionName);
} }
LOG.debug("The json path to build: " + fullJSONPath); LOG.debug("The json path to build: " + fullJSONPath);
LOG.debug("Current document is: " + theRootDocument); LOG.info("Current document is: " + theRootDocument);
// jsonPathExp = "$.chidl1.child2.child3"; // Building Parent PATH
String parentPath = fullJSONPath.compareTo(JSON_$_POINTER) == 0 ? JSON_$_POINTER
// String toJsonPathExp = fullJSONPath.replaceFirst("\\$", ""); : fullJSONPath.substring(0, fullJSONPath.lastIndexOf("."));
// String[] jsonPathDeep = toJsonPathExp.split("\\."); JsonPath parentJSONPath = JsonPath.compile(parentPath);
//List<JSONObject> listJSONObject = toListJonObject(listGDB, jsonPathDeep, theRootDocument, profile.getMinOccurs(), profile.getMaxOccurs()); LOG.info("Putting into parentJSONPath: " + parentJSONPath);
List<JSONObject> listJSONObject = toListJonObject(listGDB); List<JSONObject> listJSONObject = toListJonObject(listGDB);
JSONObject jsonObject = listJSONObject.get(0); JSONObject jsonObject = listJSONObject.get(0);
int maxOccurs = profile.getMaxOccurs();
String parentPath = fullJSONPath.compareTo(JSON_$_POINTER)==0?JSON_$_POINTER:fullJSONPath.substring(0,fullJSONPath.lastIndexOf("."));
JsonPath parentJSONPath = JsonPath.compile(parentPath);
LOG.debug("Putting into path: "+parentPath);
// If the maxOccurs is not 1 // If the maxOccurs is not 1
if(maxOccurs==0 || maxOccurs>1) { if (profile.getMaxOccurs() == 0 || profile.getMaxOccurs() > 1) {
LOG.debug("maxOccurs is not 1"); LOG.debug("maxOccurs is NOT 1");
//String pathExpAsArray = fullJSONPath+"[*]";
//LOG.debug("pathExpAsArray: "+pathExpAsArray);
// Must be an array // Must be an array
boolean pathExists = pathExists(theRootDocument, fullJSONPath + "[*]"); boolean pathExists = pathExists(theRootDocument, fullJSONPath + "[*]");
LOG.debug(fullJSONPath+ "exists? "+pathExists);
if (pathExists) { if (pathExists) {
LOG.debug("pathExists is true"); theRootDocument = JsonPath.parse(theRootDocument, configuration).add(fullJSONPath, jsonObject)
// Object object = JsonPath.read(theRootDocument, pathExpAsArray); .json();
// LOG.debug("Object instance is: " + object.getClass());
// if(object instanceof JSONArray) {
// JSONArray targetArray = (JSONArray) object;
// targetArray.put(jsonObject);
// }
theRootDocument = JsonPath.parse(theRootDocument,configuration).add(fullJSONPath, jsonObject).json();
//JsonPath.parse(theRootDocument,configuration).put(parentJSONPath, profile.getSectionName(), targetArray).json();
} else { } else {
LOG.debug("pathExists is false");
// Adding as array of object // Adding as array of object
JSONArray targetArray = JSONArrayOrdered.instance(); JSONArray targetArray = JSONArrayOrdered.instance();
targetArray.put(jsonObject); targetArray.put(jsonObject);
LOG.debug("JSON array created: " + targetArray); LOG.debug("JSON array created: " + targetArray);
theRootDocument = JsonPath.parse(theRootDocument,configuration).put(parentJSONPath, profile.getSectionName(), targetArray).json(); theRootDocument = JsonPath.parse(theRootDocument, configuration)
//JsonPath.parse(theRootDocument.toString()).set(setIntoPath, targetArray); .put(parentJSONPath, theSectionName, targetArray).json();
// theRootDocument.put(profile.getSectionName(), targetArray);
//
// String setIntoPath = String.format("%s[%d]", fullJSONPath, targetArray.length()-1);
// LOG.debug("setIntoPath is: " + setIntoPath);
// JsonPath.parse(theRootDocument.toString()).set(setIntoPath, targetArray);
} }
LOG.debug("theRootDocument as array is: " + theRootDocument); LOG.debug("theRootDocument as array is: " + theRootDocument);
//theRootDocument.get(jsonPathDeep[jsonPathDeep.length - 1], targetArray);
} else { } else {
//Adding as single object LOG.debug("maxOccurs is 1");
//theRootDocument.put(jsonPathDeep[jsonPathDeep.length - 1], jsonObject); // Merging as direct properties of the JSON root document
if (theSectionName == null || theSectionName.isEmpty()) {
deepMerge(jsonObject, theRootDocument);
} else {
// Putting as child of the JSON document
// theRootDocument.put(profile.getSectionName(), jsonObject); // theRootDocument.put(profile.getSectionName(), jsonObject);
theRootDocument = JsonPath.parse(theRootDocument,configuration).put(parentJSONPath, profile.getSectionName(), jsonObject).json(); theRootDocument = JsonPath.parse(theRootDocument, configuration)
// jsonObject.put(profile.getSectionName(), jsonObject); .put(parentJSONPath, theSectionName, jsonObject).json();
// parentPath = parentPath.endsWith(".")?parentPath.substring(0,parentPath.length()-1):parentPath; }
// JsonPath.parse(theRootDocument.toString()).set(parentPath, jsonObject);
LOG.debug("theRootDocument as single object is: " + theRootDocument); LOG.debug("theRootDocument as single object is: " + theRootDocument);
} }
/*
// adding as JSONObject to theDocument at first deep level (under the root or to the section specified)
if (listJSONObject.size() == 1) {
int maxOccurs = profile.getMaxOccurs();
//If the maxOccurs is not 1
if(maxOccurs==0 || maxOccurs>1) {
//Adding as array of object
JSONArray targetArray = JSONArrayOrdered.instance();
targetArray.put(jsonObject);
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], targetArray);
}else {
//Adding as single object
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], jsonObject);
}
theRootDocument = deepMerge(listJSONObject.get(0), theRootDocument);
//theDocument = theDocument.put(listJSONObject.get(0), jsonArray);
} else {
// adding as JSONArray to theDocument under the section specified
// to jsonPathDeep[1]
JSONArray jsonArray = new JSONArray();
for (JSONObject jsonObject : listJSONObject) {
// reading the i-mo JSONObject created in the list with the key as
// jsonPathDeep[1]
jsonArray.put(jsonObject.get(jsonPathDeep[1]));
}
theRootDocument.put(jsonPathDeep[1], jsonArray);
}*/
// recursive call... // recursive call...
theRootDocument = convert(treeNodeChild, theRootDocument); theRootDocument = convert(treeNodeChild, theRootDocument);
} }
@ -187,24 +129,13 @@ public class FormDataObjectToJSON {
} }
/*public static <T> void appendInDocument(JSONObject node, String pathExp) { /**
GWT.log("preOrderVisit called"); * Path exists.
*
if (node == null) * @param document the document
return; * @param pathExp the path exp
* @return true, if successful
*/
GWT.log("preOrderVisit Node name: " + node + ", parent: " + parentName);
for (Tree_Node<T> child : node.getChildren()) {
preOrderVisit(child);
}
return;
}*/
public static boolean pathExists(JSONObject document, String pathExp) { public static boolean pathExists(JSONObject document, String pathExp) {
LOG.debug("pathExists called"); LOG.debug("pathExists called");
@ -224,56 +155,6 @@ public class FormDataObjectToJSON {
return false; return false;
} }
/**
* Convert.
*
* @param tree_Node the tree node
* @return the JSON object
* @throws JSONException the JSON exception
*/
/*public JSONObject convert(Tree_Node<GeoNaFormDataObject> tree_Node) throws JSONException {
JSONObject theDocument = JSONObjecOrdered.instance();
for (GeoNaFormDataObject geoNaFormDataObject : tree_Node) {
List<GenericDatasetBean> listGDB = geoNaFormDataObject.getListGDB();
GcubeProfileDV profile = geoNaFormDataObject.getGcubeProfileDV();
LOG.debug("The profile is: " + profile);
String jsonPathExp = String.format("%s%s", profile.getParentName(), profile.getSectionName());
LOG.debug("The json path to build: " + jsonPathExp);
// jsonPathExp = "$.chidl1.child2.child3";
String toJsonPathExp = jsonPathExp.replaceFirst("\\$", "");
String[] jsonPathDeep = toJsonPathExp.split("\\.");
List<JSONObject> listJSONObject = toListJonObject(listGDB, jsonPathDeep);
// adding as JSONObject to theDocument at first deep level (under the root or at section specified)
if (listJSONObject.size() == 1) {
theDocument = deepMerge(listJSONObject.get(0), theDocument);
//theDocument = theDocument.put(listJSONObject.get(0), jsonArray);
} else {
// adding as JSONArray to theDocument under the section specified
// to jsonPathDeep[1]
JSONArray jsonArray = new JSONArray();
for (JSONObject jsonObject : listJSONObject) {
// reading the i-mo JSONObject created in the list with the key as
// jsonPathDeep[1]
jsonArray.put(jsonObject.get(jsonPathDeep[1]));
}
theDocument.put(jsonPathDeep[1], jsonArray);
}
LOG.debug("Partial Root Document is: " + theDocument);
}
LOG.debug("Final JSON Document is: " + theDocument);
return theDocument;
}*/
/** /**
* Generic dataset bean to JSON. * Generic dataset bean to JSON.
* *
@ -292,13 +173,11 @@ public class FormDataObjectToJSON {
if (listValues == null || listValues.isEmpty()) { if (listValues == null || listValues.isEmpty()) {
continue; continue;
} }
// key/value as string // key/value as string
if (listValues.size() == 1) { if (listValues.size() == 1) {
sectJSONObject.put(key, listValues.get(0)); sectJSONObject.put(key, listValues.get(0));
continue; continue;
} }
// value is a list // value is a list
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
for (String value : listValues) { for (String value : listValues) {
@ -309,26 +188,22 @@ public class FormDataObjectToJSON {
} }
return sectJSONObject; return sectJSONObject;
} }
/** /**
* To list jon object. * To list jon object.
* *
* @param listGDB the list GDB * @param listGDB the list GDB
* @param jsonPathDeep the json path deep
* @return the list * @return the list
* @throws JSONException the JSON exception * @throws JSONException the JSON exception
*/ */
private List<JSONObject> toListJonObject(List<GenericDatasetBean> listGDB) private List<JSONObject> toListJonObject(List<GenericDatasetBean> listGDB) throws JSONException {
throws JSONException {
List<JSONObject> listJSONObject = new ArrayList<JSONObject>(); List<JSONObject> listJSONObject = new ArrayList<JSONObject>();
for (GenericDatasetBean gdb : listGDB) { for (GenericDatasetBean gdb : listGDB) {
JSONObject jsonObject = genericDatasetBeanToJSON(gdb); JSONObject jsonObject = genericDatasetBeanToJSON(gdb);
listJSONObject.add(jsonObject); listJSONObject.add(jsonObject);
} }
LOG.info("returning : " + listJSONObject); LOG.info("returning : " + listJSONObject);
@ -336,63 +211,6 @@ public class FormDataObjectToJSON {
} }
/**
* To list jon object.
*
* @param listGDB the list GDB
* @param jsonPathDeep the json path deep
* @return the list
* @throws JSONException the JSON exception
*/
private List<JSONObject> toListJonObject(List<GenericDatasetBean> listGDB, String[] jsonPathDeep, JSONObject theRootDocument, int minOccurs, int maxOccurs)
throws JSONException {
List<JSONObject> listJSONObject = new ArrayList<JSONObject>();
for (GenericDatasetBean gdb : listGDB) {
JSONObject sectRootObject = JSONObjecOrdered.instance();
JSONObject jsonObject = genericDatasetBeanToJSON(gdb);
LOG.debug("Adding section : " + jsonObject);
LOG.trace("jsonPathDeep: " + Arrays.asList(jsonPathDeep) + " size: " + jsonPathDeep.length);
// Adding JSONObject to ROOT DOCUMENT POSITION (using PLACEHOLDER_ROOT_POINTER_JSON_DOC)
if (jsonPathDeep.length == 0) {
sectRootObject = deepMerge(jsonObject, sectRootObject);
} else {
//If the maxOccurs is not 1
if(maxOccurs==0 || maxOccurs>1) {
//Adding as array of object
JSONArray targetArray = JSONArrayOrdered.instance();
targetArray.put(jsonObject);
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], targetArray);
}else {
//Adding as single object
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], jsonObject);
}
JSONObject deepJSON = sectRootObject;
System.out.println("toListJonObject sectRootObject: " + sectRootObject.toString());
for (int i = jsonPathDeep.length - 2; i > 0; i--) {
JSONObject newOne = JSONObjecOrdered.instance();
newOne.put(jsonPathDeep[i], deepJSON);
deepJSON = newOne;
}
sectRootObject = deepJSON;
}
listJSONObject.add(sectRootObject);
}
LOG.info("For listGDB : " + listGDB);
LOG.info("returning : " + listJSONObject);
return listJSONObject;
}
/** /**
* Merge "source" into "target". If fields have equal name, merge them * Merge "source" into "target". If fields have equal name, merge them
* recursively. * recursively.
@ -421,7 +239,6 @@ public class FormDataObjectToJSON {
return target; return target;
} }
/** /**
* The Class JSONObjecOrdered. * The Class JSONObjecOrdered.
* *
@ -449,7 +266,6 @@ public class FormDataObjectToJSON {
} }
} }
/** /**
* The Class JSONObjecOrdered. * The Class JSONObjecOrdered.
* *