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:38:37 UTC

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

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