sergey-chugunov-1985 commented on a change in pull request #72:
URL: https://github.com/apache/ignite-3/pull/72#discussion_r600229646
##########
File path:
modules/rest/src/main/java/org/apache/ignite/rest/presentation/json/JsonConverter.java
##########
@@ -35,6 +52,221 @@
return gson.toJson(obj);
}
+ /** */
+ public static ConfigurationVisitor<JsonElement> jsonVisitor() {
+ return new ConfigurationVisitor<JsonElement>() {
+ /** */
+ private final Deque<JsonObject> jsonObjectsStack = new
ArrayDeque<>();
+
+ /** {@inheritDoc} */
+ @Override public JsonElement visitLeafNode(String key,
Serializable val) {
+ JsonElement jsonLeaf = toJsonLeaf(val);
+
+ addToParent(key, jsonLeaf);
+
+ return jsonLeaf;
+ }
+
+ /** {@inheritDoc} */
+ @Override public JsonElement visitInnerNode(String key, InnerNode
node) {
+ JsonObject innerJsonNode = new JsonObject();
+
+ jsonObjectsStack.push(innerJsonNode);
+
+ node.traverseChildren(this);
+
+ jsonObjectsStack.pop();
+
+ addToParent(key, innerJsonNode);
+
+ return innerJsonNode;
+ }
+
+ /** {@inheritDoc} */
+ @Override public <N extends InnerNode> JsonElement
visitNamedListNode(String key, NamedListNode<N> node) {
+ JsonObject namedListJsonNode = new JsonObject();
+
+ jsonObjectsStack.push(namedListJsonNode);
+
+ for (String subkey : node.namedListKeys())
+ node.get(subkey).accept(subkey, this);
+
+ jsonObjectsStack.pop();
+
+ addToParent(key, namedListJsonNode);
+
+ return namedListJsonNode;
+ }
+
+ /** */
+ @NotNull private JsonElement toJsonLeaf(Serializable val) {
+ if (val == null)
+ return JsonNull.INSTANCE;
+
+ Class<? extends Serializable> valClass = val.getClass();
+
+ if (!valClass.isArray())
+ return toJsonPrimitive(val);
+
+ JsonArray jsonArray = new JsonArray();
+
+ for (int i = 0; i < Array.getLength(val); i++)
+ jsonArray.add(toJsonPrimitive(Array.get(val, i)));
+
+ return jsonArray;
+ }
+
+ /** */
+ @NotNull private JsonElement toJsonPrimitive(Object val) {
+ if (val == null)
+ return JsonNull.INSTANCE;
+
+ if (val instanceof Boolean)
+ return new JsonPrimitive((Boolean)val);
+
+ if (val instanceof String)
+ return new JsonPrimitive((String)val);
+
+ if (val instanceof Number)
+ return new JsonPrimitive((Number)val);
+
+ assert false : val;
+
+ throw new
IllegalArgumentException(val.getClass().getCanonicalName());
+ }
+
+ /**
+ * Add subelement to the paretn JSON object if it exists.
+ *
+ * @param key Key for the passed JSON element.
+ * @param jsonElement JSON element to add to the parent.
+ */
+ private void addToParent(String key, JsonElement jsonElement) {
+ if (!jsonObjectsStack.isEmpty())
+ jsonObjectsStack.peek().add(key, jsonElement);
+ }
+ };
+ }
+
+ /** */
+ public static ConfigurationSource jsonSource(JsonElement jsonElement) {
+ //TODO IGNITE-14372 Finish this implementation.
+ return null;
+ }
+
+ private static class JsonObjectConfigurationSource implements
ConfigurationSource {
+ /** Shared. */
+ private final List<String> path;
+
+ /** */
+ private final JsonObject jsonObject;
+
+ private JsonObjectConfigurationSource(List<String> path, JsonObject
jsonObject) {
+ this.path = path;
+ this.jsonObject = jsonObject;
+ }
+
+ @Override public <T> T unwrap(Class<T> clazz) {
+ throw new IllegalArgumentException(""); //TODO IGNITE-14372
Implement.
+ }
+
+ @Override public void descend(ConstructableTreeNode node) {
+ for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet())
{
+ String key = entry.getKey();
+
+ path.add(key);
+
+ JsonElement jsonElement = entry.getValue();
+
+ try {
+ if (jsonElement.isJsonArray() ||
jsonElement.isJsonPrimitive())
+ node.construct(key, new
JsonPrimitiveConfigurationSource(path, jsonElement));
+ else if (jsonElement.isJsonNull()) {
+ node.construct(key, null);
+ }
+ else {
+ assert jsonElement.isJsonObject();
+
+ List<String> path = new ArrayList<>(this.path.size() +
1);
+ path.addAll(this.path);
+ path.add(key);
+
+ node.construct(key, new
JsonObjectConfigurationSource(path, jsonElement.getAsJsonObject()));
+ }
+ }
+ catch (NoSuchElementException e) {
+ throw new IllegalArgumentException(""); //TODO
IGNITE-14372 Update comment.
+ }
+
+ path.remove(path.size() - 1);
+ }
+ }
+ }
+
+ private static class JsonPrimitiveConfigurationSource implements
ConfigurationSource {
+ private final List<String> path;
+
+ private final JsonElement jsonLeaf;
+
+ private JsonPrimitiveConfigurationSource(List<String> path,
JsonElement jsonLeaf) {
+ this.path = path;
+ this.jsonLeaf = jsonLeaf;
+ }
+
+ @Override public <T> T unwrap(Class<T> clazz) {
+ if (clazz.isArray() != jsonLeaf.isJsonArray())
+ throw new IllegalArgumentException(""); //TODO IGNITE-14372
Update comment.
+
+ return null;
+ }
+
+ @Override public void descend(ConstructableTreeNode node) {
+ throw new IllegalArgumentException(""); //TODO IGNITE-14372 Update
comment.
+ }
+
+ private <T> T unwrap(JsonPrimitive jsonPrimitive, Class<T> clazz) {
+ assert !clazz.isArray();
+
+ if (clazz == String.class) {
+ if (!jsonPrimitive.isString())
+ throw new IllegalArgumentException("");
+
+ return clazz.cast(jsonPrimitive.getAsString());
+ }
+
+ if (Number.class.isAssignableFrom(clazz)) {
+ if (!jsonPrimitive.isNumber())
+ throw new IllegalArgumentException("");
+
+ if (clazz == Double.class)
+ return clazz.cast(jsonPrimitive.getAsDouble());
+
+ if (clazz == Long.class)
+ return clazz.cast(jsonPrimitive.getAsLong());
+
+ if (clazz == Integer.class) {
+ long longValue = jsonPrimitive.getAsLong();
+
+ if (longValue < Integer.MIN_VALUE || longValue >
Integer.MAX_VALUE)
+ throw new IllegalArgumentException("");
Review comment:
TODO needed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]