ibessonov commented on a change in pull request #72:
URL: https://github.com/apache/ignite-3/pull/72#discussion_r598708174
##########
File path:
modules/rest/src/main/java/org/apache/ignite/rest/presentation/json/JsonConverter.java
##########
@@ -35,6 +52,213 @@
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);
+
+ if (!jsonObjectsStack.isEmpty())
+ jsonObjectsStack.peek().add(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();
+
+ if (!jsonObjectsStack.isEmpty())
+ jsonObjectsStack.peek().add(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();
+
+ if (!jsonObjectsStack.isEmpty())
+ jsonObjectsStack.peek().add(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;
Review comment:
The difference is that "assert false" explicitly says that this code
branch is not valid
--
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]