Пример #1
0
 public void CreateWithMultiValue()
 {
   JConstructor constructor = new JConstructor("Test", new List<int> { 1, 2, 3 });
   Assert.AreEqual("Test", constructor.Name);
   Assert.AreEqual(3, constructor.Children().Count());
   Assert.AreEqual(1, (int)constructor.Children().ElementAt(0));
   Assert.AreEqual(2, (int)constructor.Children().ElementAt(1));
   Assert.AreEqual(3, (int)constructor.Children().ElementAt(2));
 }
    public void JConstructorDictionary()
    {
      Dictionary<JToken, int> dic = new Dictionary<JToken, int>(JToken.EqualityComparer);
      JConstructor v11 = new JConstructor("ConstructorValue");
      JConstructor v12 = new JConstructor("ConstructorValue");

      dic[v11] = 1;
      dic[v12] += 1;
      Assert.AreEqual(2, dic[v11]);
    }
Пример #3
0
    public void Iterate()
    {
      JConstructor c = new JConstructor("MrConstructor", 1, 2, 3, 4, 5);

      int i = 1;
      foreach (JToken token in c)
      {
        Assert.AreEqual(i, (int)token);
        i++;
      }
    }
Пример #4
0
 public void JConstructorStringIndex()
 {
   ExceptionAssert.Throws<ArgumentException>(@"Accessed JConstructor values with invalid key value: ""purple"". Argument position index expected.",
   () =>
   {
     JConstructor c = new JConstructor("ConstructorValue");
     Assert.AreEqual(null, c["purple"]);
   });
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JConstructor"/> class from another <see cref="JConstructor"/> object.
 /// </summary>
 /// <param name="other">A <see cref="JConstructor"/> object to copy from.</param>
 public JConstructor(JConstructor other)
     : base(other)
 {
     _name = other.Name;
 }
Пример #6
0
        /// <summary>
        /// Loads an <see cref="JConstructor"/> from a <see cref="JsonReader"/>. 
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param>
        /// <returns>A <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static JConstructor Load(JsonReader reader)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!reader.Read())
                    throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
            }

            while (reader.TokenType == JsonToken.Comment)
            {
                reader.Read();
            }

            if (reader.TokenType != JsonToken.StartConstructor)
                throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));

            JConstructor c = new JConstructor((string)reader.Value);
            c.SetLineInfo(reader as IJsonLineInfo);

            c.ReadTokenFrom(reader);

            return c;
        }
Пример #7
0
    public void EvaluateConstructorOutOfBoundsIndxerWithError()
    {
      JConstructor c = new JConstructor("Blah");

      ExceptionAssert.Throws<IndexOutOfRangeException>(
        @"Index 1 outside the bounds of JConstructor.",
        () =>
        {
          c.SelectToken("[1]", true);
        });
    }
Пример #8
0
    public void SetValue()
    {
      object key = 0;

      JConstructor c = new JConstructor();
      c.Name = "con";
      c.Add(null);
      c[key] = new JValue(3);

      Assert.AreEqual(3, (int)c[key]);
    }
Пример #9
0
 public void SetValueWithInvalidIndex()
 {
   ExceptionAssert.Throws<ArgumentException>(@"Set JConstructor values with invalid key value: ""badvalue"". Argument position index expected.",
   () =>
   {
     JConstructor c = new JConstructor();
     c["badvalue"] = new JValue(3);
   }); 
 }
Пример #10
0
        internal void ReadContentFrom(JsonReader r)
        {
            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo = r as IJsonLineInfo;

            JContainer parent = this;

            do
            {
                if (parent is JProperty && ((JProperty)parent).Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (r.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo);
                    parent.Add(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    JObject o = new JObject();
                    o.SetLineInfo(lineInfo);
                    parent.Add(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(r.Value.ToString());
                    constructor.SetLineInfo(lineInfo);
                    parent.Add(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Date:
                case JsonToken.Boolean:
                case JsonToken.Bytes:
                    JValue v = new JValue(r.Value);
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.Comment:
                    v = JValue.CreateComment(r.Value.ToString());
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.Null:
                    v = JValue.CreateNull();
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.Undefined:
                    v = JValue.CreateUndefined();
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.PropertyName:
                    string    propertyName = r.Value.ToString();
                    JProperty property     = new JProperty(propertyName);
                    property.SetLineInfo(lineInfo);
                    JObject parentObject = (JObject)parent;
                    // handle multiple properties with the same name in JSON
                    JProperty existingPropertyWithName = parentObject.Property(propertyName);
                    if (existingPropertyWithName == null)
                    {
                        parent.Add(property);
                    }
                    else
                    {
                        existingPropertyWithName.Replace(property);
                    }
                    parent = property;
                    break;

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
            } while (r.Read());
        }
Пример #11
0
        internal JToken Evaluate(JToken root, bool errorWhenNoMatch)
        {
            JToken current = root;

            foreach (object part in Parts)
            {
                string propertyName = part as string;
                if (propertyName != null)
                {
                    JObject o = current as JObject;
                    if (o != null)
                    {
                        current = o[propertyName];

                        if (current == null && errorWhenNoMatch)
                        {
                            throw new JsonException("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, propertyName));
                        }
                    }
                    else
                    {
                        if (errorWhenNoMatch)
                        {
                            throw new JsonException("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, propertyName, current.GetType().Name));
                        }

                        return(null);
                    }
                }
                else
                {
                    int index = (int)part;

                    JArray       a = current as JArray;
                    JConstructor c = current as JConstructor;

                    if (a != null)
                    {
                        if (a.Count <= index)
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new JsonException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, index));
                            }

                            return(null);
                        }

                        current = a[index];
                    }
                    else if (c != null)
                    {
                        if (c.Count <= index)
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new JsonException("Index {0} outside the bounds of JConstructor.".FormatWith(CultureInfo.InvariantCulture, index));
                            }

                            return(null);
                        }

                        current = c[index];
                    }
                    else
                    {
                        if (errorWhenNoMatch)
                        {
                            throw new JsonException("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, index, current.GetType().Name));
                        }

                        return(null);
                    }
                }
            }

            return(current);
        }
Пример #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JConstructor"/> class from another <see cref="JConstructor"/> object.
 /// </summary>
 /// <param name="other">A <see cref="JConstructor"/> object to copy from.</param>
 public JConstructor(JConstructor other)
     : base(other)
 {
     _name = other.Name;
 }
Пример #13
0
        internal override bool DeepEquals(JToken node)
        {
            JConstructor c = node as JConstructor;

            return(c != null && _name == c.Name && ContentsEqual(c));
        }