Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright 2020 connectifex
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package com.connectifex.util.json;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;
import java.util.TreeMap;
import org.dmd.dmc.DmcValueException;
import org.json.JSONArray;
import org.json.JSONObject;
import com.connectifex.polymer.mdl.server.extended.plastic.util.PlasticConstants;
import com.connectifex.polymer.mdl.shared.generated.types.PlasticVariable;
/**
* The JNode provides support mechanisms for the creation of parameterized
* JSON payloads. It decomposes a given JSON structure into its constituent
* pieces and characterizes them as a particular JType. The resulting hierarchy
* is easy to traverse in terms parent/child relationships.
*/
public class JNode {
// The type of this node
protected JType type;
// The key or null if this is STRING, NUMBER, BOOLEAN or NULL
private String key;
// A generated uniqueKey for this node so that we can parameterize it
protected String uniqueKey;
// For an OBJECT, this is the longest key
protected int longestKey;
// A param-name if this value has been designated as a parameter
protected String parameterName;
// The parent node or null if we're the root
protected JNode parent;
// Our value as a string if type is STRING, NUMBER, BOOLEAN or NULL
protected String value;
// The children of this node
protected ArrayList children;
/**
* Root node constructor
* @param object the JSON object we start with.
*/
protected JNode(JSONObject object, JNode parent, String key) {
type = JType.OBJECT;
this.key = key;
parameterName = null;
this.parent = parent;
value = null;
children = null;
Set keys = object.keySet();
if (keys.size() > 0) {
children = new ArrayList<>();
// We maintain the different groups of things
TreeMap primitives = new TreeMap<>(Collator.getInstance(Locale.ENGLISH));
TreeMap arrays = new TreeMap<>(Collator.getInstance(Locale.ENGLISH));
TreeMap objects = new TreeMap<>(Collator.getInstance(Locale.ENGLISH));
for(Object k: keys){
String elementKey = k.toString();
if (elementKey.toString().length() > longestKey)
longestKey = elementKey.length();
Object value = object.get(elementKey);
// if (value == null) {
// JNode child = new JNode(elementKey);
// primitives.put(elementKey, child);
// continue;
// }
if (value instanceof JSONObject){
JNode child = new JNode((JSONObject)value, this, elementKey);
objects.put(elementKey, child);
}
else if (value instanceof JSONArray){
JNode child = new JNode(this, elementKey, (JSONArray)value);
arrays.put(elementKey, child);
}
else if (value instanceof String){
JNode child = new JNode(this, JType.STRING, elementKey, value.toString());
primitives.put(elementKey, child);
}
else if (value instanceof Number){
JNode child = new JNode(this, JType.NUMBER, elementKey, value.toString());
primitives.put(elementKey, child);
}
else if (value instanceof Boolean){
JNode child = new JNode(this, JType.BOOLEAN, elementKey, value.toString());
primitives.put(elementKey, child);
}
else {
JNode child = new JNode(this,elementKey);
primitives.put(elementKey, child);
}
}
for(JNode node: primitives.values())
children.add(node);
for(JNode node: arrays.values())
children.add(node);
for(JNode node: objects.values())
children.add(node);
}
}
/**
* Constructor for ARRAY
* @param parent
* @param type
* @param key
*/
private JNode(JNode parent, String key, JSONArray array) {
this.type = JType.ARRAY;
this.key = key;
parameterName = null;
this.parent = parent;
this.value = null;
if (array.length() > 0) {
children = new ArrayList<>();
Iterator