Package com.util.org.json
Class Cookie
- java.lang.Object
-
- com.util.org.json.Cookie
-
public class Cookie extends java.lang.Object
Convert a web browser cookie specification to a JSONObject and back. JSON and Cookies are both notations for name/value pairs. See also: https://tools.ietf.org/html/rfc6265- Version:
- 2015-12-09
- Author:
- JSON.org
-
-
Constructor Summary
Constructors Constructor Description Cookie()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
escape(java.lang.String string)
Produce a copy of a string in which the characters '+', '%', '=', ';' and control characters are replaced with "%hh".static JSONObject
toJSONObject(java.lang.String string)
Convert a cookie specification string into a JSONObject.static java.lang.String
toString(JSONObject jo)
Convert a JSONObject into a cookie specification string.static java.lang.String
unescape(java.lang.String string)
Convert%
hh sequences to single characters, and convert plus to space.
-
-
-
Method Detail
-
escape
public static java.lang.String escape(java.lang.String string)
Produce a copy of a string in which the characters '+', '%', '=', ';' and control characters are replaced with "%hh". This is a gentle form of URL encoding, attempting to cause as little distortion to the string as possible. The characters '=' and ';' are meta characters in cookies. By convention, they are escaped using the URL-encoding. This is only a convention, not a standard. Often, cookies are expected to have encoded values. We encode '=' and ';' because we must. We encode '%' and '+' because they are meta characters in URL encoding.- Parameters:
string
- The source string.- Returns:
- The escaped result.
-
toJSONObject
public static JSONObject toJSONObject(java.lang.String string)
Convert a cookie specification string into a JSONObject. The string must contain a name value pair separated by '='. The name and the value will be unescaped, possibly converting '+' and '%' sequences. The cookie properties may follow, separated by ';', also represented as name=value (except the Attribute properties like "Secure" or "HttpOnly", which do not have a value. The valueBoolean.TRUE
will be used for these). The name will be stored under the key "name", and the value will be stored under the key "value". This method does not do checking or validation of the parameters. It only converts the cookie string into a JSONObject. All attribute names are converted to lower case keys in the JSONObject (HttpOnly => httponly). If an attribute is specified more than once, only the value found closer to the end of the cookie-string is kept.- Parameters:
string
- The cookie specification string.- Returns:
- A JSONObject containing "name", "value", and possibly other members.
- Throws:
JSONException
- If there is an error parsing the Cookie String. Cookie strings must have at least one '=' character and the 'name' portion of the cookie must not be blank.
-
toString
public static java.lang.String toString(JSONObject jo) throws JSONException
Convert a JSONObject into a cookie specification string. The JSONObject must contain "name" and "value" members (case insensitive). If the JSONObject contains other members, they will be appended to the cookie specification string. User-Agents are instructed to ignore unknown attributes, so ensure your JSONObject is using only known attributes. See also: https://tools.ietf.org/html/rfc6265- Parameters:
jo
- A JSONObject- Returns:
- A cookie specification string
- Throws:
JSONException
- thrown if the cookie has no name.
-
unescape
public static java.lang.String unescape(java.lang.String string)
Convert%
hh sequences to single characters, and convert plus to space.- Parameters:
string
- A string that may contain+
(plus) and%
hh sequences.- Returns:
- The unescaped string.
-
-