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 2015-12-09
033 */
034public class CookieList {
035
036    /**
037     * Convert a cookie list into a JSONObject. A cookie list is a sequence
038     * of name/value pairs. The names are separated from the values by '='.
039     * The pairs are separated by ';'. The names and the values
040     * will be unescaped, possibly converting '+' and '%' sequences.
041     *
042     * To add a cookie to a cooklist,
043     * cookielistJSONObject.put(cookieJSONObject.getString("name"),
044     *     cookieJSONObject.getString("value"));
045     * @param string  A cookie list string
046     * @return A JSONObject
047     * @throws JSONException
048     */
049    public static JSONObject toJSONObject(String string) throws JSONException {
050        JSONObject jo = new JSONObject();
051        JSONTokener x = new JSONTokener(string);
052        while (x.more()) {
053            String name = Cookie.unescape(x.nextTo('='));
054            x.next('=');
055            jo.put(name, Cookie.unescape(x.nextTo(';')));
056            x.next();
057        }
058        return jo;
059    }
060
061    /**
062     * Convert a JSONObject into a cookie list. A cookie list is a sequence
063     * of name/value pairs. The names are separated from the values by '='.
064     * The pairs are separated by ';'. The characters '%', '+', '=', and ';'
065     * in the names and values are replaced by "%hh".
066     * @param jo A JSONObject
067     * @return A cookie list string
068     * @throws JSONException
069     */
070    public static String toString(JSONObject jo) throws JSONException {
071        boolean             b = false;
072        Iterator<String>    keys = jo.keys();
073        String              string;
074        StringBuilder sb = new StringBuilder();
075        while (keys.hasNext()) {
076            string = keys.next();
077            if (!jo.isNull(string)) {
078                if (b) {
079                    sb.append(';');
080                }
081                sb.append(Cookie.escape(string));
082                sb.append("=");
083                sb.append(Cookie.escape(jo.getString(string)));
084                b = true;
085            }
086        }
087        return sb.toString();
088    }
089}