/// <summary>Scan the content following the named tag, attaching it to the context.</summary>
		/// <param name="x">The XMLTokener containing the source string.</param>
		/// <param name="context">The JSONObject that will include the new material.</param>
		/// <param name="name">The tag name.</param>
		/// <returns>true if the close tag is processed.</returns>
		/// <exception cref="JSONException"/>
		/// <exception cref="org.json.JSONException"/>
		private static bool Parse(org.json.XMLTokener x, org.json.JSONObject context, string name)
		{
			char c;
			int i;
			org.json.JSONObject jsonobject = null;
			string @string;
			string tagName;
			object token;
			// Test for and skip past these forms:
			//      <!-- ... -->
			//      <!   ...   >
			//      <![  ... ]]>
			//      <?   ...  ?>
			// Report errors for these forms:
			//      <>
			//      <=
			//      <<
			token = x.NextToken();
			// <!
			if (token == BANG)
			{
				c = x.Next();
				if (c == '-')
				{
					if (x.Next() == '-')
					{
						x.SkipPast("-->");
						return false;
					}
					x.Back();
				}
				else
				{
					if (c == '[')
					{
						token = x.NextToken();
						if ("CDATA".Equals(token))
						{
							if (x.Next() == '[')
							{
								@string = x.NextCDATA();
								if (@string.Length > 0)
								{
									context.Accumulate("content", @string);
								}
								return false;
							}
						}
						throw x.SyntaxError("Expected 'CDATA['");
					}
				}
				i = 1;
				do
				{
					token = x.NextMeta();
					if (token == null)
					{
						throw x.SyntaxError("Missing '>' after '<!'.");
					}
					else
					{
						if (token == LT)
						{
							i += 1;
						}
						else
						{
							if (token == GT)
							{
								i -= 1;
							}
						}
					}
				}
				while (i > 0);
				return false;
			}
			else
			{
				if (token == QUEST)
				{
					// <?
					x.SkipPast("?>");
					return false;
				}
				else
				{
					if (token == SLASH)
					{
						// Close tag </
						token = x.NextToken();
						if (name == null)
						{
							throw x.SyntaxError("Mismatched close tag " + token);
						}
						if (!token.Equals(name))
						{
							throw x.SyntaxError("Mismatched " + name + " and " + token);
						}
						if (x.NextToken() != GT)
						{
							throw x.SyntaxError("Misshaped close tag");
						}
						return true;
					}
					else
					{
						if (token is char)
						{
							throw x.SyntaxError("Misshaped tag");
						}
						else
						{
							// Open tag <
							tagName = (string)token;
							token = null;
							jsonobject = new org.json.JSONObject();
							for (; ; )
							{
								if (token == null)
								{
									token = x.NextToken();
								}
								// attribute = value
								if (token is string)
								{
									@string = (string)token;
									token = x.NextToken();
									if (token == EQ)
									{
										token = x.NextToken();
										if (!(token is string))
										{
											throw x.SyntaxError("Missing value");
										}
										jsonobject.Accumulate(@string, org.json.XML.StringToValue((string)token));
										token = null;
									}
									else
									{
										jsonobject.Accumulate(@string, string.Empty);
									}
								}
								else
								{
									// Empty tag <.../>
									if (token == SLASH)
									{
										if (x.NextToken() != GT)
										{
											throw x.SyntaxError("Misshaped tag");
										}
										if (jsonobject.Length() > 0)
										{
											context.Accumulate(tagName, jsonobject);
										}
										else
										{
											context.Accumulate(tagName, string.Empty);
										}
										return false;
									}
									else
									{
										// Content, between <...> and </...>
										if (token == GT)
										{
											for (; ; )
											{
												token = x.NextContent();
												if (token == null)
												{
													if (tagName != null)
													{
														throw x.SyntaxError("Unclosed tag " + tagName);
													}
													return false;
												}
												else
												{
													if (token is string)
													{
														@string = (string)token;
														if (@string.Length > 0)
														{
															jsonobject.Accumulate("content", org.json.XML.StringToValue(@string));
														}
													}
													else
													{
														// Nested element
														if (token == LT)
														{
															if (Parse(x, jsonobject, tagName))
															{
																if (jsonobject.Length() == 0)
																{
																	context.Accumulate(tagName, string.Empty);
																}
																else
																{
																	if (jsonobject.Length() == 1 && jsonobject.Opt("content") != null)
																	{
																		context.Accumulate(tagName, jsonobject.Opt("content"));
																	}
																	else
																	{
																		context.Accumulate(tagName, jsonobject);
																	}
																}
																return false;
															}
														}
													}
												}
											}
										}
										else
										{
											throw x.SyntaxError("Misshaped tag");
										}
									}
								}
							}
						}
					}
				}
			}
		}
		/*
		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>Get the next value.</summary>
		/// <remarks>
		/// Get the next value. The value can be wrapped in quotes. The value can
		/// be empty.
		/// </remarks>
		/// <param name="x">A JSONTokener of the source text.</param>
		/// <returns>The value string, or null if empty.</returns>
		/// <exception cref="JSONException">if the quoted string is badly formed.</exception>
		/// <exception cref="org.json.JSONException"/>
		private static string GetValue(org.json.JSONTokener x)
		{
			char c;
			char q;
			System.Text.StringBuilder sb;
			do
			{
				c = x.Next();
			}
			while (c == ' ' || c == '\t');
			switch (c)
			{
				case 0:
				{
					return null;
				}

				case '"':
				case '\'':
				{
					q = c;
					sb = new System.Text.StringBuilder();
					for (; ; )
					{
						c = x.Next();
						if (c == q)
						{
							break;
						}
						if (c == 0 || c == '\n' || c == '\r')
						{
							throw x.SyntaxError("Missing close quote '" + q + "'.");
						}
						sb.Append(c);
					}
					return sb.ToString();
				}

				case ',':
				{
					x.Back();
					return string.Empty;
				}

				default:
				{
					x.Back();
					return x.NextTo(',');
				}
			}
		}
		/*
		Copyright (c) 2008 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>Parse XML values and store them in a JSONArray.</summary>
		/// <param name="x">The XMLTokener containing the source string.</param>
		/// <param name="arrayForm">true if array form, false if object form.</param>
		/// <param name="ja">
		/// The JSONArray that is containing the current tag or null
		/// if we are at the outermost level.
		/// </param>
		/// <returns>A JSONArray if the value is the outermost tag, otherwise null.</returns>
		/// <exception cref="JSONException"/>
		/// <exception cref="org.json.JSONException"/>
		private static object Parse(org.json.XMLTokener x, bool arrayForm, org.json.JSONArray ja)
		{
			string attribute;
			char c;
			string closeTag = null;
			int i;
			org.json.JSONArray newja = null;
			org.json.JSONObject newjo = null;
			object token;
			string tagName = null;
			// Test for and skip past these forms:
			//      <!-- ... -->
			//      <![  ... ]]>
			//      <!   ...   >
			//      <?   ...  ?>
			while (true)
			{
				if (!x.More())
				{
					throw x.SyntaxError("Bad XML");
				}
				token = x.NextContent();
				if (token == org.json.XML.LT)
				{
					token = x.NextToken();
					if (token is char)
					{
						if (token == org.json.XML.SLASH)
						{
							// Close tag </
							token = x.NextToken();
							if (!(token is string))
							{
								throw new org.json.JSONException("Expected a closing name instead of '" + token + "'.");
							}
							if (x.NextToken() != org.json.XML.GT)
							{
								throw x.SyntaxError("Misshaped close tag");
							}
							return token;
						}
						else
						{
							if (token == org.json.XML.BANG)
							{
								// <!
								c = x.Next();
								if (c == '-')
								{
									if (x.Next() == '-')
									{
										x.SkipPast("-->");
									}
									else
									{
										x.Back();
									}
								}
								else
								{
									if (c == '[')
									{
										token = x.NextToken();
										if (token.Equals("CDATA") && x.Next() == '[')
										{
											if (ja != null)
											{
												ja.Put(x.NextCDATA());
											}
										}
										else
										{
											throw x.SyntaxError("Expected 'CDATA['");
										}
									}
									else
									{
										i = 1;
										do
										{
											token = x.NextMeta();
											if (token == null)
											{
												throw x.SyntaxError("Missing '>' after '<!'.");
											}
											else
											{
												if (token == org.json.XML.LT)
												{
													i += 1;
												}
												else
												{
													if (token == org.json.XML.GT)
													{
														i -= 1;
													}
												}
											}
										}
										while (i > 0);
									}
								}
							}
							else
							{
								if (token == org.json.XML.QUEST)
								{
									// <?
									x.SkipPast("?>");
								}
								else
								{
									throw x.SyntaxError("Misshaped tag");
								}
							}
						}
					}
					else
					{
						// Open tag <
						if (!(token is string))
						{
							throw x.SyntaxError("Bad tagName '" + token + "'.");
						}
						tagName = (string)token;
						newja = new org.json.JSONArray();
						newjo = new org.json.JSONObject();
						if (arrayForm)
						{
							newja.Put(tagName);
							if (ja != null)
							{
								ja.Put(newja);
							}
						}
						else
						{
							newjo.Put("tagName", tagName);
							if (ja != null)
							{
								ja.Put(newjo);
							}
						}
						token = null;
						for (; ; )
						{
							if (token == null)
							{
								token = x.NextToken();
							}
							if (token == null)
							{
								throw x.SyntaxError("Misshaped tag");
							}
							if (!(token is string))
							{
								break;
							}
							// attribute = value
							attribute = (string)token;
							if (!arrayForm && ("tagName".Equals(attribute) || "childNode".Equals(attribute)))
							{
								throw x.SyntaxError("Reserved attribute.");
							}
							token = x.NextToken();
							if (token == org.json.XML.EQ)
							{
								token = x.NextToken();
								if (!(token is string))
								{
									throw x.SyntaxError("Missing value");
								}
								newjo.Accumulate(attribute, org.json.XML.StringToValue((string)token));
								token = null;
							}
							else
							{
								newjo.Accumulate(attribute, string.Empty);
							}
						}
						if (arrayForm && newjo.Length() > 0)
						{
							newja.Put(newjo);
						}
						// Empty tag <.../>
						if (token == org.json.XML.SLASH)
						{
							if (x.NextToken() != org.json.XML.GT)
							{
								throw x.SyntaxError("Misshaped tag");
							}
							if (ja == null)
							{
								if (arrayForm)
								{
									return newja;
								}
								else
								{
									return newjo;
								}
							}
						}
						else
						{
							// Content, between <...> and </...>
							if (token != org.json.XML.GT)
							{
								throw x.SyntaxError("Misshaped tag");
							}
							closeTag = (string)Parse(x, arrayForm, newja);
							if (closeTag != null)
							{
								if (!closeTag.Equals(tagName))
								{
									throw x.SyntaxError("Mismatched '" + tagName + "' and '" + closeTag + "'");
								}
								tagName = null;
								if (!arrayForm && newja.Length() > 0)
								{
									newjo.Put("childNodes", newja);
								}
								if (ja == null)
								{
									if (arrayForm)
									{
										return newja;
									}
									else
									{
										return newjo;
									}
								}
							}
						}
					}
				}
				else
				{
					if (ja != null)
					{
						ja.Put(token is string ? org.json.XML.StringToValue((string)token) : token);
					}
				}
			}
		}
		/// <summary>Construct a JSONObject from a JSONTokener.</summary>
		/// <param name="x">A JSONTokener object containing the source string.</param>
		/// <exception cref="JSONException">
		/// If there is a syntax error in the source string or a
		/// duplicated key.
		/// </exception>
		/// <exception cref="org.json.JSONException"/>
		public JSONObject(org.json.JSONTokener x)
			: this()
		{
			char c;
			string key;
			if (x.NextClean() != '{')
			{
				throw x.SyntaxError("A JSONObject text must begin with '{'");
			}
			for (; ; )
			{
				c = x.NextClean();
				switch (c)
				{
					case 0:
					{
						throw x.SyntaxError("A JSONObject text must end with '}'");
					}

					case '}':
					{
						return;
					}

					default:
					{
						x.Back();
						key = x.NextValue().ToString();
						break;
					}
				}
				// The key is followed by ':'.
				c = x.NextClean();
				if (c != ':')
				{
					throw x.SyntaxError("Expected a ':' after a key");
				}
				this.PutOnce(key, x.NextValue());
				switch (x.NextClean())
				{
					case ';':
					case ',':
					{
						// Pairs are separated by ','.
						if (x.NextClean() == '}')
						{
							return;
						}
						x.Back();
						break;
					}

					case '}':
					{
						return;
					}

					default:
					{
						throw x.SyntaxError("Expected a ',' or '}'");
					}
				}
			}
		}