Class | Line # | Actions | |||
---|---|---|---|---|---|
CookieList | 33 | 20 | 5 |
1 | package org.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 | * Convert a web browser cookie list string to a JSONObject and back. | |
29 | * | |
30 | * @author JSON.org | |
31 | * @version 2015-12-09 | |
32 | */ | |
33 | public class CookieList | |
34 | { | |
35 | ||
36 | /** | |
37 | * Convert a cookie list into a JSONObject. A cookie list is a sequence of | |
38 | * name/value pairs. The names are separated from the values by '='. The pairs | |
39 | * are separated by ';'. The names and the values will be unescaped, possibly | |
40 | * converting '+' and '%' sequences. | |
41 | * | |
42 | * To add a cookie to a cookie list, | |
43 | * cookielistJSONObject.put(cookieJSONObject.getString("name"), | |
44 | * cookieJSONObject.getString("value")); | |
45 | * | |
46 | * @param string | |
47 | * A cookie list string | |
48 | * @return A JSONObject | |
49 | * @throws JSONException | |
50 | */ | |
51 | 0 | public static JSONObject toJSONObject(String string) throws JSONException |
52 | { | |
53 | 0 | JSONObject jo = new JSONObject(); |
54 | 0 | JSONTokener x = new JSONTokener(string); |
55 | 0 | while (x.more()) |
56 | { | |
57 | 0 | String name = Cookie.unescape(x.nextTo('=')); |
58 | 0 | x.next('='); |
59 | 0 | jo.put(name, Cookie.unescape(x.nextTo(';'))); |
60 | 0 | x.next(); |
61 | } | |
62 | 0 | return jo; |
63 | } | |
64 | ||
65 | /** | |
66 | * Convert a JSONObject into a cookie list. A cookie list is a sequence of | |
67 | * name/value pairs. The names are separated from the values by '='. The pairs | |
68 | * are separated by ';'. The characters '%', '+', '=', and ';' in the names | |
69 | * and values are replaced by "%hh". | |
70 | * | |
71 | * @param jo | |
72 | * A JSONObject | |
73 | * @return A cookie list string | |
74 | * @throws JSONException | |
75 | */ | |
76 | 0 | public static String toString(JSONObject jo) throws JSONException |
77 | { | |
78 | 0 | boolean b = false; |
79 | 0 | final StringBuilder sb = new StringBuilder(); |
80 | // Don't use the new entrySet API to maintain Android support | |
81 | 0 | for (final String key : jo.keySet()) |
82 | { | |
83 | 0 | final Object value = jo.opt(key); |
84 | 0 | if (!JSONObject.NULL.equals(value)) |
85 | { | |
86 | 0 | if (b) |
87 | { | |
88 | 0 | sb.append(';'); |
89 | } | |
90 | 0 | sb.append(Cookie.escape(key)); |
91 | 0 | sb.append("="); |
92 | 0 | sb.append(Cookie.escape(value.toString())); |
93 | 0 | b = true; |
94 | } | |
95 | } | |
96 | 0 | return sb.toString(); |
97 | } | |
98 | } |