Skip to content
This repository
Browse code

Ingested org.json classes as de.monoid.json, cleaned them up

  • Loading branch information...
commit 2613fdeda5340371c1025880d701eb99bb5d9cd6 1 parent 6b0324f
Jochen Bedersdorfer authored

Showing 56 changed files with 7,105 additions and 73 deletions. Show diff stats Hide diff stats

  1. BIN  .DS_Store
  2. +0 2  .classpath
  3. +6 1 .project
  4. +23 0 .project~
  5. +6 0 .settings/org.eclipse.jdt.core.prefs
  6. +3 0  .settings/org.eclipse.ltk.core.refactoring.prefs
  7. +8 0 .settings/org.maven.ide.eclipse.prefs
  8. +23 0 NOTICE
  9. +6 2 README.rst
  10. +83 0 README.rst~
  11. +20 9 pom.xml
  12. +279 0 src/main/java/de/monoid/json/CDL.java
  13. +170 0 src/main/java/de/monoid/json/Cookie.java
  14. +90 0 src/main/java/de/monoid/json/CookieList.java
  15. +163 0 src/main/java/de/monoid/json/HTTP.java
  16. +78 0 src/main/java/de/monoid/json/HTTPTokener.java
  17. +948 0 src/main/java/de/monoid/json/JSONArray.java
  18. +31 0 src/main/java/de/monoid/json/JSONException.java
  19. +461 0 src/main/java/de/monoid/json/JSONML.java
  20. +2,078 0 src/main/java/de/monoid/json/JSONObject.java
  21. +18 0 src/main/java/de/monoid/json/JSONString.java
  22. +78 0 src/main/java/de/monoid/json/JSONStringer.java
  23. +435 0 src/main/java/de/monoid/json/JSONTokener.java
  24. +323 0 src/main/java/de/monoid/json/JSONWriter.java
  25. +678 0 src/main/java/de/monoid/json/Test.java
  26. +441 0 src/main/java/de/monoid/json/XML.java
  27. +366 0 src/main/java/de/monoid/json/XMLTokener.java
  28. +4 0 src/main/java/de/monoid/web/AbstractResource.java
  29. +47 0 src/main/java/de/monoid/web/BinaryResource.java
  30. +2 2 src/main/java/de/monoid/web/JSONPathQuery.java
  31. +5 6 src/main/java/de/monoid/web/JSONResource.java
  32. +54 1 src/main/java/de/monoid/web/Resty.java
  33. +2 1  src/main/java/de/monoid/web/XMLResource.java
  34. +2 2 src/main/java/de/monoid/web/jp/javacc/AbstractJSONExpr.java
  35. +2 2 src/main/java/de/monoid/web/jp/javacc/Predicates.java
  36. +1 1  src/main/java/de/monoid/web/jp/javacc/json.jjt
  37. +25 0 src/main/java/de/monoid/web/package.html
  38. +29 0 src/test/java/de/monoid/web/RestyGoogleTest.java
  39. +3 4 src/test/java/de/monoid/web/jp/javacc/JSONPathCompilerTest.java
  40. BIN  target/resty-0.0.1-SNAPSHOT.jar
  41. BIN  target/resty-0.0.2-SNAPSHOT.jar
  42. BIN  target/resty-0.1.0.jar
  43. +1 1  target/site-webapp/WEB-INF/web.xml
  44. +6 6 target/site/apidocs/de/monoid/web/JSONResource.html
  45. +2 2 target/site/apidocs/de/monoid/web/jp/javacc/JSONPathCompiler.JSONPathExpr.html
  46. +1 1  target/site/apidocs/de/monoid/web/jp/javacc/JSONPathCompilerConstants.html
  47. +5 5 target/site/apidocs/de/monoid/web/jp/javacc/Predicates.Test.html
  48. +2 2 target/site/apidocs/index-all.html
  49. +70 0 target/surefire-reports/TEST-de.monoid.web.RestyGoogleTest.xml
  50. +8 8 target/surefire-reports/TEST-de.monoid.web.RestyTest.xml
  51. +7 7 target/surefire-reports/TEST-de.monoid.web.RestyXmlTest.xml
  52. +5 5 target/surefire-reports/TEST-de.monoid.web.jp.javacc.JSONPathCompilerTest.xml
  53. +4 0 target/surefire-reports/de.monoid.web.RestyGoogleTest.txt
  54. +1 1  target/surefire-reports/de.monoid.web.RestyTest.txt
  55. +1 1  target/surefire-reports/de.monoid.web.RestyXmlTest.txt
  56. +1 1  target/surefire-reports/de.monoid.web.jp.javacc.JSONPathCompilerTest.txt
BIN  .DS_Store
Binary file not shown
2  .classpath
@@ -6,7 +6,5 @@
6 6 <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
7 7 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
8 8 <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
9   - <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
10   - <classpathentry combineaccessrules="false" kind="src" path="/json.org"/>
11 9 <classpathentry kind="output" path="target/classes"/>
12 10 </classpath>
7 .project
@@ -11,13 +11,18 @@
11 11 </arguments>
12 12 </buildCommand>
13 13 <buildCommand>
  14 + <name>org.ide.eclipse.maven2Builder</name>
  15 + <arguments>
  16 + </arguments>
  17 + </buildCommand>
  18 + <buildCommand>
14 19 <name>org.maven.ide.eclipse.maven2Builder</name>
15 20 <arguments>
16 21 </arguments>
17 22 </buildCommand>
18 23 </buildSpec>
19 24 <natures>
20   - <nature>org.eclipse.jdt.core.javanature</nature>
21 25 <nature>org.maven.ide.eclipse.maven2Nature</nature>
  26 + <nature>org.eclipse.jdt.core.javanature</nature>
22 27 </natures>
23 28 </projectDescription>
23 .project~
... ... @@ -0,0 +1,23 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<projectDescription>
  3 + <name>resty</name>
  4 + <comment></comment>
  5 + <projects>
  6 + </projects>
  7 + <buildSpec>
  8 + <buildCommand>
  9 + <name>de.monoid.eclipse.jdt.core.javabuilder</name>
  10 + <arguments>
  11 + </arguments>
  12 + </buildCommand>
  13 + <buildCommand>
  14 + <name>de.monoid.maven.ide.eclipse.maven2Builder</name>
  15 + <arguments>
  16 + </arguments>
  17 + </buildCommand>
  18 + </buildSpec>
  19 + <natures>
  20 + <nature>de.monoid.eclipse.jdt.core.javanature</nature>
  21 + <nature>de.monoid.maven.ide.eclipse.maven2Nature</nature>
  22 + </natures>
  23 +</projectDescription>
6 .settings/org.eclipse.jdt.core.prefs
... ... @@ -0,0 +1,6 @@
  1 +#Mon Dec 13 16:17:13 PST 2010
  2 +eclipse.preferences.version=1
  3 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
  4 +org.eclipse.jdt.core.compiler.compliance=1.5
  5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
  6 +org.eclipse.jdt.core.compiler.source=1.5
3  .settings/org.eclipse.ltk.core.refactoring.prefs
... ... @@ -0,0 +1,3 @@
  1 +#Sun Jan 02 15:58:12 PST 2011
  2 +eclipse.preferences.version=1
  3 +org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
8 .settings/org.maven.ide.eclipse.prefs
... ... @@ -0,0 +1,8 @@
  1 +#Mon Dec 13 16:17:13 PST 2010
  2 +activeProfiles=
  3 +eclipse.preferences.version=1
  4 +fullBuildGoals=process-test-resources
  5 +resolveWorkspaceProjects=true
  6 +resourceFilterGoals=process-resources resources\:testResources
  7 +skipCompilerPlugin=true
  8 +version=1
23 NOTICE
... ... @@ -0,0 +1,23 @@
  1 +Resty - licensed under the Apache 2.0 license
  2 +is using the following software:
  3 +
  4 +org.json package for Java, repackaged as de.monoid.json
  5 +Copyright (c) 2002 JSON.org
  6 +
  7 +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  8 +to deal in the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  10 +and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11 +
  12 +The above copyright notice and this permission notice shall be included in all copies or
  13 +substantial portions of the Software.
  14 +
  15 +The Software shall be used for Good, not Evil.
  16 +
  17 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  18 + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  19 + PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20 + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21 + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22 + OTHER DEALINGS IN THE SOFTWARE.
  23 +
8 README.rst
Source Rendered
@@ -41,8 +41,7 @@ Infancy
41 41
42 42 Installation
43 43 -------------
44   -Either create the JAR yourself or grab the rest-*.jar file and add it to your CLASSPATH.
45   -You need the org.json classes for JSON support.
  44 +Either create the JAR yourself (see target directory or grab the rest-*.jar file and add it to your CLASSPATH.
46 45
47 46
48 47 Compile it yourself
@@ -95,6 +94,9 @@ JSON Sample for paths above:
95 94
96 95 *Chaining calls to navigate JSON objects. This is useful if the JSON contains URIs to go down the rabbit hole so to say*::
97 96
  97 + import static de.monoid.web.Resty.*;
  98 + import de.monoid.web.Resty;
  99 +
98 100 JSONObject json = r.
99 101 json("http://localhost:9999/rest/sc").
100 102 json(path("serviceclients[displayName='Sample'].href")).
@@ -105,4 +107,6 @@ Developers
105 107 ===========
106 108
107 109 - Jochen Bedersdorfer (resty@bedersdorfer.de)
  110 +
  111 +
108 112
83 README.rst~
... ... @@ -0,0 +1,83 @@
  1 +======
  2 +Resty
  3 +======
  4 +
  5 +Introduction
  6 +-------------
  7 +
  8 +Resty is a small, convenient interface to talk to RESTful services from Java.
  9 +
  10 +Its focus is on simplicity and ease-of-use, often requiring only two lines of code to access any web service.
  11 +It supports chaining several requests which is very useful in RESTful application employing HATEOS.
  12 +
  13 +Basic usage is very simple: Create a Resty instance, use authenticate methode to add credentials, then call one of the content type specific methods.
  14 +The idea is that the method name will convey the expected content type you can then operate on.
  15 +
  16 +Here is an example on how to use the geonames web service. It retrieves the json object (see json.org for details) and gets the name of a place from the zip code::
  17 +
  18 + Resty r = new Resty();
  19 + Object name = r.json("http://ws.geonames.org/postalCodeLookupJSON?postalcode=66780&country=DE").get("postalcodes[0].placeName");
  20 + assertEquals(name, "Rehlingen-Siersburg");
  21 +
  22 +Resty supports complex path queries to navigate into a json object.
  23 +
  24 +Status
  25 +-------
  26 +
  27 +Infancy
  28 +- POST, GET for JSON and text/plain
  29 +- Path expressions to access JSON: simple tests on fields with operators >,=,< and full boolean expressions (&&,||,!)
  30 +
  31 +Installation
  32 +-------------
  33 +Either compile yourself or grab the rest-*.jar file and add it to your CLASSPATH.
  34 +No other runtime requirements (so far).
  35 +
  36 +
  37 +Compile
  38 +-------
  39 +Use Maven 2 or 3 to build.
  40 +
  41 +
  42 +Examples
  43 +-----------
  44 +
  45 +Some supported path constructs::
  46 +
  47 + store.book[price>9 && price<12.999].author
  48 + store.book[!category='reference'].author
  49 +
  50 +JSON Sample::
  51 +
  52 + { "store": {
  53 + "book": [
  54 + { "category": "reference",
  55 + "author": "Nigel Rees",
  56 + "title": "Sayings of the Century",
  57 + "price": 8.95
  58 + },
  59 + { "category": "fiction",
  60 + "author": "Evelyn Waugh",
  61 + "title": "Sword of Honour",
  62 + "price": 12.99
  63 + },
  64 + { "category": "fiction",
  65 + "author": "Herman Melville",
  66 + "title": "Moby Dick",
  67 + "isbn": "0-553-21311-3",
  68 + "price": 8.99
  69 + },
  70 + { "category": "fiction",
  71 + "author": "J. R. R. Tolkien",
  72 + "title": "The Lord of the Rings",
  73 + "isbn": "0-395-19395-8",
  74 + "price": 22.99
  75 + }
  76 + ],
  77 + "bicycle": {
  78 + "color": "red",
  79 + "price": 19.95
  80 + }
  81 + }
  82 + }
  83 +
29 pom.xml
@@ -17,6 +17,24 @@
17 17 <scm>
18 18 <url>https://beders@github.com/beders/Resty.git</url>
19 19 </scm>
  20 + <build>
  21 + <plugins>
  22 + <plugin>
  23 + <groupId>org.apache.maven.plugins</groupId>
  24 + <artifactId>maven-gpg-plugin</artifactId>
  25 + <version>1.1</version>
  26 + <executions>
  27 + <execution>
  28 + <id>sign-artifacts</id>
  29 + <phase>verify</phase>
  30 + <goals>
  31 + <goal>sign</goal>
  32 + </goals>
  33 + </execution>
  34 + </executions>
  35 + </plugin>
  36 + </plugins>
  37 + </build>
20 38 <repositories>
21 39 <repository>
22 40 <id>maven2-repository.dev.java.net</id>
@@ -34,24 +52,17 @@
34 52 <scope>compile</scope>
35 53 </dependency>
36 54 <dependency>
37   - <groupId>org.json</groupId>
38   - <artifactId>json</artifactId>
39   - <version>20090211</version>
40   - <type>jar</type>
41   - <scope>runtime</scope>
42   - </dependency>
43   - <dependency>
44 55 <groupId>junit</groupId>
45 56 <artifactId>junit</artifactId>
46 57 <version>4.8.2</version>
47 58 <type>jar</type>
48   - <scope>compile</scope>
  59 + <scope>test</scope>
49 60 </dependency>
50 61 </dependencies>
51 62 <reporting>
52 63 <plugins>
53 64 <plugin>
54   - <groupId>org.apache.maven.plugin</groupId>
  65 + <groupId>de.monoid.apache.maven.plugin</groupId>
55 66 <artifactId>maven-javadoc-plugin</artifactId>
56 67 <version>2.7</version>
57 68 </plugin>
279 src/main/java/de/monoid/json/CDL.java
... ... @@ -0,0 +1,279 @@
  1 +package de.monoid.json;
  2 +
  3 +/*
  4 +Copyright (c) 2002 JSON.org
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy
  7 +of this software and associated documentation files (the "Software"), to deal
  8 +in the Software without restriction, including without limitation the rights
  9 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 +copies of the Software, and to permit persons to whom the Software is
  11 +furnished to do so, subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +The Software shall be used for Good, not Evil.
  17 +
  18 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24 +SOFTWARE.
  25 +*/
  26 +
  27 +/**
  28 + * This provides static methods to convert comma delimited text into a
  29 + * JSONArray, and to covert a JSONArray into comma delimited text. Comma
  30 + * delimited text is a very popular format for data interchange. It is
  31 + * understood by most database, spreadsheet, and organizer programs.
  32 + * <p>
  33 + * Each row of text represents a row in a table or a data record. Each row
  34 + * ends with a NEWLINE character. Each row contains one or more values.
  35 + * Values are separated by commas. A value can contain any character except
  36 + * for comma, unless is is wrapped in single quotes or double quotes.
  37 + * <p>
  38 + * The first row usually contains the names of the columns.
  39 + * <p>
  40 + * A comma delimited list can be converted into a JSONArray of JSONObjects.
  41 + * The names for the elements in the JSONObjects can be taken from the names
  42 + * in the first row.
  43 + * @author JSON.org
  44 + * @version 2009-09-11
  45 + */
  46 +public class CDL {
  47 +
  48 + /**
  49 + * Get the next value. The value can be wrapped in quotes. The value can
  50 + * be empty.
  51 + * @param x A JSONTokener of the source text.
  52 + * @return The value string, or null if empty.
  53 + * @throws JSONException if the quoted string is badly formed.
  54 + */
  55 + private static String getValue(JSONTokener x) throws JSONException {
  56 + char c;
  57 + char q;
  58 + StringBuffer sb;
  59 + do {
  60 + c = x.next();
  61 + } while (c == ' ' || c == '\t');
  62 + switch (c) {
  63 + case 0:
  64 + return null;
  65 + case '"':
  66 + case '\'':
  67 + q = c;
  68 + sb = new StringBuffer();
  69 + for (;;) {
  70 + c = x.next();
  71 + if (c == q) {
  72 + break;
  73 + }
  74 + if (c == 0 || c == '\n' || c == '\r') {
  75 + throw x.syntaxError("Missing close quote '" + q + "'.");
  76 + }
  77 + sb.append(c);
  78 + }
  79 + return sb.toString();
  80 + case ',':
  81 + x.back();
  82 + return "";
  83 + default:
  84 + x.back();
  85 + return x.nextTo(',');
  86 + }
  87 + }
  88 +
  89 + /**
  90 + * Produce a JSONArray of strings from a row of comma delimited values.
  91 + * @param x A JSONTokener of the source text.
  92 + * @return A JSONArray of strings.
  93 + * @throws JSONException
  94 + */
  95 + public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
  96 + JSONArray ja = new JSONArray();
  97 + for (;;) {
  98 + String value = getValue(x);
  99 + char c = x.next();
  100 + if (value == null ||
  101 + (ja.length() == 0 && value.length() == 0 && c != ',')) {
  102 + return null;
  103 + }
  104 + ja.put(value);
  105 + for (;;) {
  106 + if (c == ',') {
  107 + break;
  108 + }
  109 + if (c != ' ') {
  110 + if (c == '\n' || c == '\r' || c == 0) {
  111 + return ja;
  112 + }
  113 + throw x.syntaxError("Bad character '" + c + "' (" +
  114 + (int)c + ").");
  115 + }
  116 + c = x.next();
  117 + }
  118 + }
  119 + }
  120 +
  121 + /**
  122 + * Produce a JSONObject from a row of comma delimited text, using a
  123 + * parallel JSONArray of strings to provides the names of the elements.
  124 + * @param names A JSONArray of names. This is commonly obtained from the
  125 + * first row of a comma delimited text file using the rowToJSONArray
  126 + * method.
  127 + * @param x A JSONTokener of the source text.
  128 + * @return A JSONObject combining the names and values.
  129 + * @throws JSONException
  130 + */
  131 + public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
  132 + throws JSONException {
  133 + JSONArray ja = rowToJSONArray(x);
  134 + return ja != null ? ja.toJSONObject(names) : null;
  135 + }
  136 +
  137 + /**
  138 + * Produce a JSONArray of JSONObjects from a comma delimited text string,
  139 + * using the first row as a source of names.
  140 + * @param string The comma delimited text.
  141 + * @return A JSONArray of JSONObjects.
  142 + * @throws JSONException
  143 + */
  144 + public static JSONArray toJSONArray(String string) throws JSONException {
  145 + return toJSONArray(new JSONTokener(string));
  146 + }
  147 +
  148 + /**
  149 + * Produce a JSONArray of JSONObjects from a comma delimited text string,
  150 + * using the first row as a source of names.
  151 + * @param x The JSONTokener containing the comma delimited text.
  152 + * @return A JSONArray of JSONObjects.
  153 + * @throws JSONException
  154 + */
  155 + public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
  156 + return toJSONArray(rowToJSONArray(x), x);
  157 + }
  158 +
  159 + /**
  160 + * Produce a JSONArray of JSONObjects from a comma delimited text string
  161 + * using a supplied JSONArray as the source of element names.
  162 + * @param names A JSONArray of strings.
  163 + * @param string The comma delimited text.
  164 + * @return A JSONArray of JSONObjects.
  165 + * @throws JSONException
  166 + */
  167 + public static JSONArray toJSONArray(JSONArray names, String string)
  168 + throws JSONException {
  169 + return toJSONArray(names, new JSONTokener(string));
  170 + }
  171 +
  172 + /**
  173 + * Produce a JSONArray of JSONObjects from a comma delimited text string
  174 + * using a supplied JSONArray as the source of element names.
  175 + * @param names A JSONArray of strings.
  176 + * @param x A JSONTokener of the source text.
  177 + * @return A JSONArray of JSONObjects.
  178 + * @throws JSONException
  179 + */
  180 + public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
  181 + throws JSONException {
  182 + if (names == null || names.length() == 0) {
  183 + return null;
  184 + }
  185 + JSONArray ja = new JSONArray();
  186 + for (;;) {
  187 + JSONObject jo = rowToJSONObject(names, x);
  188 + if (jo == null) {
  189 + break;
  190 + }
  191 + ja.put(jo);
  192 + }
  193 + if (ja.length() == 0) {
  194 + return null;
  195 + }
  196 + return ja;
  197 + }
  198 +
  199 +
  200 + /**
  201 + * Produce a comma delimited text row from a JSONArray. Values containing
  202 + * the comma character will be quoted. Troublesome characters may be
  203 + * removed.
  204 + * @param ja A JSONArray of strings.
  205 + * @return A string ending in NEWLINE.
  206 + */
  207 + public static String rowToString(JSONArray ja) {
  208 + StringBuffer sb = new StringBuffer();
  209 + for (int i = 0; i < ja.length(); i += 1) {
  210 + if (i > 0) {
  211 + sb.append(',');
  212 + }
  213 + Object o = ja.opt(i);
  214 + if (o != null) {
  215 + String s = o.toString();
  216 + if (s.length() > 0 && (s.indexOf(',') >= 0 || s.indexOf('\n') >= 0 ||
  217 + s.indexOf('\r') >= 0 || s.indexOf(0) >= 0 ||
  218 + s.charAt(0) == '"')) {
  219 + sb.append('"');
  220 + int length = s.length();
  221 + for (int j = 0; j < length; j += 1) {
  222 + char c = s.charAt(j);
  223 + if (c >= ' ' && c != '"') {
  224 + sb.append(c);
  225 + }
  226 + }
  227 + sb.append('"');
  228 + } else {
  229 + sb.append(s);
  230 + }
  231 + }
  232 + }
  233 + sb.append('\n');
  234 + return sb.toString();
  235 + }
  236 +
  237 + /**
  238 + * Produce a comma delimited text from a JSONArray of JSONObjects. The
  239 + * first row will be a list of names obtained by inspecting the first
  240 + * JSONObject.
  241 + * @param ja A JSONArray of JSONObjects.
  242 + * @return A comma delimited text.
  243 + * @throws JSONException
  244 + */
  245 + public static String toString(JSONArray ja) throws JSONException {
  246 + JSONObject jo = ja.optJSONObject(0);
  247 + if (jo != null) {
  248 + JSONArray names = jo.names();
  249 + if (names != null) {
  250 + return rowToString(names) + toString(names, ja);
  251 + }
  252 + }
  253 + return null;
  254 + }
  255 +
  256 + /**
  257 + * Produce a comma delimited text from a JSONArray of JSONObjects using
  258 + * a provided list of names. The list of names is not included in the
  259 + * output.
  260 + * @param names A JSONArray of strings.
  261 + * @param ja A JSONArray of JSONObjects.
  262 + * @return A comma delimited text.
  263 + * @throws JSONException
  264 + */
  265 + public static String toString(JSONArray names, JSONArray ja)
  266 + throws JSONException {
  267 + if (names == null || names.length() == 0) {
  268 + return null;
  269 + }
  270 + StringBuffer sb = new StringBuffer();
  271 + for (int i = 0; i < ja.length(); i += 1) {
  272 + JSONObject jo = ja.optJSONObject(i);
  273 + if (jo != null) {
  274 + sb.append(rowToString(jo.toJSONArray(names)));
  275 + }
  276 + }
  277 + return sb.toString();
  278 + }
  279 +}
170 src/main/java/de/monoid/json/Cookie.java
... ... @@ -0,0 +1,170 @@
90 src/main/java/de/monoid/json/CookieList.java
... ... @@ -0,0 +1,90 @@
  1 +package de.monoid.json;
  2 +
  3 +/*
  4 +Copyright (c) 2002 JSON.org
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy
  7 +of this software and associated documentation files (the "Software"), to deal
  8 +in the Software without restriction, including without limitation the rights
  9 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 +copies of the Software, and to permit persons to whom the Software is
  11 +furnished to do so, subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +The Software shall be used for Good, not Evil.
  17 +
  18 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24 +SOFTWARE.
  25 +*/
  26 +
  27 +import java.util.Iterator;
  28 +
  29 +/**
  30 + * Convert a web browser cookie list string to a JSONObject and back.
  31 + * @author JSON.org
  32 + * @version 2008-09-18
  33 + */
  34 +public class CookieList {
  35 +
  36 + /**
  37 + * Convert a cookie list into a JSONObject. A cookie list is a sequence
  38 + * of name/value pairs. The names are separated from the values by '='.
  39 + * The pairs are separated by ';'. The names and the values
  40 + * will be unescaped, possibly converting '+' and '%' sequences.
  41 + *
  42 + * To add a cookie to a cooklist,
  43 + * cookielistJSONObject.put(cookieJSONObject.getString("name"),
  44 + * cookieJSONObject.getString("value"));
  45 + * @param string A cookie list string
  46 + * @return A JSONObject
  47 + * @throws JSONException
  48 + */
  49 + public static JSONObject toJSONObject(String string) throws JSONException {
  50 + JSONObject o = new JSONObject();
  51 + JSONTokener x = new JSONTokener(string);
  52 + while (x.more()) {
  53 + String name = Cookie.unescape(x.nextTo('='));
  54 + x.next('=');
  55 + o.put(name, Cookie.unescape(x.nextTo(';')));
  56 + x.next();
  57 + }
  58 + return o;
  59 + }
  60 +
  61 +
  62 + /**
  63 + * Convert a JSONObject into a cookie list. A cookie list is a sequence
  64 + * of name/value pairs. The names are separated from the values by '='.
  65 + * The pairs are separated by ';'. The characters '%', '+', '=', and ';'
  66 + * in the names and values are replaced by "%hh".
  67 + * @param o A JSONObject
  68 + * @return A cookie list string
  69 + * @throws JSONException
  70 + */
  71 + public static String toString(JSONObject o) throws JSONException {
  72 + boolean b = false;
  73 + Iterator<?> keys = o.keys();
  74 + String s;
  75 + StringBuffer sb = new StringBuffer();
  76 + while (keys.hasNext()) {
  77 + s = keys.next().toString();
  78 + if (!o.isNull(s)) {
  79 + if (b) {
  80 + sb.append(';');
  81 + }
  82 + sb.append(Cookie.escape(s));
  83 + sb.append("=");
  84 + sb.append(Cookie.escape(o.getString(s)));
  85 + b = true;
  86 + }
  87 + }
  88 + return sb.toString();
  89 + }
  90 +}
163 src/main/java/de/monoid/json/HTTP.java
... ... @@ -0,0 +1,163 @@
  1 +package de.monoid.json;
  2 +
  3 +/*
  4 +Copyright (c) 2002 JSON.org
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy
  7 +of this software and associated documentation files (the "Software"), to deal
  8 +in the Software without restriction, including without limitation the rights
  9 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 +copies of the Software, and to permit persons to whom the Software is
  11 +furnished to do so, subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +The Software shall be used for Good, not Evil.
  17 +
  18 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24 +SOFTWARE.
  25 +*/
  26 +
  27 +import java.util.Iterator;
  28 +
  29 +/**
  30 + * Convert an HTTP header to a JSONObject and back.
  31 + * @author JSON.org
  32 + * @version 2008-09-18
  33 + */
  34 +public class HTTP {
  35 +
  36 + /** Carriage return/line feed. */
  37 + public static final String CRLF = "\r\n";
  38 +
  39 + /**
  40 + * Convert an HTTP header string into a JSONObject. It can be a request
  41 + * header or a response header. A request header will contain
  42 + * <pre>{
  43 + * Method: "POST" (for example),
  44 + * "Request-URI": "/" (for example),
  45 + * "HTTP-Version": "HTTP/1.1" (for example)
  46 + * }</pre>
  47 + * A response header will contain
  48 + * <pre>{
  49 + * "HTTP-Version": "HTTP/1.1" (for example),
  50 + * "Status-Code": "200" (for example),
  51 + * "Reason-Phrase": "OK" (for example)
  52 + * }</pre>
  53 + * In addition, the other parameters in the header will be captured, using
  54 + * the HTTP field names as JSON names, so that <pre>
  55 + * Date: Sun, 26 May 2002 18:06:04 GMT
  56 + * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
  57 + * Cache-Control: no-cache</pre>
  58 + * become
  59 + * <pre>{...
  60 + * Date: "Sun, 26 May 2002 18:06:04 GMT",
  61 + * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
  62 + * "Cache-Control": "no-cache",
  63 + * ...}</pre>
  64 + * It does no further checking or conversion. It does not parse dates.
  65 + * It does not do '%' transforms on URLs.
  66 + * @param string An HTTP header string.
  67 + * @return A JSONObject containing the elements and attributes
  68 + * of the XML string.
  69 + * @throws JSONException
  70 + */
  71 + public static JSONObject toJSONObject(String string) throws JSONException {
  72 + JSONObject o = new JSONObject();
  73 + HTTPTokener x = new HTTPTokener(string);
  74 + String t;
  75 +
  76 + t = x.nextToken();
  77 + if (t.toUpperCase().startsWith("HTTP")) {
  78 +
  79 +// Response
  80 +
  81 + o.put("HTTP-Version", t);
  82 + o.put("Status-Code", x.nextToken());
  83 + o.put("Reason-Phrase", x.nextTo('\0'));
  84 + x.next();
  85 +
  86 + } else {
  87 +
  88 +// Request
  89 +
  90 + o.put("Method", t);
  91 + o.put("Request-URI", x.nextToken());
  92 + o.put("HTTP-Version", x.nextToken());
  93 + }
  94 +
  95 +// Fields
  96 +
  97 + while (x.more()) {
  98 + String name = x.nextTo(':');
  99 + x.next(':');
  100 + o.put(name, x.nextTo('\0'));
  101 + x.next();
  102 + }
  103 + return o;
  104 + }
  105 +
  106 +
  107 + /**
  108 + * Convert a JSONObject into an HTTP header. A request header must contain
  109 + * <pre>{
  110 + * Method: "POST" (for example),
  111 + * "Request-URI": "/" (for example),
  112 + * "HTTP-Version": "HTTP/1.1" (for example)
  113 + * }</pre>
  114 + * A response header must contain
  115 + * <pre>{
  116 + * "HTTP-Version": "HTTP/1.1" (for example),
  117 + * "Status-Code": "200" (for example),
  118 + * "Reason-Phrase": "OK" (for example)
  119 + * }</pre>
  120 + * Any other members of the JSONObject will be output as HTTP fields.
  121 + * The result will end with two CRLF pairs.
  122 + * @param o A JSONObject
  123 + * @return An HTTP header string.
  124 + * @throws JSONException if the object does not contain enough
  125 + * information.
  126 + */
  127 + public static String toString(JSONObject o) throws JSONException {
  128 + Iterator<?> keys = o.keys();
  129 + String s;
  130 + StringBuffer sb = new StringBuffer();
  131 + if (o.has("Status-Code") && o.has("Reason-Phrase")) {
  132 + sb.append(o.getString("HTTP-Version"));
  133 + sb.append(' ');
  134 + sb.append(o.getString("Status-Code"));
  135 + sb.append(' ');
  136 + sb.append(o.getString("Reason-Phrase"));
  137 + } else if (o.has("Method") && o.has("Request-URI")) {
  138 + sb.append(o.getString("Method"));
  139 + sb.append(' ');
  140 + sb.append('"');
  141 + sb.append(o.getString("Request-URI"));
  142 + sb.append('"');
  143 + sb.append(' ');
  144 + sb.append(o.getString("HTTP-Version"));
  145 + } else {
  146 + throw new JSONException("Not enough material for an HTTP header.");
  147 + }
  148 + sb.append(CRLF);
  149 + while (keys.hasNext()) {
  150 + s = keys.next().toString();
  151 + if (!s.equals("HTTP-Version") && !s.equals("Status-Code") &&
  152 + !s.equals("Reason-Phrase") && !s.equals("Method") &&
  153 + !s.equals("Request-URI") && !o.isNull(s)) {
  154 + sb.append(s);
  155 + sb.append(": ");
  156 + sb.append(o.getString(s));
  157 + sb.append(CRLF);
  158 + }
  159 + }
  160 + sb.append(CRLF);
  161 + return sb.toString();
  162 + }
  163 +}
78 src/main/java/de/monoid/json/HTTPTokener.java
... ... @@ -0,0 +1,78 @@
  1 +package de.monoid.json;
  2 +
  3 +
  4 +/*
  5 +Copyright (c) 2002 JSON.org
  6 +
  7 +Permission is hereby granted, free of charge, to any person obtaining a copy
  8 +of this software and associated documentation files (the "Software"), to deal
  9 +in the Software without restriction, including without limitation the rights
  10 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 +copies of the Software, and to permit persons to whom the Software is
  12 +furnished to do so, subject to the following conditions:
  13 +
  14 +The above copyright notice and this permission notice shall be included in all
  15 +copies or substantial portions of the Software.
  16 +
  17 +The Software shall be used for Good, not Evil.
  18 +
  19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25 +SOFTWARE.
  26 +*/
  27 +
  28 +/**
  29 + * The HTTPTokener extends the JSONTokener to provide additional methods
  30 + * for the parsing of HTTP headers.
  31 + * @author JSON.org
  32 + * @version 2008-09-18
  33 + */
  34 +public class HTTPTokener extends JSONTokener {
  35 +
  36 + /**
  37 + * Construct an HTTPTokener from a string.
  38 + * @param s A source string.
  39 + */
  40 + public HTTPTokener(String s) {
  41 + super(s);
  42 + }
  43 +
  44 +
  45 + /**
  46 + * Get the next token or string. This is used in parsing HTTP headers.
  47 + * @throws JSONException
  48 + * @return A String.
  49 + */
  50 + public String nextToken() throws JSONException {
  51 + char c;
  52 + char q;
  53 + StringBuffer sb = new StringBuffer();
  54 + do {
  55 + c = next();
  56 + } while (Character.isWhitespace(c));
  57 + if (c == '"' || c == '\'') {
  58 + q = c;
  59 + for (;;) {
  60 + c = next();
  61 + if (c < ' ') {
  62 + throw syntaxError("Unterminated string.");
  63 + }
  64 + if (c == q) {
  65 + return sb.toString();
  66 + }
  67 + sb.append(c);
  68 + }
  69 + }
  70 + for (;;) {
  71 + if (c == 0 || Character.isWhitespace(c)) {
  72 + return sb.toString();
  73 + }
  74 + sb.append(c);
  75 + c = next();
  76 + }
  77 + }
  78 +}
948 src/main/java/de/monoid/json/JSONArray.java
... ... @@ -0,0 +1,948 @@
  1 +package de.monoid.json;
  2 +
  3 +/*
  4 + Copyright (c) 2002 JSON.org
  5 +
  6 + Permission is hereby granted, free of charge, to any person obtaining a copy
  7 + of this software and associated documentation files (the "Software"), to deal
  8 + in the Software without restriction, including without limitation the rights
  9 + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 + copies of the Software, and to permit persons to whom the Software is
  11 + furnished to do so, subject to the following conditions:
  12 +
  13 + The above copyright notice and this permission notice shall be included in all
  14 + copies or substantial portions of the Software.
  15 +
  16 + The Software shall be used for Good, not Evil.
  17 +
  18 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21 + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24 + SOFTWARE.
  25 + */
  26 +
  27 +import java.io.IOException;
  28 +import java.io.Writer;
  29 +import java.lang.reflect.Array;
  30 +import java.util.ArrayList;
  31 +import java.util.Collection;
  32 +import java.util.Map;
  33 +
  34 +/**
  35 + * A JSONArray is an ordered sequence of values. Its external text form is a
  36 + * string wrapped in square brackets with commas separating the values. The
  37 + * internal form is an object having <code>get</code> and <code>opt</code>
  38 + * methods for accessing the values by index, and <code>put</code> methods for
  39 + * adding or replacing values. The values can be any of these types:
  40 + * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
  41 + * <code>Number</code>, <code>String</code>, or the
  42 + * <code>JSONObject.NULL object</code>.
  43 + * <p>
  44 + * The constructor can convert a JSON text into a Java object. The
  45 + * <code>toString</code> method converts to JSON text.
  46 + * <p>
  47 + * A <code>get</code> method returns a value if one can be found, and throws an
  48 + * exception if one cannot be found. An <code>opt</code> method returns a
  49 + * default value instead of throwing an exception, and so is useful for
  50 + * obtaining optional values.
  51 + * <p>
  52 + * The generic <code>get()</code> and <code>opt()</code> methods return an
  53 + * object which you can cast or query for type. There are also typed
  54 + * <code>get</code> and <code>opt</code> methods that do type checking and type
  55 + * coercion for you.
  56 + * <p>
  57 + * The texts produced by the <code>toString</code> methods strictly conform to
  58 + * JSON syntax rules. The constructors are more forgiving in the texts they will
  59 + * accept:
  60 + * <ul>
  61 + * <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
  62 + * before the closing bracket.</li>
  63 + * <li>The <code>null</code> value will be inserted when there is <code>,</code>
  64 + * &nbsp;<small>(comma)</small> elision.</li>
  65 + * <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
  66 + * quote)</small>.</li><