Ejemplo n.º 1
0
 /// <summary>
 /// Tries to parse a string of json and determine is <see cref="JsonValue"/> type.
 /// </summary>
 /// <param name="json">A string of json that should be parsed.</param>
 /// <param name="value">The output <see cref="JsonValue"/> created if successful. If not successful, a <see cref="JsonNull"/> se set to <paramref name="value"/>.</param>
 /// <returns>Returns true if <see cref="JsonValue"/> was successfully created; otherwise false is returned.</returns>
 public static bool TryParse(string json, out JsonValue value)
 {
     if (string.IsNullOrEmpty(json))
     {
         value = new JsonNull();
         return true;
     }
     if (json.StartsWith("{", StringComparison.CurrentCulture) && json.EndsWith("}", StringComparison.Ordinal))
     {
         value = new JsonObject(json);
         return true;
     }
     if (json.StartsWith("[") && json.EndsWith("]"))
     {
         value = new JsonArray(json);
         return true;
     }
     if (json.StartsWith("\"") && json.EndsWith("\""))
     {
         json = json.Substring(1, json.Length - 2);
         if (json.StartsWith("base64:"))
             value = new JsonBinary(json);
         else
             value = new JsonString(json);
         return true;
     }
     decimal v;
     bool bv;
     if (decimal.TryParse(json, out v))
     {
         value = new JsonNumber(v);
         return true;
     }
     if (bool.TryParse(json, out bv))
     {
         value = new JsonBoolean(bv);
         return true;
     }
     value = null;
     return false;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of the specified type, provided by the constructor.
        /// </summary>
        /// <returns>Returns the newly created instance.</returns>
        public object AddNew()
        {
            if (newPos != -1)
                this.OnListChanged(ListChangedType.ItemAdded, newPos);

            JsonValue newValue = null;

            if (vtype == typeof(JsonString))
                newValue = new JsonString();
            else if (vtype == typeof(JsonNumber))
                newValue = new JsonNumber();
            else if (vtype == typeof(JsonBoolean))
                newValue = new JsonBoolean();
            else if (vtype == typeof(JsonObject))
            {
                JsonObject obj = new JsonObject();
                foreach (PropertyDescriptor prop in props)
                    obj.Add(prop.Name, (JsonValue)Activator.CreateInstance(prop.PropertyType));
                newValue = obj;
            }
            if (newValue != null)
            {
                newPos = List.Add(newValue);
                this.Position = newPos;
            }
            return newValue;
        }