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. JSON and
29 * Cookies are both notations for name/value pairs.
38 * Produce a copy of a string in which the characters '+', '%', '=', ';' and
39 * control characters are replaced with "%hh". This is a gentle form of URL
40 * encoding, attempting to cause as little distortion to the string as
41 * possible. The characters '=' and ';' are meta characters in cookies. By
42 * convention, they are escaped using the URL-encoding. This is only a
43 * convention, not a standard. Often, cookies are expected to have encoded
44 * values. We encode '=' and ';' because we must. We encode '%' and '+'
45 * because they are meta characters in URL encoding.
49 * @return The escaped result.
51 public static String escape(String string)
54 String s = string.trim();
55 int length = s.length();
56 StringBuilder sb = new StringBuilder(length);
57 for (int i = 0; i < length; i += 1)
60 if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';')
63 sb.append(Character.forDigit((char) ((c >>> 4) & 0x0f), 16));
64 sb.append(Character.forDigit((char) (c & 0x0f), 16));
75 * Convert a cookie specification string into a JSONObject. The string will
76 * contain a name value pair separated by '='. The name and the value will be
77 * unescaped, possibly converting '+' and '%' sequences. The cookie properties
78 * may follow, separated by ';', also represented as name=value (except the
79 * secure property, which does not have a value). The name will be stored
80 * under the key "name", and the value will be stored under the key "value".
81 * This method does not do checking or validation of the parameters. It only
82 * converts the cookie string into a JSONObject.
85 * The cookie specification string.
86 * @return A JSONObject containing "name", "value", and possibly other
88 * @throws JSONException
90 public static JSONObject toJSONObject(String string) throws JSONException
93 JSONObject jo = new JSONObject();
95 JSONTokener x = new JSONTokener(string);
96 jo.put("name", x.nextTo('='));
98 jo.put("value", x.nextTo(';'));
102 name = unescape(x.nextTo("=;"));
105 if (name.equals("secure"))
107 value = Boolean.TRUE;
111 throw x.syntaxError("Missing '=' in cookie parameter.");
116 value = unescape(x.nextTo(';'));
125 * Convert a JSONObject into a cookie specification string. The JSONObject
126 * must contain "name" and "value" members. If the JSONObject contains
127 * "expires", "domain", "path", or "secure" members, they will be appended to
128 * the cookie specification string. All other members are ignored.
132 * @return A cookie specification string
133 * @throws JSONException
135 public static String toString(JSONObject jo) throws JSONException
137 StringBuilder sb = new StringBuilder();
139 sb.append(escape(jo.getString("name")));
141 sb.append(escape(jo.getString("value")));
142 if (jo.has("expires"))
144 sb.append(";expires=");
145 sb.append(jo.getString("expires"));
147 if (jo.has("domain"))
149 sb.append(";domain=");
150 sb.append(escape(jo.getString("domain")));
155 sb.append(escape(jo.getString("path")));
157 if (jo.optBoolean("secure"))
159 sb.append(";secure");
161 return sb.toString();
165 * Convert <code>%</code><i>hh</i> sequences to single characters, and convert
169 * A string that may contain
170 * <code>+</code> <small>(plus)</small> and
171 * <code>%</code><i>hh</i> sequences.
172 * @return The unescaped string.
174 public static String unescape(String string)
176 int length = string.length();
177 StringBuilder sb = new StringBuilder(length);
178 for (int i = 0; i < length; ++i)
180 char c = string.charAt(i);
185 else if (c == '%' && i + 2 < length)
187 int d = JSONTokener.dehexchar(string.charAt(i + 1));
188 int e = JSONTokener.dehexchar(string.charAt(i + 2));
189 if (d >= 0 && e >= 0)
191 c = (char) (d * 16 + e);
197 return sb.toString();