/*
		Copyright (c) 2002 JSON.org
		
		Permission is hereby granted, free of charge, to any person obtaining a copy
		of this software and associated documentation files (the "Software"), to deal
		in the Software without restriction, including without limitation the rights
		to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
		copies of the Software, and to permit persons to whom the Software is
		furnished to do so, subject to the following conditions:
		
		The above copyright notice and this permission notice shall be included in all
		copies or substantial portions of the Software.
		
		The Software shall be used for Good, not Evil.
		
		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
		IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
		FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
		AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
		LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
		OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
		SOFTWARE.
		*/
		/// <summary>Convert a cookie list into a JSONObject.</summary>
		/// <remarks>
		/// Convert a cookie list into a JSONObject. A cookie list is a sequence
		/// of name/value pairs. The names are separated from the values by '='.
		/// The pairs are separated by ';'. The names and the values
		/// will be unescaped, possibly converting '+' and '%' sequences.
		/// To add a cookie to a cooklist,
		/// cookielistJSONObject.put(cookieJSONObject.getString("name"),
		/// cookieJSONObject.getString("value"));
		/// </remarks>
		/// <param name="string">A cookie list string</param>
		/// <returns>A JSONObject</returns>
		/// <exception cref="JSONException"/>
		/// <exception cref="org.json.JSONException"/>
		public static org.json.JSONObject ToJSONObject(string @string)
		{
			org.json.JSONObject jo = new org.json.JSONObject();
			org.json.JSONTokener x = new org.json.JSONTokener(@string);
			while (x.More())
			{
				string name = org.json.Cookie.Unescape(x.NextTo('='));
				x.Next('=');
				jo.Put(name, org.json.Cookie.Unescape(x.NextTo(';')));
				x.Next();
			}
			return jo;
		}
		/// <summary>Convert a cookie specification string into a JSONObject.</summary>
		/// <remarks>
		/// Convert a cookie specification string into a JSONObject. The string
		/// will 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 secure property, which does not have a value).
		/// 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.
		/// </remarks>
		/// <param name="string">The cookie specification string.</param>
		/// <returns>
		/// A JSONObject containing "name", "value", and possibly other
		/// members.
		/// </returns>
		/// <exception cref="JSONException"/>
		/// <exception cref="org.json.JSONException"/>
		public static org.json.JSONObject ToJSONObject(string @string)
		{
			string name;
			org.json.JSONObject jo = new org.json.JSONObject();
			object value;
			org.json.JSONTokener x = new org.json.JSONTokener(@string);
			jo.Put("name", x.NextTo('='));
			x.Next('=');
			jo.Put("value", x.NextTo(';'));
			x.Next();
			while (x.More())
			{
				name = Unescape(x.NextTo("=;"));
				if (x.Next() != '=')
				{
					if (name.Equals("secure"))
					{
						value = true;
					}
					else
					{
						throw x.SyntaxError("Missing '=' in cookie parameter.");
					}
				}
				else
				{
					value = Unescape(x.NextTo(';'));
					x.Next();
				}
				jo.Put(name, value);
			}
			return jo;
		}