4 Copyright (c) 2002 JSON.org
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:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 The Software shall be used for Good, not Evil.
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
28 * Convert a web browser cookie specification to a JSONObject and back.
29 * JSON and Cookies are both notations for name/value pairs.
36 * Produce a copy of a string in which the characters '+', '%', '=', ';'
37 * and control characters are replaced with "%hh". This is a gentle form
38 * of URL encoding, attempting to cause as little distortion to the
39 * string as possible. The characters '=' and ';' are meta characters in
40 * cookies. By convention, they are escaped using the URL-encoding. This is
41 * only a convention, not a standard. Often, cookies are expected to have
42 * encoded values. We encode '=' and ';' because we must. We encode '%' and
43 * '+' because they are meta characters in URL encoding.
44 * @param string The source string.
45 * @return The escaped result.
47 public static String escape(String string) {
49 String s = string.trim();
50 int length = s.length();
51 StringBuilder sb = new StringBuilder(length);
52 for (int i = 0; i < length; i += 1) {
54 if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
56 sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));
57 sb.append(Character.forDigit((char)(c & 0x0f), 16));
67 * Convert a cookie specification string into a JSONObject. The string
68 * will contain a name value pair separated by '='. The name and the value
69 * will be unescaped, possibly converting '+' and '%' sequences. The
70 * cookie properties may follow, separated by ';', also represented as
71 * name=value (except the secure property, which does not have a value).
72 * The name will be stored under the key "name", and the value will be
73 * stored under the key "value". This method does not do checking or
74 * validation of the parameters. It only converts the cookie string into
76 * @param string The cookie specification string.
77 * @return A JSONObject containing "name", "value", and possibly other
79 * @throws JSONException
81 public static JSONObject toJSONObject(String string) throws JSONException {
83 JSONObject jo = new JSONObject();
85 JSONTokener x = new JSONTokener(string);
86 jo.put("name", x.nextTo('='));
88 jo.put("value", x.nextTo(';'));
91 name = unescape(x.nextTo("=;"));
92 if (x.next() != '=') {
93 if (name.equals("secure")) {
96 throw x.syntaxError("Missing '=' in cookie parameter.");
99 value = unescape(x.nextTo(';'));
109 * Convert a JSONObject into a cookie specification string. The JSONObject
110 * must contain "name" and "value" members.
111 * If the JSONObject contains "expires", "domain", "path", or "secure"
112 * members, they will be appended to the cookie specification string.
113 * All other members are ignored.
114 * @param jo A JSONObject
115 * @return A cookie specification string
116 * @throws JSONException
118 public static String toString(JSONObject jo) throws JSONException {
119 StringBuilder sb = new StringBuilder();
121 sb.append(escape(jo.getString("name")));
123 sb.append(escape(jo.getString("value")));
124 if (jo.has("expires")) {
125 sb.append(";expires=");
126 sb.append(jo.getString("expires"));
128 if (jo.has("domain")) {
129 sb.append(";domain=");
130 sb.append(escape(jo.getString("domain")));
132 if (jo.has("path")) {
134 sb.append(escape(jo.getString("path")));
136 if (jo.optBoolean("secure")) {
137 sb.append(";secure");
139 return sb.toString();
143 * Convert <code>%</code><i>hh</i> sequences to single characters, and
144 * convert plus to space.
145 * @param string A string that may contain
146 * <code>+</code> <small>(plus)</small> and
147 * <code>%</code><i>hh</i> sequences.
148 * @return The unescaped string.
150 public static String unescape(String string) {
151 int length = string.length();
152 StringBuilder sb = new StringBuilder(length);
153 for (int i = 0; i < length; ++i) {
154 char c = string.charAt(i);
157 } else if (c == '%' && i + 2 < length) {
158 int d = JSONTokener.dehexchar(string.charAt(i + 1));
159 int e = JSONTokener.dehexchar(string.charAt(i + 2));
160 if (d >= 0 && e >= 0) {
161 c = (char)(d * 16 + e);
167 return sb.toString();