001package org.json; 002 003/* 004Copyright (c) 2002 JSON.org 005 006Permission is hereby granted, free of charge, to any person obtaining a copy 007of this software and associated documentation files (the "Software"), to deal 008in the Software without restriction, including without limitation the rights 009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010copies of the Software, and to permit persons to whom the Software is 011furnished to do so, subject to the following conditions: 012 013The above copyright notice and this permission notice shall be included in all 014copies or substantial portions of the Software. 015 016The Software shall be used for Good, not Evil. 017 018THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 019IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 020FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 021AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 022LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 023OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 024SOFTWARE. 025 */ 026 027import java.util.Iterator; 028 029/** 030 * Convert a web browser cookie list string to a JSONObject and back. 031 * @author JSON.org 032@version $Id$ 033@since Ptolemy II 10.0 034 * @version 2008-09-18 035 */ 036public class CookieList { 037 038 /** 039 * Convert a cookie list into a JSONObject. A cookie list is a sequence 040 * of name/value pairs. The names are separated from the values by '='. 041 * The pairs are separated by ';'. The names and the values 042 * will be unescaped, possibly converting '+' and '%' sequences. 043 * 044 * To add a cookie to a cooklist, 045 * cookielistJSONObject.put(cookieJSONObject.getString("name"), 046 * cookieJSONObject.getString("value")); 047 * @param string A cookie list string 048 * @return A JSONObject 049 * @exception JSONException 050 */ 051 public static JSONObject toJSONObject(String string) throws JSONException { 052 JSONObject o = new JSONObject(); 053 JSONTokener x = new JSONTokener(string); 054 while (x.more()) { 055 String name = Cookie.unescape(x.nextTo('=')); 056 x.next('='); 057 o.put(name, Cookie.unescape(x.nextTo(';'))); 058 x.next(); 059 } 060 return o; 061 } 062 063 /** 064 * Convert a JSONObject into a cookie list. A cookie list is a sequence 065 * of name/value pairs. The names are separated from the values by '='. 066 * The pairs are separated by ';'. The characters '%', '+', '=', and ';' 067 * in the names and values are replaced by "%hh". 068 * @param o A JSONObject 069 * @return A cookie list string 070 * @exception JSONException 071 */ 072 public static String toString(JSONObject o) throws JSONException { 073 boolean b = false; 074 Iterator keys = o.keys(); 075 String s; 076 StringBuffer sb = new StringBuffer(); 077 while (keys.hasNext()) { 078 s = keys.next().toString(); 079 if (!o.isNull(s)) { 080 if (b) { 081 sb.append(';'); 082 } 083 sb.append(Cookie.escape(s)); 084 sb.append("="); 085 sb.append(Cookie.escape(o.getString(s))); 086 b = true; 087 } 088 } 089 return sb.toString(); 090 } 091}