incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r759723 - in /incubator/sling/trunk/bundles/commons/json/src: main/java/org/apache/sling/commons/json/util/Validator.java test/java/org/apache/sling/commons/json/util/ test/java/org/apache/sling/commons/json/util/ValidatorTest.java
Date Sun, 29 Mar 2009 16:43:56 GMT
Author: cziegeler
Date: Sun Mar 29 16:43:56 2009
New Revision: 759723

URL: http://svn.apache.org/viewvc?rev=759723&view=rev
Log:
SLING-901 : Add strict json validator.

Added:
    incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
  (with props)
    incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/
    incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
  (with props)

Added: incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java?rev=759723&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
(added)
+++ incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
Sun Mar 29 16:43:56 2009
@@ -0,0 +1,125 @@
+/*
+ * 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.sling.commons.json.util;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONTokener;
+
+/**
+ * Utility class for validating JSON text.
+ */
+public class Validator {
+
+    /**
+     * Strictly validate the JSON text
+     * @param text The text to check.
+     * @throws JSONException If the text is not valid.
+     */
+    public static void validate(final String text)
+    throws JSONException {
+        validate(new JSONTokener(text));
+    }
+
+    /**
+     * Strictly validate the JSON text
+     * @param x The tokener to check.
+     * @throws JSONException If the text is not valid.
+     */
+    public static void validate(JSONTokener x)
+    throws JSONException {
+        char c = x.nextClean();
+        if ( c == 0 ) {
+            // no tokens at all - we consider this valid
+            return;
+        } else  if (c == '[') {
+            if (x.nextClean() == ']') {
+                return;
+            }
+            x.back();
+            for (;;) {
+                if (x.nextClean() == ',') {
+                    x.back();
+                } else {
+                    x.back();
+                    c = x.nextClean();
+                    x.back();
+                    if ( c == '{' || c == '[') {
+                        // recursiv validation for object and array
+                        validate(x);
+                    } else {
+                        x.nextValue();
+                    }
+                }
+                switch (x.nextClean()) {
+                    case ',': if (x.nextClean() == ']') {
+                                  throw x.syntaxError("Trailing separator ',' in array.");
+                              }
+                              x.back();
+                              break;
+                    case ']': return;
+                    default:  throw x.syntaxError("Expected a ',' or ']'");
+                }
+            }
+        } else if ( c == '{') {
+            final Set<String> keys = new HashSet<String>();
+            for (;;) {
+                String key;
+                c = x.nextClean();
+                switch (c) {
+                    case 0  : throw x.syntaxError("A JSONObject text must end with '}'");
+                    case '}': return;
+                    default : x.back();
+                              key = x.nextValue().toString();
+                }
+
+                c = x.nextClean();
+                if (c != ':') {
+                    throw x.syntaxError("Expected a ':' after a key");
+                }
+                if ( keys.contains(key) ) {
+                    throw x.syntaxError("JSONObject contains key '" + key + "' multiple times.");
+                }
+                keys.add(key);
+                // get the value
+                c = x.nextClean();
+                x.back();
+                if ( c == '{' || c == '[') {
+                    // recursiv validation for object and array
+                    validate(x);
+                } else {
+                    x.nextValue();
+                }
+
+                switch (x.nextClean()) {
+                    case ',': if (x.nextClean() == '}') {
+                                  throw x.syntaxError("Trailing separator ',' in object.");
+                              }
+                              x.back();
+                              break;
+                    case '}': return;
+                    default:  throw x.syntaxError("Expected a ',' or '}'");
+                }
+            }
+
+        } else {
+            throw x.syntaxError("A JSON text must begin with '{' (for an object) or '[' (for
an array).");
+        }
+    }
+}

Propchange: incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java?rev=759723&view=auto
==============================================================================
--- incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
(added)
+++ incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
Sun Mar 29 16:43:56 2009
@@ -0,0 +1,64 @@
+/*
+ * 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.sling.commons.json.util;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.commons.json.JSONException;
+
+/**
+ * Test the Validator.
+ */
+public class ValidatorTest extends TestCase {
+
+    public void testSimpleJSON()
+    throws JSONException {
+        Validator.validate("");
+        Validator.validate("[]");
+        Validator.validate("{}");
+    }
+
+    public void testBasicJSON()
+    throws JSONException {
+        Validator.validate("[1,true,\"hallo\"]");
+        Validator.validate("{a:\"you\", b:2, c:true}");
+    }
+
+    public void testNestedJSON()
+    throws JSONException {
+        Validator.validate("[1,true,\"hallo\", {a:1}, [1,2]]");
+        Validator.validate("{a:\"you\", b:2, c:true, d: {d:1}, e: []}");
+    }
+
+    /**
+     * These tests are supposed to fail!
+     */
+    public void testTrailingChars() {
+        try {
+            Validator.validate("[1,true,\"hallo\",]");
+            assertTrue("Trailing separator should not be allowed.", false);
+        } catch (JSONException e) {
+            // ignore
+        }
+        try {
+            Validator.validate("{a:\"you\", b:2, c:true,}");
+            assertTrue("Trailing separator should not be allowed.", false);
+        } catch (JSONException e) {
+            // ignore
+        }
+    }
+}

Propchange: incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message