You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2013/11/09 01:23:34 UTC
svn commit: r1540229 - in
/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io:
JSWriter.java JsonWriter.java
Author: andy
Date: Sat Nov 9 00:23:34 2013
New Revision: 1540229
URL: http://svn.apache.org/r1540229
Log:
Better JSON pretty printing
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JsonWriter.java
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java?rev=1540229&r1=1540228&r2=1540229&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java Sat Nov 9 00:23:34 2013
@@ -39,6 +39,8 @@ import org.apache.jena.atlas.lib.Ref ;
*/
public class JSWriter {
+ // Isn't this just a weird builder?
+ // This is broken for arrays for key then startObject
protected IndentedWriter out = IndentedWriter.stdout ;
public JSWriter() {
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JsonWriter.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JsonWriter.java?rev=1540229&r1=1540228&r2=1540229&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JsonWriter.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JsonWriter.java Sat Nov 9 00:23:34 2013
@@ -28,9 +28,6 @@ import org.apache.jena.atlas.json.* ;
public class JsonWriter implements JsonVisitor
{
- // Use JSWriter????!!!! Multiline control is tricky then.
- // Or is it? Because we know the length of things
-
IndentedWriter out ;
public JsonWriter() { this(IndentedWriter.stdout) ; }
@@ -45,33 +42,79 @@ public class JsonWriter implements JsonV
private static String ArrayFinish = "]" ;
private static String ArraySep = "," ;
- private static String ObjectStart = "{ " ;
+ private static String ObjectStart = "{" ;
private static String ObjectFinish = "}" ;
private static String ObjectSep = " ," ;
private static String ObjectPairSep = " : " ;
-
- // Make "unnecessary" space
private static String SPC = " " ;
+ // Number of key/value pairs in an object before it is always written long form.
+ // It may still written long form if any of the values are consider complicated.
+ private static int maxCompactObject = 1 ;
+ // Number of array elements before an array is always written long form.
+ // It may still written long form if any of the elements are consider complicated.
+ private static int maxCompactArray = 1 ;
+
@Override
public void visit(JsonObject jsonObject)
{
- out.print(ObjectStart) ;
- out.incIndent() ;
boolean first = true ;
+ Set<String> x = jsonObject.keySet() ;
// Sort keys.
+ out.print(ObjectStart) ;
+ if ( x.size() == 0 ) {
+ out.print(SPC) ;
+ out.print(ObjectFinish) ;
+ return ;
+ }
+
+ out.incIndent() ;
+
+ if ( isJsonObjectCompact(jsonObject) )
+ writeObjectCompact(jsonObject, x) ;
+ else
+ writeObjectLong(jsonObject, x) ;
+ out.decIndent() ;
+ out.print(ObjectFinish) ;
+ }
+
+ private static boolean isJsonObjectCompact(JsonObject jsonObject) {
Set<String> x = jsonObject.keySet() ;
- SortedSet<String> y = new TreeSet<String>(x) ;
+ if ( x.size() == 0 ) return true ;
+ if ( x.size() > maxCompactObject ) return false ;
- boolean multiLine = x.size() > 1 ;
+ for ( String k : x ) {
+ if ( ! isCompactValue(jsonObject.get(k)) )
+ return false ;
+ }
+ return true ;
+ }
+ private void writeObjectCompact(JsonObject jsonObject, Set<String> x) {
+ SortedSet<String> y = new TreeSet<String>(x) ;
+ boolean first = true ;
+ for ( String k : y ) {
+ if ( ! first )
+ out.print(ObjectSep) ;
+ first = false ;
+ JSWriter.outputQuotedString(out, k) ;
+ out.print(ObjectPairSep) ;
+ out.incIndent() ;
+ jsonObject.get(k).visit(this) ;
+ out.decIndent() ;
+ }
+ out.print(SPC) ;
-
- for ( String k : y )
- {
- if ( ! first ) {
+ }
+
+ private void writeObjectLong(JsonObject jsonObject, Set<String> x) {
+ SortedSet<String> y = new TreeSet<String>(x) ;
+ boolean first = true ;
+ //out.println();
+ for ( String k : y ) {
+ if ( ! first ) {
out.print(ObjectSep) ;
- out.println() ;
+ out.println() ;
}
first = false ;
JSWriter.outputQuotedString(out, k) ;
@@ -80,47 +123,100 @@ public class JsonWriter implements JsonV
jsonObject.get(k).visit(this) ;
out.decIndent() ;
}
- out.decIndent() ;
- if ( multiLine )
- out.println() ;
- else
- out.print(SPC) ;
- out.print(ObjectFinish) ;
- out.print(SPC) ;
-
+ out.println();
}
-
+
+ private static boolean isCompactValue(JsonValue v) {
+ if ( v.isPrimitive()) return true ;
+ if ( v.isArray() ) {
+ JsonArray a = v.getAsArray() ;
+ if ( a.size() == 0 ) return true ;
+ if ( a.size() > 1 ) return false ;
+ return a.get(0).isPrimitive() ;
+ }
+ if ( v.isObject() ) {
+ JsonObject obj = v.getAsObject() ;
+ Set<String> x = obj.keySet() ;
+ if ( x.size() == 0 )
+ return true ;
+ if ( x.size() > 1 )
+ return false ;
+ String k = obj.keys().iterator().next();
+ return obj.get(k).isPrimitive() ;
+ }
+ return false ;
+ }
+
@Override
public void visit(JsonArray jsonArray)
{
- boolean multiLine = (jsonArray.size() > 1 ) ;
- if ( multiLine )
- out.ensureStartOfLine() ;
+ if ( jsonArray.size() == 0 ) {
+ out.print(ArrayStart) ;
+ out.incIndent() ;
+ out.print(ArrayFinish) ;
+ out.decIndent() ;
+ return ;
+ }
+
+ if ( isJsonArrayCompact(jsonArray) )
+ writeArrayCompact(jsonArray) ;
+ else
+ writeArrayLong(jsonArray) ;
+ }
+
+ private boolean isJsonArrayCompact(JsonArray jsonArray) {
+ if ( jsonArray.size() > maxCompactArray ) return false ;
+ for ( int i = 0 ; i < jsonArray.size() ; i++ ) {
+ if ( ! jsonArray.get(i).isPrimitive() )
+ return false ;
+ }
+ return true ;
+ }
+
+ private void writeArrayCompact(JsonArray jsonArray) {
+ out.print(ArrayStart) ;
+ out.incIndent() ;
+ boolean first = true ;
+
+ for ( JsonValue elt : jsonArray )
+ {
+ if ( ! first ) {
+ out.print(ArraySep) ;
+ out.print(SPC) ;
+ }
+ first = false ;
+ elt.visit(this) ;
+
+ }
+ out.print(SPC) ;
+ out.decIndent() ;
+ out.print(ArrayFinish) ;
+ }
+ private void writeArrayLong(JsonArray jsonArray) {
+ //out.println() ;
out.print(ArrayStart) ;
+
out.incIndent() ;
+ out.ensureStartOfLine() ;
boolean first = true ;
for ( JsonValue elt : jsonArray )
{
if ( ! first )
{
+ out.print(SPC) ;
out.print(ArraySep) ;
out.println() ;
- multiLine = true ;
}
first = false ;
elt.visit(this) ;
- out.print(SPC) ;
}
out.decIndent() ;
- if ( multiLine )
- out.ensureStartOfLine() ;
-// else
-// out.print(SPC) ;
+ out.ensureStartOfLine() ;
out.print(ArrayFinish) ;
}
-
+
@Override
public void visit(JsonString jsonString)
{