示例#1
0
文件: Json.cs 项目: Vita-Nex/Core
        /// <summary>
        ///     Output: Dictionary[string, object] (Object), IEnumerable[object] (Array), Simple Types
        /// </summary>
        public static object Decode(string json, out JsonException e)
        {
            object value;

            if (Decode(json, out value, out e))
            {
                return value;
            }

            return null;
        }
示例#2
0
文件: Json.cs 项目: Ravenwolfe/Core
		/// <summary>
		///     Input: IDictionary (Object), IEnumerable (Array), Simple Types
		/// </summary>
		public static string Encode(object value, out JsonException e)
		{
			string json;

			if (Encode(value, out json, out e))
			{
				return json;
			}

			return null;
		}
示例#3
0
文件: Json.cs 项目: Ravenwolfe/Core
		/// <summary>
		///     Input: IDictionary (Object), IEnumerable (Array), Simple Types
		/// </summary>
		public static bool Encode(object value, out string json, out JsonException e)
		{
			json = null;
			e = null;

			try
			{
				var sb = new StringBuilder(0x800);

				if (SerializeValue(value, sb))
				{
					json = sb.ToString();
					return true;
				}

				int line;
				int col;

				var sub = sb.ToString();
				var idx = Math.Max(sub.LastIndexOf('\r'), sub.LastIndexOf('\n'));

				if (idx > -1)
				{
					line = 1 + sub.Count(c => c == '\r' || c == '\n');
					col = sub.Length - idx;
				}
				else
				{
					line = 1;
					col = sub.Length;
				}

				sub = sub.Substring(Math.Max(0, sub.Length - 16));

				e = new JsonException(String.Format("Encoding failed at line {0} col {1} near {2}", line, col, sub));
			}
			catch (Exception x)
			{
				if (x is JsonException)
				{
					e = (JsonException)x;
				}
				else
				{
					e = new JsonException("Encoding failed", x);
				}
			}

			return false;
		}
示例#4
0
文件: Json.cs 项目: Vita-Nex/Core
        /// <summary>
        ///     Output: Dictionary[string, object] (Object), IEnumerable[object] (Array), Simple Types
        /// </summary>
        public static bool Decode(string json, out object value, out JsonException e)
        {
            value = null;
            e = null;

            try
            {
                var index = 0;

                if (DeserializeValue(json, ref index, out value))
                {
                    return true;
                }

                int line;
                int col;

                var sub = json.Substring(0, index);
                var idx = Math.Max(sub.LastIndexOf('\r'), sub.LastIndexOf('\n'));

                if (idx > -1)
                {
                    line = 1 + sub.Count(c => c == '\r' || c == '\n');
                    col = index - idx;
                }
                else
                {
                    line = 1;
                    col = index;
                }

                sub = json.Substring(index, Math.Min(16, json.Length - index));

                e = new JsonException(String.Format("Decoding failed at line {0} col {1} near {2}", line, col, sub));
            }
            catch (Exception x)
            {
                if (x is JsonException)
                {
                    e = (JsonException)x;
                }
                else
                {
                    e = new JsonException("Decoding failed", x);
                }
            }

            return false;
        }