You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by GitBox <gi...@apache.org> on 2022/06/01 09:14:13 UTC

[GitHub] [jackrabbit-oak] AngelaFabregues opened a new pull request, #582: Deeper representation of the trees with properties and in json format.

AngelaFabregues opened a new pull request, #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582

   AbstractTree's previous toString() method was calling a toString(int depth) method that wasn't working really. It did only represent the first level of the tree. Also the properties were represented together with the child nodes.
   This implementation goes as deep as necessary and represents the node in JSON format. Therefore, making it queryable with JSON tools like jq. 


-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886796825


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +150,63 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");
+
+            if (ps.getType().isArray()) {
+                str.append("[ ");
+                for (int i = 0; i < ps.count(); i++) {
+                    try {
+                        str.append(quote(escapeJsonString(ps.getValue(Type.STRING, i))) + ",");
+                    } catch (Exception e) {
+                        str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                    }
+                }
+                str.deleteCharAt(str.length() - 1); //removing the space or the ,
+                str.append("],");
+            } else {
+                try {
+                    str.append(quote(escapeJsonString(ps.getValue(Type.STRING))) + ",");
+                } catch (Exception e) {
+                    str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str.deleteCharAt(str.length() - 1);  //removing the space or the ,
+        str.append("},");
+        for (Tree child : this.getChildren()) {
+            str.append(quote(child.getName()) + ":");

Review Comment:
   Applied with JsopBuilder.encode()



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886739003


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";

Review Comment:
   Done



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886735827


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";

Review Comment:
   Done!



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] thomasmueller commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
thomasmueller commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886771861


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +150,63 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");
+
+            if (ps.getType().isArray()) {
+                str.append("[ ");
+                for (int i = 0; i < ps.count(); i++) {
+                    try {
+                        str.append(quote(escapeJsonString(ps.getValue(Type.STRING, i))) + ",");
+                    } catch (Exception e) {
+                        str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                    }
+                }
+                str.deleteCharAt(str.length() - 1); //removing the space or the ,
+                str.append("],");
+            } else {
+                try {
+                    str.append(quote(escapeJsonString(ps.getValue(Type.STRING))) + ",");
+                } catch (Exception e) {
+                    str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str.deleteCharAt(str.length() - 1);  //removing the space or the ,
+        str.append("},");
+        for (Tree child : this.getChildren()) {
+            str.append(quote(child.getName()) + ":");
+            str.append(((AbstractTree) child).toJsonString(depth - 1) + ",");
         }
+        str.deleteCharAt(str.length() - 1); //removing the ,
+        str.append("}");
+        return str.toString();
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String escapeJsonString(String value) {
+        return value.replaceAll("\"","\\/");
+    }
 
-        return sb.toString();
+    private String quote(String toQuote) {

Review Comment:
   private static



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +150,63 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");
+
+            if (ps.getType().isArray()) {
+                str.append("[ ");
+                for (int i = 0; i < ps.count(); i++) {
+                    try {
+                        str.append(quote(escapeJsonString(ps.getValue(Type.STRING, i))) + ",");
+                    } catch (Exception e) {
+                        str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                    }
+                }
+                str.deleteCharAt(str.length() - 1); //removing the space or the ,
+                str.append("],");
+            } else {
+                try {
+                    str.append(quote(escapeJsonString(ps.getValue(Type.STRING))) + ",");
+                } catch (Exception e) {
+                    str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str.deleteCharAt(str.length() - 1);  //removing the space or the ,
+        str.append("},");
+        for (Tree child : this.getChildren()) {
+            str.append(quote(child.getName()) + ":");
+            str.append(((AbstractTree) child).toJsonString(depth - 1) + ",");
         }
+        str.deleteCharAt(str.length() - 1); //removing the ,
+        str.append("}");
+        return str.toString();
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String escapeJsonString(String value) {
+        return value.replaceAll("\"","\\/");

Review Comment:
   This doesn't correctly escape... It's probably better to use oak-commons org.apache.jackrabbit.oak.commons.json 
   JsopBuilder.escape



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +150,63 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");
+
+            if (ps.getType().isArray()) {
+                str.append("[ ");
+                for (int i = 0; i < ps.count(); i++) {
+                    try {
+                        str.append(quote(escapeJsonString(ps.getValue(Type.STRING, i))) + ",");
+                    } catch (Exception e) {
+                        str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                    }
+                }
+                str.deleteCharAt(str.length() - 1); //removing the space or the ,
+                str.append("],");
+            } else {
+                try {
+                    str.append(quote(escapeJsonString(ps.getValue(Type.STRING))) + ",");
+                } catch (Exception e) {
+                    str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str.deleteCharAt(str.length() - 1);  //removing the space or the ,
+        str.append("},");
+        for (Tree child : this.getChildren()) {
+            str.append(quote(child.getName()) + ":");

Review Comment:
   I think it needs to be:
   
       str.append(quote(escapeJsonString(child.getName())) + ":");
   



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#issuecomment-1145723183

   Hi, we've merged the code into a branch so more people can work on improving it before merging to trunk.
   This is the merged PR https://github.com/apache/jackrabbit-oak/pull/583 towards the branch apache:OAK-9789.


-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886792345


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +150,63 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");
+
+            if (ps.getType().isArray()) {
+                str.append("[ ");
+                for (int i = 0; i < ps.count(); i++) {
+                    try {
+                        str.append(quote(escapeJsonString(ps.getValue(Type.STRING, i))) + ",");
+                    } catch (Exception e) {
+                        str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                    }
+                }
+                str.deleteCharAt(str.length() - 1); //removing the space or the ,
+                str.append("],");
+            } else {
+                try {
+                    str.append(quote(escapeJsonString(ps.getValue(Type.STRING))) + ",");
+                } catch (Exception e) {
+                    str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str.deleteCharAt(str.length() - 1);  //removing the space or the ,
+        str.append("},");
+        for (Tree child : this.getChildren()) {
+            str.append(quote(child.getName()) + ":");
+            str.append(((AbstractTree) child).toJsonString(depth - 1) + ",");
         }
+        str.deleteCharAt(str.length() - 1); //removing the ,
+        str.append("}");
+        return str.toString();
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String escapeJsonString(String value) {
+        return value.replaceAll("\"","\\/");
+    }
 
-        return sb.toString();
+    private String quote(String toQuote) {

Review Comment:
   Done



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues closed pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues closed pull request #582: Deeper representation of the trees with properties and in json format.
URL: https://github.com/apache/jackrabbit-oak/pull/582


-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886791314


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +150,63 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");
+
+            if (ps.getType().isArray()) {
+                str.append("[ ");
+                for (int i = 0; i < ps.count(); i++) {
+                    try {
+                        str.append(quote(escapeJsonString(ps.getValue(Type.STRING, i))) + ",");
+                    } catch (Exception e) {
+                        str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                    }
+                }
+                str.deleteCharAt(str.length() - 1); //removing the space or the ,
+                str.append("],");
+            } else {
+                try {
+                    str.append(quote(escapeJsonString(ps.getValue(Type.STRING))) + ",");
+                } catch (Exception e) {
+                    str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str.deleteCharAt(str.length() - 1);  //removing the space or the ,
+        str.append("},");
+        for (Tree child : this.getChildren()) {
+            str.append(quote(child.getName()) + ":");
+            str.append(((AbstractTree) child).toJsonString(depth - 1) + ",");
         }
+        str.deleteCharAt(str.length() - 1); //removing the ,
+        str.append("}");
+        return str.toString();
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String escapeJsonString(String value) {
+        return value.replaceAll("\"","\\/");

Review Comment:
   JsopBuilder.encode is perfect. Thanks for spotting it out.
   Done



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] reschke commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
reschke commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886904736


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,59 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");

Review Comment:
   or
   
   ~~~
   String sep = "";
   for (PropertyState ps : this.getProperties()) {
       buf.append(sep);
       sep = ", ";
   ~~~



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] thomasmueller commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
thomasmueller commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886780433


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +150,63 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");
+
+            if (ps.getType().isArray()) {
+                str.append("[ ");
+                for (int i = 0; i < ps.count(); i++) {
+                    try {
+                        str.append(quote(escapeJsonString(ps.getValue(Type.STRING, i))) + ",");
+                    } catch (Exception e) {
+                        str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                    }
+                }
+                str.deleteCharAt(str.length() - 1); //removing the space or the ,
+                str.append("],");
+            } else {
+                try {
+                    str.append(quote(escapeJsonString(ps.getValue(Type.STRING))) + ",");
+                } catch (Exception e) {
+                    str.append(quote("ERROR:" + escapeJsonString(e.getMessage())) + ",");
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str.deleteCharAt(str.length() - 1);  //removing the space or the ,
+        str.append("},");
+        for (Tree child : this.getChildren()) {
+            str.append(quote(child.getName()) + ":");
+            str.append(((AbstractTree) child).toJsonString(depth - 1) + ",");
         }
+        str.deleteCharAt(str.length() - 1); //removing the ,
+        str.append("}");
+        return str.toString();
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String escapeJsonString(String value) {
+        return value.replaceAll("\"","\\/");

Review Comment:
   so:
   
       StringBuilder buff = new StringBuilder();
       JsopBuilder.escape(s, s.length(), buff);
       return buff.toString();



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886742095


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){
+                        str += quote("ERROR:" + jsonStringEscaper(e.getMessage()))+",";
+                    }
+                }
+                str = str.substring(0,str.length()-1); //removing the space or the ,
+                str += "],";
+            }else {
+                try{
+                    str += quote(jsonStringEscaper(ps.getValue(Type.STRING))) + ",";
+                }catch (Exception e){
+                    str += quote("ERROR:" + jsonStringEscaper(e.getMessage())) + ",";
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str = str.substring(0,str.length()-1); //removing the space or the ,
+        str+="},";
+        for (Tree child : this.getChildren()){
+            str+=quote(child.getName())+":";
+            str+=((AbstractTree)child).toJsonString(depth-1)+",";
         }
+        str = str.substring(0,str.length()-1); //removing the ,
+        str+="}";
+        return str;
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String jsonStringEscaper(String value){
+        return value.replaceAll("\"","\\/");
+    }
 
-        return sb.toString();
+    private String quote (String toQuote) {

Review Comment:
   Done!



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886743150


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){

Review Comment:
   done



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886749525


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){
+                        str += quote("ERROR:" + jsonStringEscaper(e.getMessage()))+",";
+                    }
+                }
+                str = str.substring(0,str.length()-1); //removing the space or the ,

Review Comment:
   it would be` if ( i < ps.count()-1 ) { `at the end of the for and I cannot use it for `for (Tree child : this.getChildren()) {`.
   I think it will be more confusing to have two ways of doing the same. Any alternatives?



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] thomasmueller commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
thomasmueller commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886802446


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,59 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth) {
+        if (depth == 0) {
+            return quote("...");
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        StringBuilder str = new StringBuilder();
+        str.append("{");
+        str.append(quote("_properties_") + ":{ ");
+        for (PropertyState ps : this.getProperties()) {
+            str.append(quote(ps.getName()) + ":");

Review Comment:
   To replace the trailing comma with a prefix comma:
   
       int count = 0;
       for (PropertyState ps : this.getProperties()) {
           if (count++ > 0) {
               buff.append(", ");
           }



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] joerghoh commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
joerghoh commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886593077


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,60 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return "\"...\"";
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        String str = "{";
+        str += "\"_properties_\":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+="\""+ps.getName()+"\":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += "\"" + jsonStringEscaper(ps.getValue(Type.STRING, i)) + "\",";
+                    }catch (Exception e){
+                        e.printStackTrace();

Review Comment:
   e.printStackTrace will not be logged, but always printed to stderr, which is not always captured, and misses context like a timestamp. 
   Please use proper logging.



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,60 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return "\"...\"";
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        String str = "{";
+        str += "\"_properties_\":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+="\""+ps.getName()+"\":";

Review Comment:
   I don't feel comfortable with all these string operations here, especially when it's about quoting.
   
   can you wrap these string operations into meaningful static methods like this?
   ```
   private static String quote (String toQuote) {
     return "\"" + toQuote + "\"";
   }
   
   [...]
   str+=quote(ps.get.Name());
   [...]
   ```
   That would make the code easier to understand.



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,60 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){

Review Comment:
   I think that we need to have some tests for this.



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886642179


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,60 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return "\"...\"";
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        String str = "{";
+        str += "\"_properties_\":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+="\""+ps.getName()+"\":";

Review Comment:
   Cool!



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886741587


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){
+                        str += quote("ERROR:" + jsonStringEscaper(e.getMessage()))+",";
+                    }
+                }
+                str = str.substring(0,str.length()-1); //removing the space or the ,
+                str += "],";
+            }else {
+                try{
+                    str += quote(jsonStringEscaper(ps.getValue(Type.STRING))) + ",";
+                }catch (Exception e){
+                    str += quote("ERROR:" + jsonStringEscaper(e.getMessage())) + ",";
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str = str.substring(0,str.length()-1); //removing the space or the ,
+        str+="},";
+        for (Tree child : this.getChildren()){
+            str+=quote(child.getName())+":";
+            str+=((AbstractTree)child).toJsonString(depth-1)+",";
         }
+        str = str.substring(0,str.length()-1); //removing the ,
+        str+="}";
+        return str;
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String jsonStringEscaper(String value){
+        return value.replaceAll("\"","\\/");
+    }
 
-        return sb.toString();
+    private String quote (String toQuote) {
+        return "\"" + toQuote + "\"";

Review Comment:
   Done



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886738759


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){

Review Comment:
   Done!



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886739217


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){

Review Comment:
   Done



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] thomasmueller commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
thomasmueller commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886723058


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){

Review Comment:
   Nit: please add spaces before "{" (standard code style)



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";

Review Comment:
   spaces should be added around "+=" and "+", and after "if" and so on.



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){

Review Comment:
   Spaces around "=" and "<" and after "for" and before "{"



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){
+                        str += quote("ERROR:" + jsonStringEscaper(e.getMessage()))+",";
+                    }
+                }
+                str = str.substring(0,str.length()-1); //removing the space or the ,
+                str += "],";
+            }else {
+                try{
+                    str += quote(jsonStringEscaper(ps.getValue(Type.STRING))) + ",";
+                }catch (Exception e){
+                    str += quote("ERROR:" + jsonStringEscaper(e.getMessage())) + ",";
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str = str.substring(0,str.length()-1); //removing the space or the ,
+        str+="},";
+        for (Tree child : this.getChildren()){
+            str+=quote(child.getName())+":";
+            str+=((AbstractTree)child).toJsonString(depth-1)+",";
         }
+        str = str.substring(0,str.length()-1); //removing the ,
+        str+="}";
+        return str;
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String jsonStringEscaper(String value){
+        return value.replaceAll("\"","\\/");
+    }
 
-        return sb.toString();
+    private String quote (String toQuote) {

Review Comment:
   Actually there should be no space before "("



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){
+                        str += quote("ERROR:" + jsonStringEscaper(e.getMessage()))+",";
+                    }
+                }
+                str = str.substring(0,str.length()-1); //removing the space or the ,
+                str += "],";
+            }else {
+                try{
+                    str += quote(jsonStringEscaper(ps.getValue(Type.STRING))) + ",";
+                }catch (Exception e){
+                    str += quote("ERROR:" + jsonStringEscaper(e.getMessage())) + ",";
+                }
+            }
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        str = str.substring(0,str.length()-1); //removing the space or the ,
+        str+="},";
+        for (Tree child : this.getChildren()){
+            str+=quote(child.getName())+":";
+            str+=((AbstractTree)child).toJsonString(depth-1)+",";
         }
+        str = str.substring(0,str.length()-1); //removing the ,
+        str+="}";
+        return str;
+    }
 
-        if (sb.charAt(sb.length() - 1) == ',') {
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        sb.append('}');
+    private String jsonStringEscaper(String value){
+        return value.replaceAll("\"","\\/");
+    }
 
-        return sb.toString();
+    private String quote (String toQuote) {
+        return "\"" + toQuote + "\"";

Review Comment:
   What about escaping?



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){

Review Comment:
   Spaces after "}" and before "{"



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";
+        str += quote("_properties_")+":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+=quote(ps.getName())+":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += quote(jsonStringEscaper(ps.getValue(Type.STRING, i)))+",";
+                    }catch (Exception e){
+                        str += quote("ERROR:" + jsonStringEscaper(e.getMessage()))+",";
+                    }
+                }
+                str = str.substring(0,str.length()-1); //removing the space or the ,

Review Comment:
   I would use the following style instead:
   
       for (int i = 0; i < ps.count(); i++) {
           if (i > 0) {
               buff.append(", ");
           }
           buff.append(...)



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,62 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return quote("...");
         }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+        String str = "{";

Review Comment:
   Please use StringBuilder instead of concatenating strings.



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886639543


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,60 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){
+        if(depth == 0){
+            return "\"...\"";
         }
-
-        if (names.hasNext()) {
-            sb.append(" ...");
+        String str = "{";
+        str += "\"_properties_\":{ ";
+        for (PropertyState ps : this.getProperties()){
+            str+="\""+ps.getName()+"\":";
+
+            if(ps.getType().isArray()){
+                str += "[ ";
+                for(int i=0; i<ps.count(); i++){
+                    try {
+                        str += "\"" + jsonStringEscaper(ps.getValue(Type.STRING, i)) + "\",";
+                    }catch (Exception e){
+                        e.printStackTrace();

Review Comment:
   True!! and it is neither needed. I will delete the e.printStackTrace();



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jackrabbit-oak] AngelaFabregues commented on a diff in pull request #582: Deeper representation of the trees with properties and in json format.

Posted by GitBox <gi...@apache.org>.
AngelaFabregues commented on code in PR #582:
URL: https://github.com/apache/jackrabbit-oak/pull/582#discussion_r886651778


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/tree/impl/AbstractTree.java:
##########
@@ -150,34 +151,60 @@ protected Iterable<String> getChildNames() {
 
     @Override
     public String toString() {
-        return toString(5);
+        return toJsonString(2);
     }
 
-    private String toString(int childNameCountLimit) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getPath()).append(": ");
-
-        sb.append('{');
-        for (PropertyState p : getProperties()) {
-            sb.append(' ').append(p).append(',');
-        }
-
-        Iterator<String> names = this.getChildNames().iterator();
-        int count = 0;
-        while (names.hasNext() && ++count <= childNameCountLimit) {
-            sb.append(' ').append(names.next()).append(" = { ... },");
+    /**
+     * Represents an OAK tree as a JSON object string
+     * When the representation is limited in depth, {"...":"..."} represents the limit.
+     * Note that, try-catch sentences are used to add "ERROR: "+e.getMessage() values in case of representation error.
+     * Note that, this is a recursive method.
+     * @param depth tree depth to represent. Use -1 for an unlimited depth.
+     * @return json object representation of the tree as a string
+     */
+    public String toJsonString(int depth){

Review Comment:
   I need your help here. I've tested it with the ExcerptTest repositories created with initial data for elastic and lucene. The methods seems to work like a charm.
   Feel free to debug any code and evaluate, for instance, trees from the root with `((AbstractTree)root.getTree("/")).toJsonString(-1)`. 



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org