Statistics
| Branch: | Tag: | Revision:

root / src / org / json / CookieList.java @ 4bf05cad

History | View | Annotate | Download (3.2 kB)

1 14ad7326 pastith
package org.json;
2 14ad7326 pastith
3 14ad7326 pastith
/*
4 14ad7326 pastith
Copyright (c) 2002 JSON.org
5 14ad7326 pastith

6 14ad7326 pastith
Permission is hereby granted, free of charge, to any person obtaining a copy
7 14ad7326 pastith
of this software and associated documentation files (the "Software"), to deal
8 14ad7326 pastith
in the Software without restriction, including without limitation the rights
9 14ad7326 pastith
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 14ad7326 pastith
copies of the Software, and to permit persons to whom the Software is
11 14ad7326 pastith
furnished to do so, subject to the following conditions:
12 14ad7326 pastith

13 14ad7326 pastith
The above copyright notice and this permission notice shall be included in all
14 14ad7326 pastith
copies or substantial portions of the Software.
15 14ad7326 pastith

16 14ad7326 pastith
The Software shall be used for Good, not Evil.
17 14ad7326 pastith

18 14ad7326 pastith
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 14ad7326 pastith
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 14ad7326 pastith
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 14ad7326 pastith
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 14ad7326 pastith
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 14ad7326 pastith
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 14ad7326 pastith
SOFTWARE.
25 14ad7326 pastith
*/
26 14ad7326 pastith
27 14ad7326 pastith
import java.util.Iterator;
28 14ad7326 pastith
29 14ad7326 pastith
/**
30 14ad7326 pastith
 * Convert a web browser cookie list string to a JSONObject and back.
31 14ad7326 pastith
 * @author JSON.org
32 14ad7326 pastith
 * @version 2008-09-18
33 14ad7326 pastith
 */
34 14ad7326 pastith
public class CookieList {
35 14ad7326 pastith
36 14ad7326 pastith
    /**
37 14ad7326 pastith
     * Convert a cookie list into a JSONObject. A cookie list is a sequence
38 14ad7326 pastith
     * of name/value pairs. The names are separated from the values by '='.
39 14ad7326 pastith
     * The pairs are separated by ';'. The names and the values
40 14ad7326 pastith
     * will be unescaped, possibly converting '+' and '%' sequences.
41 14ad7326 pastith
     *
42 14ad7326 pastith
     * To add a cookie to a cooklist,
43 14ad7326 pastith
     * cookielistJSONObject.put(cookieJSONObject.getString("name"),
44 14ad7326 pastith
     *     cookieJSONObject.getString("value"));
45 14ad7326 pastith
     * @param string  A cookie list string
46 14ad7326 pastith
     * @return A JSONObject
47 14ad7326 pastith
     * @throws JSONException
48 14ad7326 pastith
     */
49 14ad7326 pastith
    public static JSONObject toJSONObject(String string) throws JSONException {
50 14ad7326 pastith
        JSONObject o = new JSONObject();
51 14ad7326 pastith
        JSONTokener x = new JSONTokener(string);
52 14ad7326 pastith
        while (x.more()) {
53 14ad7326 pastith
            String name = Cookie.unescape(x.nextTo('='));
54 14ad7326 pastith
            x.next('=');
55 14ad7326 pastith
            o.put(name, Cookie.unescape(x.nextTo(';')));
56 14ad7326 pastith
            x.next();
57 14ad7326 pastith
        }
58 14ad7326 pastith
        return o;
59 14ad7326 pastith
    }
60 14ad7326 pastith
61 14ad7326 pastith
62 14ad7326 pastith
    /**
63 14ad7326 pastith
     * Convert a JSONObject into a cookie list. A cookie list is a sequence
64 14ad7326 pastith
     * of name/value pairs. The names are separated from the values by '='.
65 14ad7326 pastith
     * The pairs are separated by ';'. The characters '%', '+', '=', and ';'
66 14ad7326 pastith
     * in the names and values are replaced by "%hh".
67 14ad7326 pastith
     * @param o A JSONObject
68 14ad7326 pastith
     * @return A cookie list string
69 14ad7326 pastith
     * @throws JSONException
70 14ad7326 pastith
     */
71 14ad7326 pastith
    public static String toString(JSONObject o) throws JSONException {
72 14ad7326 pastith
        boolean      b = false;
73 14ad7326 pastith
        Iterator     keys = o.keys();
74 14ad7326 pastith
        String       s;
75 14ad7326 pastith
        StringBuffer sb = new StringBuffer();
76 14ad7326 pastith
        while (keys.hasNext()) {
77 14ad7326 pastith
            s = keys.next().toString();
78 14ad7326 pastith
            if (!o.isNull(s)) {
79 14ad7326 pastith
                if (b) {
80 14ad7326 pastith
                    sb.append(';');
81 14ad7326 pastith
                }
82 14ad7326 pastith
                sb.append(Cookie.escape(s));
83 14ad7326 pastith
                sb.append("=");
84 14ad7326 pastith
                sb.append(Cookie.escape(o.getString(s)));
85 14ad7326 pastith
                b = true;
86 14ad7326 pastith
            }
87 14ad7326 pastith
        }
88 14ad7326 pastith
        return sb.toString();
89 14ad7326 pastith
    }
90 14ad7326 pastith
}