Ejemplo n.º 1
0
		/*
		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);
					}
				}
			}
		}
Ejemplo n.º 2
0
		/// <summary>Convert a JSONObject into a well-formed, element-normal XML string.</summary>
		/// <param name="object">A JSONObject.</param>
		/// <param name="tagName">The optional name of the enclosing tag.</param>
		/// <returns>A string.</returns>
		/// <exception cref="JSONException"/>
		/// <exception cref="org.json.JSONException"/>
		public static string ToString(object @object, string tagName)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			int i;
			org.json.JSONArray ja;
			org.json.JSONObject jo;
			string key;
			System.Collections.Generic.IEnumerator<string> keys;
			int length;
			string @string;
			object value;
			if (@object is org.json.JSONObject)
			{
				// Emit <tagName>
				if (tagName != null)
				{
					sb.Append('<');
					sb.Append(tagName);
					sb.Append('>');
				}
				// Loop thru the keys.
				jo = (org.json.JSONObject)@object;
				keys = jo.Keys();
				while (keys.HasNext())
				{
					key = keys.Next();
					value = jo.Opt(key);
					if (value == null)
					{
						value = string.Empty;
					}
					@string = value is string ? (string)value : null;
					// Emit content in body
					if ("content".Equals(key))
					{
						if (value is org.json.JSONArray)
						{
							ja = (org.json.JSONArray)value;
							length = ja.Length();
							for (i = 0; i < length; i += 1)
							{
								if (i > 0)
								{
									sb.Append('\n');
								}
								sb.Append(Escape(ja.Get(i).ToString()));
							}
						}
						else
						{
							sb.Append(Escape(value.ToString()));
						}
					}
					else
					{
						// Emit an array of similar keys
						if (value is org.json.JSONArray)
						{
							ja = (org.json.JSONArray)value;
							length = ja.Length();
							for (i = 0; i < length; i += 1)
							{
								value = ja.Get(i);
								if (value is org.json.JSONArray)
								{
									sb.Append('<');
									sb.Append(key);
									sb.Append('>');
									sb.Append(ToString(value));
									sb.Append("</");
									sb.Append(key);
									sb.Append('>');
								}
								else
								{
									sb.Append(ToString(value, key));
								}
							}
						}
						else
						{
							if (string.Empty.Equals(value))
							{
								sb.Append('<');
								sb.Append(key);
								sb.Append("/>");
							}
							else
							{
								// Emit a new tag <k>
								sb.Append(ToString(value, key));
							}
						}
					}
				}
				if (tagName != null)
				{
					// Emit the </tagname> close tag
					sb.Append("</");
					sb.Append(tagName);
					sb.Append('>');
				}
				return sb.ToString();
			}
			else
			{
				// XML does not have good support for arrays. If an array appears in a place
				// where XML is lacking, synthesize an <array> element.
				if (@object.GetType().IsArray)
				{
					@object = new org.json.JSONArray(@object);
				}
				if (@object is org.json.JSONArray)
				{
					ja = (org.json.JSONArray)@object;
					length = ja.Length();
					for (i = 0; i < length; i += 1)
					{
						sb.Append(ToString(ja.Opt(i), tagName == null ? "array" : tagName));
					}
					return sb.ToString();
				}
				else
				{
					@string = (@object == null) ? "null" : Escape(@object.ToString());
					return (tagName == null) ? "\"" + @string + "\"" : (@string.Length == 0) ? "<" + tagName + "/>" : "<" + tagName + ">" + @string + "</" + tagName + ">";
				}
			}
		}
Ejemplo n.º 3
0
		/// <summary>
		/// Produce a JSONArray of JSONObjects from a comma delimited text string
		/// using a supplied JSONArray as the source of element names.
		/// </summary>
		/// <param name="names">A JSONArray of strings.</param>
		/// <param name="x">A JSONTokener of the source text.</param>
		/// <returns>A JSONArray of JSONObjects.</returns>
		/// <exception cref="JSONException"/>
		/// <exception cref="org.json.JSONException"/>
		public static org.json.JSONArray ToJSONArray(org.json.JSONArray names, org.json.JSONTokener x)
		{
			if (names == null || names.Length() == 0)
			{
				return null;
			}
			org.json.JSONArray ja = new org.json.JSONArray();
			for (; ; )
			{
				org.json.JSONObject jo = RowToJSONObject(names, x);
				if (jo == null)
				{
					break;
				}
				ja.Put(jo);
			}
			if (ja.Length() == 0)
			{
				return null;
			}
			return ja;
		}
Ejemplo n.º 4
0
		/// <summary>Produce a JSONArray of strings from a row of comma delimited values.</summary>
		/// <param name="x">A JSONTokener of the source text.</param>
		/// <returns>A JSONArray of strings.</returns>
		/// <exception cref="JSONException"/>
		/// <exception cref="org.json.JSONException"/>
		public static org.json.JSONArray RowToJSONArray(org.json.JSONTokener x)
		{
			org.json.JSONArray ja = new org.json.JSONArray();
			for (; ; )
			{
				string value = GetValue(x);
				char c = x.Next();
				if (value == null || (ja.Length() == 0 && value.Length == 0 && c != ','))
				{
					return null;
				}
				ja.Put(value);
				for (; ; )
				{
					if (c == ',')
					{
						break;
					}
					if (c != ' ')
					{
						if (c == '\n' || c == '\r' || c == 0)
						{
							return ja;
						}
						throw x.SyntaxError("Bad character '" + c + "' (" + (int)c + ").");
					}
					c = x.Next();
				}
			}
		}
		/// <summary>
		/// Produce a JSONArray containing the names of the elements of this
		/// JSONObject.
		/// </summary>
		/// <returns>
		/// A JSONArray containing the key strings, or null if the JSONObject
		/// is empty.
		/// </returns>
		public virtual org.json.JSONArray Names()
		{
			org.json.JSONArray ja = new org.json.JSONArray();
			System.Collections.Generic.IEnumerator<string> keys = this.Keys();
			while (keys.HasNext())
			{
				ja.Put(keys.Next());
			}
			return ja.Length() == 0 ? null : ja;
		}
		/// <summary>
		/// Produce a JSONArray containing the values of the members of this
		/// JSONObject.
		/// </summary>
		/// <param name="names">
		/// A JSONArray containing a list of key strings. This determines
		/// the sequence of the values in the result.
		/// </param>
		/// <returns>A JSONArray of values.</returns>
		/// <exception cref="JSONException">If any of the values are non-finite numbers.</exception>
		/// <exception cref="org.json.JSONException"/>
		public virtual org.json.JSONArray ToJSONArray(org.json.JSONArray names)
		{
			if (names == null || names.Length() == 0)
			{
				return null;
			}
			org.json.JSONArray ja = new org.json.JSONArray();
			for (int i = 0; i < names.Length(); i += 1)
			{
				ja.Put(this.Opt(names.GetString(i)));
			}
			return ja;
		}
 internal JSONArrayEnumerator(org.json.JSONArray jsonArray)
 {
     m_bStarted     = false;
     m_currentIndex = -1;
     m_jsonArray    = jsonArray;
 }