pombredanne commented on code in PR #325:
URL: 
https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1485889948


##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.dependency.tree;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import 
org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer using 
the JSON format.
+ */
+public class JsonDependencyNodeVisitor extends AbstractSerializingVisitor 
implements DependencyNodeVisitor {
+
+    private String indentChar = " ";
+
+    /**
+     * Creates a new instance of {@link JsonDependencyNodeVisitor}. The writer 
will be used to write the output.
+     * @param writer  the writer to write to
+     */
+    public JsonDependencyNodeVisitor(Writer writer) {
+        super(writer);
+    }
+
+    @Override
+    public boolean visit(DependencyNode node) {
+        if (node.getParent() == null || node.getParent() == node) {
+            writeRootNode(node, writer);
+        }
+        return true;
+    }
+
+    /**
+     * Writes the node to the writer. This method is recursive and will write 
all children nodes.
+     * @param node  the node to write
+     * @param writer  the writer to write to
+     */
+    private void writeRootNode(DependencyNode node, Writer writer) {
+        int indent = 2;
+        StringBuilder sb = new StringBuilder();
+        sb.append("{").append("\n");
+        writeNode(indent, node, sb);
+        sb.append("}").append("\n");
+        try {
+            writer.write(sb.toString());
+        } catch (IOException e) {
+            throw new RuntimeException("Error while writing json output", e);

Review Comment:
   What about just not catch an exception here and leaving this business to the 
callers.... fixing the PrintWriter could then be done in another patch and not 
here.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to