jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r1552599 - in /jena/trunk/jena-arq/src: main/java/org/apache/jena/atlas/json/JsonBuilder.java test/java/org/apache/jena/atlas/json/TestJsonBuilder.java
Date Fri, 20 Dec 2013 11:54:10 GMT
Author: andy
Date: Fri Dec 20 11:54:10 2013
New Revision: 1552599

URL: http://svn.apache.org/r1552599
Log:
Labels to start/finish of object building to help find alignment errors.

Modified:
    jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonBuilder.java
    jena/trunk/jena-arq/src/test/java/org/apache/jena/atlas/json/TestJsonBuilder.java

Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonBuilder.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonBuilder.java?rev=1552599&r1=1552598&r2=1552599&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonBuilder.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonBuilder.java Fri Dec
20 11:54:10 2013
@@ -22,6 +22,7 @@ import java.math.BigDecimal ;
 import java.util.ArrayDeque ;
 import java.util.Deque ;
 
+import org.apache.jena.atlas.lib.Lib ;
 import org.apache.jena.atlas.logging.Log ;
 
 /* Builder pattern for JSON.
@@ -29,50 +30,19 @@ import org.apache.jena.atlas.logging.Log
  */
 public class JsonBuilder {
 
-    public static void main(String... argv) throws Exception {
-
-        // c.f. JSWriter
-        
-        JsonBuilder builder = new JsonBuilder() ;
-        builder.startArray().value(3).value(4).finishArray() ;
-        JsonValue value1 = builder.build() ;
-        JSON.write(value1) ;
-        System.out.println() ;
-        System.out.println() ;
-        
-        builder.reset() ;
-        builder.startObject() ;
-
-        builder.key("bar").value("foo") ;
-        builder.key("bar2").value(5) ;
-        builder.key("bar3").startObject().key("inner1").value(true).key("inner2").value(false).finishObject()
;
-
-        builder.key("outer") ;
-        builder.startObject().key("array").startArray().value(3).value(4).finishArray().finishObject()
;
-
-        builder.finishObject() ;
-
-        JsonValue value2 = builder.build() ;
-        JSON.write(value2) ;
-        System.out.println() ;
-        System.out.println() ;
-        System.out.println("DONE") ;
-
-    }
-
-    // =============
-
     // If not an array or object.
-    private JsonValue         builtValue   = null ;
-    private Deque<JsonArray>  arrays  = new ArrayDeque<JsonArray>() ;
-    private Deque<JsonObject> objects = new ArrayDeque<JsonObject>() ;
+    private JsonValue         builtValue    = null ;
+    private static final String NoMarker    = "" ;
+    private Deque<String>     markers       = new ArrayDeque<String>() ;
+    private Deque<JsonArray>  arrays        = new ArrayDeque<JsonArray>() ;
+    private Deque<JsonObject> objects       = new ArrayDeque<JsonObject>() ;
 
     private static enum State {
         ARRAY, OBJECT
     }
     private Deque<State>  stack = new ArrayDeque<State>() ;
 
-    // The depth of this stack is the object depth.
+    // The depth of this stack is the object depth. key: { key: ... } 
     private Deque<String> keys  = new ArrayDeque<String>() ;
 
     public JsonBuilder() {
@@ -96,13 +66,18 @@ public class JsonBuilder {
         arrays.clear(); 
     }
 
-    public JsonBuilder startObject() {
+    public JsonBuilder startObject() { return startObject(NoMarker) ; }
+    
+    public JsonBuilder startObject(String startMarker) {
+        markers.push(startMarker); 
         objects.push(new JsonObject()) ;
         stack.push(State.OBJECT) ;
         return this ;
     }
 
-    public JsonBuilder finishObject() {
+    public JsonBuilder finishObject() { return finishObject(NoMarker) ; }
+
+    public JsonBuilder finishObject(String finishMarker) {
         if ( stack.isEmpty() )
             throw new JsonException("Alignment error : already built outer most object or
array") ; 
         State state = stack.pop() ;
@@ -112,6 +87,9 @@ public class JsonBuilder {
         maybeObjectOrArray(value) ;
         if ( stack.isEmpty() )
             builtValue = value ;
+        String startMarker = markers.pop(); 
+        if ( ! Lib.equal(startMarker, finishMarker) )
+            throw new JsonException("JSON build error : start/finish alignment error: start="+startMarker+"
 finish="+finishMarker) ;
         return this ;
     }
 

Modified: jena/trunk/jena-arq/src/test/java/org/apache/jena/atlas/json/TestJsonBuilder.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/org/apache/jena/atlas/json/TestJsonBuilder.java?rev=1552599&r1=1552598&r2=1552599&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/org/apache/jena/atlas/json/TestJsonBuilder.java (original)
+++ jena/trunk/jena-arq/src/test/java/org/apache/jena/atlas/json/TestJsonBuilder.java Fri
Dec 20 11:54:10 2013
@@ -29,8 +29,8 @@ public class TestJsonBuilder extends Bas
     @Test public void jsonBuild01() {
         JsonValue x = JSON.parseAny("{ }") ;
         JsonBuilder builder = new JsonBuilder() ;
-        builder.startObject() ;
-        builder.finishObject() ;
+        builder.startObject("A") ;
+        builder.finishObject("A") ;
         JsonValue v = builder.build() ;
         assertEquals(x,v) ;
     }
@@ -38,10 +38,10 @@ public class TestJsonBuilder extends Bas
     @Test public void jsonBuild02() {
         JsonValue x = JSON.parseAny("{ a: 'A', b:'B'}") ;
         JsonBuilder builder = new JsonBuilder() ;
-        builder.startObject() ;
+        builder.startObject("Obj1") ;
         builder.key("a").value("A") ;
         builder.key("b").value("B") ;
-        builder.finishObject() ;
+        builder.finishObject("Obj1") ;
         JsonValue v = builder.build() ;
         assertEquals(x,v) ;
     }
@@ -100,4 +100,12 @@ public class TestJsonBuilder extends Bas
         builder.startObject() ;
         builder.finishArray() ;
     }
+    
+    @Test(expected=JsonException.class)
+    public void jsonBuildErr03() {
+        JsonBuilder builder = new JsonBuilder() ;
+        builder.startObject("A") ;
+        builder.finishObject("B") ;
+    }
+
 }



Mime
View raw message