Skip to main content

[jsonp~git:9477829c] Adding a demo using JsonPointer to locate json values

  • From:
  • To:
  • Subject: [jsonp~git:9477829c] Adding a demo using JsonPointer to locate json values
  • Date: Tue, 22 Oct 2013 23:29:18 +0000

Project:    jsonp
Repository: git
Revision:   9477829c87e3a1e13296c2342618ee6f341277bf
Author:     jitu
Date:       2013-10-22 23:13:29 UTC
Link:       

Log Message:
------------
Adding a demo using JsonPointer to locate json values



Revisions:
----------
9477829c87e3a1e13296c2342618ee6f341277bf


Modified Paths:
---------------
demos/pom.xml


Added Paths:
------------
demos/jsonpointer/pom.xml
demos/jsonpointer/src/main/java/org/glassfish/jsondemos/jsonpointer/JsonpointerDemo.java
demos/jsonpointer/src/main/resources/jsonpointer.json
demos/jsonpointer/src/main/resources/wiki.json


Diffs:
------
--- /dev/null
+++ b/demos/jsonpointer/pom.xml
@@ -0,0 +1,101 @@
+<!--
+
+    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+
+    Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
+
+    The contents of this file are subject to the terms of either the GNU
+    General Public License Version 2 only ("GPL") or the Common Development
+    and Distribution License("CDDL") (collectively, the "License").  You
+    may not use this file except in compliance with the License.  You can
+    obtain a copy of the License at
+    https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
+    or packager/legal/LICENSE.txt.  See the License for the specific
+    language governing permissions and limitations under the License.
+
+    When distributing the software, include this License Header Notice in 
each
+    file and include the License file at packager/legal/LICENSE.txt.
+
+    GPL Classpath Exception:
+    Oracle designates this particular file as subject to the "Classpath"
+    exception as provided by Oracle in the GPL Version 2 section of the 
License
+    file that accompanied this code.
+
+    Modifications:
+    If applicable, add the following below the License Header, with the 
fields
+    enclosed by brackets [] replaced by your own identifying information:
+    "Portions Copyright [year] [name of copyright owner]"
+
+    Contributor(s):
+    If you wish your version of this file to be governed by only the CDDL or
+    only the GPL Version 2, indicate your decision by adding "[Contributor]
+    elects to include this software in this distribution under the [CDDL or 
GPL
+    Version 2] license."  If you don't indicate a single choice of license, a
+    recipient has the option to distribute your version of this file under
+    either the CDDL, the GPL Version 2 or to extend the choice of license to
+    its licensees as provided above.  However, if you add GPL Version 2 code
+    and therefore, elected the GPL Version 2 license, then the option applies
+    only if the new code is made subject to such option by the copyright
+    holder.
+
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" ;
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 ;
http://maven.apache.org/maven-v4_0_0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.glassfish.jsonp</groupId>
+        <artifactId>demos</artifactId>
+        <version>1.0.4-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <packaging>jar</packaging>
+    <url>http://maven.apache.org</url>
+    <artifactId>jsondemos-jsonpointer</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>javax.json</groupId>
+            <artifactId>javax.json-api</artifactId>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish</groupId>
+            <artifactId>javax.json</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-compiler-plugin</artifactId>
+                               <version>2.5.1</version>
+                               <configuration>
+                                       <source>1.7</source>
+                                       <target>1.7</target>
+                               </configuration>
+                       </plugin>
+<!-- mvn exec:java
+-->
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>exec-maven-plugin</artifactId>
+                <version>1.2.1</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>java</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                               <configuration>
+                                       
<mainClass>org.glassfish.jsondemos.jsonpointer.JsonPointerDemo</mainClass>
+                               </configuration>
+                       </plugin>
+        </plugins>
+    </build>
+</project>--- /dev/null
+++ 
b/demos/jsonpointer/src/main/java/org/glassfish/jsondemos/jsonpointer/JsonpointerDemo.java
@@ -0,0 +1,161 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common Development
+ * and Distribution License("CDDL") (collectively, the "License").  You
+ * may not use this file except in compliance with the License.  You can
+ * obtain a copy of the License at
+ * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
+ * or packager/legal/LICENSE.txt.  See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ * When distributing the software, include this License Header Notice in each
+ * file and include the License file at packager/legal/LICENSE.txt.
+ *
+ * GPL Classpath Exception:
+ * Oracle designates this particular file as subject to the "Classpath"
+ * exception as provided by Oracle in the GPL Version 2 section of the 
License
+ * file that accompanied this code.
+ *
+ * Modifications:
+ * If applicable, add the following below the License Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyright [year] [name of copyright owner]"
+ *
+ * Contributor(s):
+ * If you wish your version of this file to be governed by only the CDDL or
+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
+ * elects to include this software in this distribution under the [CDDL or 
GPL
+ * Version 2] license."  If you don't indicate a single choice of license, a
+ * recipient has the option to distribute your version of this file under
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
+ * its licensees as provided above.  However, if you add GPL Version 2 code
+ * and therefore, elected the GPL Version 2 license, then the option applies
+ * only if the new code is made subject to such option by the copyright
+ * holder.
+ */
+
+package org.glassfish.jsondemos.jsonpointer;
+
+import javax.json.*;
+import java.io.*;
+
+/**
+ * JsonPointer (http://tools.ietf.org/html/rfc6901) demo with object model 
API
+ *
+ * @author Jitendra Kotamraju
+ */
+public class JsonpointerDemo {
+
+    public static void main(String... args) throws Exception {
+        testWiki();
+        testPointer();
+    }
+
+    private static void testWiki() throws IOException {
+        try (InputStream is = 
JsonpointerDemo.class.getResourceAsStream("/wiki.json");
+             JsonReader rdr = Json.createReader(is)) {
+            JsonObject person = rdr.readObject();
+
+            assertEquals("NY", getString(person, "/address/state"));
+            assertEquals("212 555-1234", getString(person, 
"/phoneNumber/0/number"));
+        }
+    }
+
+    private static void testPointer() throws IOException {
+        try (InputStream is = 
JsonpointerDemo.class.getResourceAsStream("/jsonpointer.json");
+             JsonReader rdr = Json.createReader(is)) {
+            JsonObject root = rdr.readObject();
+
+            assertEquals(root, get(root, ""));
+            assertEquals(root.get("foo"), get(root, "/foo"));
+            assertEquals(root.getJsonArray("foo").get(0), get(root, 
"/foo/0"));
+            assertEquals(root.get(""), get(root, "/"));
+            assertEquals(root.get("a/b"), get(root, "/a~1b"));
+            assertEquals(root.get("c%d"), get(root, "/c%d"));
+            assertEquals(root.get("e^f"), get(root, "/e^f"));
+            assertEquals(root.get("k\"l"), get(root, "/k\"l"));
+            assertEquals(root.get("i\\j"), get(root, "/i\\j"));
+            assertEquals(root.get(" "), get(root, "/ "));
+            assertEquals(root.get("m~n"), get(root, "/m~0n"));
+
+            // Adding a parent to current root and try with it
+            JsonObject doc = Json.createObjectBuilder().add("doc", 
root).build();
+            root = doc.getJsonObject("doc");
+            assertEquals(doc, get(doc, ""));
+            assertEquals(root.get("foo"), get(doc, "/doc/foo"));
+            assertEquals(root.getJsonArray("foo").get(0), get(doc, 
"/doc/foo/0"));
+            assertEquals(root.get(""), get(doc, "/doc/"));
+            assertEquals(root.get("a/b"), get(doc, "/doc/a~1b"));
+            assertEquals(root.get("c%d"), get(doc, "/doc/c%d"));
+            assertEquals(root.get("e^f"), get(doc, "/doc/e^f"));
+            assertEquals(root.get("k\"l"), get(doc, "/doc/k\"l"));
+            assertEquals(root.get("i\\j"), get(doc, "/doc/i\\j"));
+            assertEquals(root.get(" "), get(doc, "/doc/ "));
+            assertEquals(root.get("m~n"), get(doc, "/doc/m~0n"));
+        }
+    }
+
+    private static String getString(JsonValue root, String pointer) {
+        return ((JsonString)get(root, pointer)).getString();
+    }
+
+    private static JsonValue get(JsonValue root, String pointer) {
+        if (pointer.isEmpty()) {
+            return root;
+        }
+        if (pointer.charAt(0) != '/') {
+            throw new IllegalArgumentException(
+                    "JsonPointer "+pointer+" doesn't start with /");
+        }
+
+        StringBuilder referenceToken = new StringBuilder();
+        for(int i=1; i < pointer.length(); i++) {   // 1 to skip first /
+            char ch = pointer.charAt(i);
+            if (ch == '/') {
+                return get(newRoot(root, referenceToken.toString()), 
pointer.substring(i));
+            } else if (ch == '~') {
+                // handle escaping ~0, ~1
+                if (i+1 == pointer.length()) {
+                    throw new IllegalArgumentException("Illegal escaping: 
expected ~0 or ~1, but got only ~ in pointer="+pointer);
+                }
+                ch = pointer.charAt(++i);
+                if (ch == '0') {
+                    referenceToken.append('~');
+                } else if (ch == '1') {
+                    referenceToken.append('/');
+                } else {
+                    throw new IllegalArgumentException("Illegal escaping: 
expected ~0 or ~1, but got ~"+ch+" in pointer="+pointer);
+                }
+            } else {
+                referenceToken.append(ch);
+            }
+        }
+        return newRoot(root, referenceToken.toString());
+    }
+
+    private static JsonValue newRoot(JsonValue root, String referenceToken) {
+        if (root instanceof JsonObject) {
+            return ((JsonObject)root).get(referenceToken);
+        } else if (root instanceof JsonArray) {
+            return ((JsonArray)root).get(Integer.parseInt(referenceToken));
+        }
+        throw new IllegalArgumentException("Illegal reference 
token="+referenceToken+" for value="+root);
+    }
+
+    private static void assertEquals(JsonValue exp, JsonValue got) {
+        if (exp != got) {
+            throw new RuntimeException("Expected = "+exp+" but got = "+got);
+        }
+    }
+
+    private static void assertEquals(String exp, String got) {
+        if (!exp.equals(got)) {
+            throw new RuntimeException("Expected = "+exp+" but got = "+got);
+        }
+    }
+
+}--- /dev/null
+++ b/demos/jsonpointer/src/main/resources/jsonpointer.json
@@ -0,0 +1,12 @@
+{
+  "foo": ["bar", "baz"],
+  "": 0,
+  "a/b": 1,
+  "c%d": 2,
+  "e^f": 3,
+  "g|h": 4,
+  "i\\j": 5,
+  "k\"l": 6,
+  " ": 7,
+  "m~n": 8
+}--- /dev/null
+++ b/demos/jsonpointer/src/main/resources/wiki.json
@@ -0,0 +1,21 @@
+{
+     "firstName": "John",
+     "lastName": "Smith",
+     "age": 25,
+     "address": {
+         "streetAddress": "21 2nd Street",
+         "city": "New York",
+         "state": "NY",
+         "postalCode": "10021"
+     },
+     "phoneNumber": [
+         {
+           "type": "home",
+           "number": "212 555-1234"
+         },
+         {
+           "type": "fax",
+           "number": "646 555-4567"
+         }
+     ]
+}--- a/demos/pom.xml
+++ b/demos/pom.xml
@@ -72,5 +72,6 @@
                <module>jaxrs</module>
                <module>twitter</module>
                <module>facebook</module>
+               <module>jsonpointer</module>
     </modules>
 </project>





[jsonp~git:9477829c] Adding a demo using JsonPointer to locate json values

jitu 10/22/2013
 
 
Close
loading
Please Confirm
Close