Пример #1
0
        public void EvaluateConstructorOutOfBoundsIndxer()
        {
            JConstructor c = new JConstructor("Blah");

            Assert.IsNull(c.SelectToken("[1]"));
        }
        private void WriteToken(JSchema context, JsonWriter writer, JToken token, bool isDefinitions = false)
        {
            if (token is JObject o)
            {
                JSchemaAnnotation schemaAnnotation = o.Annotation <JSchemaAnnotation>();

                if (schemaAnnotation != null)
                {
                    ReferenceOrWriteSchema(context, schemaAnnotation.Schema, null, isDefinitions);
                }
                else
                {
                    writer.WriteStartObject();

                    foreach (JProperty property in o.Properties())
                    {
                        writer.WritePropertyName(property.Name);

                        JToken value = property.Value;
                        if (value != null)
                        {
                            WriteToken(context, writer, value, isDefinitions);
                        }
                        else
                        {
                            writer.WriteNull();
                        }
                    }

                    writer.WriteEndObject();
                }
            }
            else if (token is JArray)
            {
                JArray a = (JArray)token;

                writer.WriteStartArray();

                for (int i = 0; i < a.Count; i++)
                {
                    WriteToken(context, writer, a[i]);
                }

                writer.WriteEndArray();
            }
            else if (token is JConstructor)
            {
                JConstructor c = (JConstructor)token;

                writer.WriteStartConstructor(c.Name);

                foreach (JToken t in c.Children())
                {
                    WriteToken(context, writer, t);
                }

                writer.WriteEndConstructor();
            }
            else if (token is JValue)
            {
                token.WriteTo(writer);
            }
        }
Пример #3
0
        public void EvaluateConstructorOutOfBoundsIndxerWithError()
        {
            JConstructor c = new JConstructor("Blah");

            ExceptionAssert.Throws <JsonException>(() => { c.SelectToken("[1]", true); }, @"Index 1 outside the bounds of JConstructor.");
        }
Пример #4
0
 public JConstructor EchoJConstructor(JConstructor input)
 {
     return(input);
 }
Пример #5
0
        public void EvaluateIndexerOnConstructorWithError()
        {
            JConstructor c = new JConstructor("Blah");

            c.SelectToken("[1]", true);
        }
Пример #6
0
 /// <summary>
 /// Visits the specified constructor.
 /// </summary>
 /// <param name="json">The constructor.</param>
 /// <param name="context">The context.</param>
 protected virtual void Visit(JConstructor json, TContext context) => this.Visit((JContainer)json, context);
Пример #7
0
        public void JConstructorStringIndex()
        {
            JConstructor c = new JConstructor("ConstructorValue");

            Assert.AreEqual(null, c["purple"]);
        }
Пример #8
0
 public virtual void Visit(JConstructor json)
 {
 }
Пример #9
0
 private XjsltJson ReadConstructor(JConstructor n)
 {
     throw new NotImplementedException();
 }
Пример #10
0
        public async Task ReadFromAsync()
        {
            JObject o = (JObject)await JToken.ReadFromAsync(
                new JsonTextReader(new StringReader("{'pie':true}"))
                );

            Assert.AreEqual(true, (bool)o["pie"]);

            JArray a = (JArray)await JToken.ReadFromAsync(
                new JsonTextReader(new StringReader("[1,2,3]"))
                );

            Assert.AreEqual(1, (int)a[0]);
            Assert.AreEqual(2, (int)a[1]);
            Assert.AreEqual(3, (int)a[2]);

            JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
            await reader.ReadAsync();

            await reader.ReadAsync();

            JProperty p = (JProperty)await JToken.ReadFromAsync(reader);

            Assert.AreEqual("pie", p.Name);
            Assert.AreEqual(true, (bool)p.Value);

            JConstructor c = (JConstructor)await JToken.ReadFromAsync(
                new JsonTextReader(new StringReader("new Date(1)"))
                );

            Assert.AreEqual("Date", c.Name);
            Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));

            JValue v = (JValue)await JToken.ReadFromAsync(
                new JsonTextReader(new StringReader(@"""stringvalue"""))
                );

            Assert.AreEqual("stringvalue", (string)v);

            v = (JValue)await JToken.ReadFromAsync(new JsonTextReader(new StringReader(@"1")));

            Assert.AreEqual(1, (int)v);

            v = (JValue)await JToken.ReadFromAsync(new JsonTextReader(new StringReader(@"1.1")));

            Assert.AreEqual(1.1, (double)v);

            v = (JValue)await JToken.ReadFromAsync(
                new JsonTextReader(new StringReader(@"""1970-01-01T00:00:00+12:31"""))
            {
                DateParseHandling = DateParseHandling.DateTimeOffset
            }
                );

            Assert.AreEqual(typeof(DateTimeOffset), v.Value.GetType());
            Assert.AreEqual(
                new DateTimeOffset(
                    DateTimeUtils.InitialJavaScriptDateTicks,
                    new TimeSpan(12, 31, 0)
                    ),
                v.Value
                );
        }
Пример #11
0
        public void EvaluateConstructorOutOfBoundsIndxerWithError()
        {
            JConstructor c = new JConstructor("Blah");

            c.SelectToken("[1]", true);
        }
Пример #12
0
        public void SetValueWithInvalidIndex()
        {
            JConstructor c = new JConstructor();

            c["badvalue"] = new JValue(3);
        }
 /// <inheritdoc />
 protected override void Visit(JConstructor json, EmitterContext context)
 {
     context.Emit(new ScalarValue(Reference.FromJConstructor(json)));
 }
Пример #14
0
        internal JToken method_2(JToken jtoken_0, bool bool_0)
        {
            JToken token = jtoken_0;

            using (List <object> .Enumerator enumerator = this.List_0.GetEnumerator())
            {
                string str;
                int    num;
                while (enumerator.MoveNext())
                {
                    object current = enumerator.Current;
                    str = current as string;
                    if (str != null)
                    {
                        JObject obj3 = token as JObject;
                        if (obj3 != null)
                        {
                            token = obj3[str];
                            if ((token == null) && bool_0)
                            {
                                throw new JsonException("Property '{0}' does not exist on JObject.".smethod_0(CultureInfo.InvariantCulture, str));
                            }
                            continue;
                        }
                        goto Label_00C5;
                    }
                    num = (int)current;
                    JArray       array       = token as JArray;
                    JConstructor constructor = token as JConstructor;
                    if (array != null)
                    {
                        if (array.Count <= num)
                        {
                            goto Label_00F1;
                        }
                        token = array[num];
                    }
                    else
                    {
                        if (constructor == null)
                        {
                            goto Label_0139;
                        }
                        if (constructor.Count <= num)
                        {
                            goto Label_0115;
                        }
                        token = constructor[num];
                    }
                }
                return(token);

Label_00C5:
                if (bool_0)
                {
                    throw new JsonException("Property '{0}' not valid on {1}.".smethod_1(CultureInfo.InvariantCulture, str, token.GetType().Name));
                }
                return(null);

Label_00F1:
                if (bool_0)
                {
                    throw new JsonException("Index {0} outside the bounds of JArray.".smethod_0(CultureInfo.InvariantCulture, num));
                }
                return(null);

Label_0115:
                if (bool_0)
                {
                    throw new JsonException("Index {0} outside the bounds of JConstructor.".smethod_0(CultureInfo.InvariantCulture, num));
                }
                return(null);

Label_0139:
                if (bool_0)
                {
                    throw new JsonException("Index {0} not valid on {1}.".smethod_1(CultureInfo.InvariantCulture, num, token.GetType().Name));
                }
                return(null);
            }
        }
Пример #15
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());
        }
Пример #16
0
 protected virtual void VisitConstructor(JConstructor json, TContext context)
 {
 }