public void Bad_Array_Path_Throws_Exception()
        {
            var selector = "channel.item[*].bad";
            var parser   = new JsonObjectParser();
            var ex       = Assert.Throws <Exception>(delegate { parser.Parse(JSON_WITH_ARRAYS, selector); });

            Assert.AreEqual(ex.Message, "The selector path channel.item[*].bad was not found.", "Wrong exception was thrown");
        }
        public void Bad_Path_Throws_Exception()
        {
            var selector = "query.results.channel.wind.xxxx,query.results.channel.wind.speed";
            var parser   = new JsonObjectParser();
            var ex       = Assert.Throws <Exception>(delegate { parser.Parse(JSON, selector); });

            Assert.AreEqual(ex.Message, "The selector path query.results.channel.wind.xxxx was not found.", "Wrong exception was thrown");
        }
        public void Can_Create_From_Json()
        {
            var     selector   = "query.results.channel.wind.chill,query.results.channel.wind.speed";
            var     parser     = new JsonObjectParser();
            var     jsonString = parser.Parse(JSON, selector);
            dynamic result     = JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

            Assert.IsNotNull(result, "result was null");
            Assert.AreEqual("52", result.chill, "chill was not correct.");
            Assert.AreEqual("32", result.speed, "speed was not correct.");
        }
Example #4
0
        public void Can_Convert_Xml_And_Parse_Json()
        {
            var     json       = XmlToJson.Convert(xml);
            var     selector   = "query.results.channel.wind.chill,query.results.channel.wind.speed";
            var     parser     = new JsonObjectParser();
            var     jsonString = parser.Parse(json, selector);
            dynamic result     = JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

            Assert.IsNotNull(result, "result was null");
            Assert.AreEqual("59", result.chill, "chill was not correct.");
            Assert.AreEqual("18", result.speed, "speed was not correct.");
        }
        public void Empty_Template_Returns_Full_Json()
        {
            var     selector   = string.Empty;
            var     parser     = new JsonObjectParser();
            var     jsonString = parser.Parse(JSON, selector);
            dynamic result     = JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

            Assert.IsNotNull(result, "result was null");
            Assert.AreEqual("52", result.query.results.channel.wind.chill, "chill was not correct.");
            Assert.AreEqual("32", result.query.results.channel.wind.speed, "speed was not correct.");
            Assert.AreEqual("55", result.query.results.channel.wind.direction, "direction was not correct.");
        }
        public void Can_Parse_Json_With_Arrays()
        {
            var     selector   = "channel.item[*].title";
            var     parser     = new JsonObjectParser();
            var     jsonString = parser.Parse(JSON_WITH_ARRAYS, selector);
            dynamic result     = JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

            Assert.IsNotNull(result, "result was null");
            Assert.IsNotNull(result.title, "title was null");
            Assert.AreEqual(2, result.title.Count, "title length not correct.");
            Assert.AreEqual("Json.NET 1.3 + New license + Now on CodePlex", result.title[0], "first title is not correct.");
            Assert.AreEqual("LINQ to JSON beta", result.title[1], "second title is not correct.");
        }
Example #7
0
 protected object ParseJsonObject()
 {
     try
     {
         JsonObjectParser objParser = new JsonObjectParser(jsonContent.Substring(currentPosition));
         object           result    = objParser.Parse();
         currentToken = jsonContent[currentPosition += objParser.currentPosition];
         MatchToken('}');
         return(result);
     }
     catch
     {
         throw;
     }
 }
        public void Can_Parse_Json_With_Nested_Array()
        {
            var     selector   = "channel.item[*].categories[*]";
            var     parser     = new JsonObjectParser();
            var     jsonString = parser.Parse(JSON_WITH_ARRAYS, selector);
            dynamic result     = JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

            Assert.IsNotNull(result, "result was null");
            Assert.IsNotNull(result.categories, "categories array was null");
            Assert.AreEqual(4, result.categories.Count, "categories length not correct.");
            Assert.AreEqual("Json.NET", result.categories[0], "first categories is not correct.");
            Assert.AreEqual("CodePlex", result.categories[1], "second categories is not correct.");
            Assert.AreEqual("Json.NET", result.categories[2], "third categories is not correct.");
            Assert.AreEqual("LINQ", result.categories[3], "fourth categories is not correct.");
        }
        public void Can_Parse_Json_With_Arrays_And_Properties()
        {
            var     selector   = "channel.description,channel.link,channel.item[*].title";
            var     parser     = new JsonObjectParser();
            var     jsonString = parser.Parse(JSON_WITH_ARRAYS, selector);
            dynamic result     = JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

            Assert.IsNotNull(result, "result was null");
            Assert.IsNotNull(result.description, "description was null");
            Assert.AreEqual("James Newton - King\'s blog.", result.description, "description is not correct.");
            Assert.IsNotNull(result.link, "link was null");
            Assert.AreEqual("http://james.newtonking.com", result.link, "link is not correct.");
            Assert.AreEqual(2, result.title.Count, "title length not correct.");
            Assert.AreEqual("Json.NET 1.3 + New license + Now on CodePlex", result.title[0], "first title is not correct.");
            Assert.AreEqual("LINQ to JSON beta", result.title[1], "second title is not correct.");
        }
Example #10
0
        /// <summary>
        /// Parses the record field.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="parent">The parent schema.</param>
        /// <param name="namedSchemas">The named schemas.</param>
        /// <param name="position">The position.</param>
        /// <returns>
        /// Schema internal representation.
        /// </returns>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when <paramref name="field"/> is not valid or when sort order is not valid.</exception>
        private RecordField ParseRecordField(JObject field, NamedSchema parent, Dictionary<string, NamedSchema> namedSchemas, int position)
        {
            var name = field.RequiredProperty<string>(Token.Name);
            var doc = field.OptionalProperty<string>(Token.Doc);
            var order = field.OptionalProperty<string>(Token.Order);
            var aliases = this.GetAliases(field, parent.FullName);
            var fieldType = field[Token.Type];
            if (fieldType == null)
            {
                throw new SerializationException(
                    string.Format(CultureInfo.InvariantCulture, "Record field schema '{0}' has no type.", field));
            }

            TypeSchema type = this.Parse(fieldType, parent, namedSchemas);
            object defaultValue = null;
            bool hasDefaultValue = field[Token.Default] != null;
            if (hasDefaultValue)
            {
                var objectParser = new JsonObjectParser();
                defaultValue = objectParser.Parse(type, field[Token.Default].ToString());
            }

            var orderValue = SortOrder.Ascending;
            if (!string.IsNullOrEmpty(order))
            {
                if (!SortValue.ContainsKey(order.ToUpperInvariant()))
                {
                    throw new SerializationException(
                        string.Format(CultureInfo.InvariantCulture, "Invalid sort order of the field '{0}'.", order));
                }
                orderValue = SortValue[order.ToUpperInvariant()];
            }

            var fieldName = new SchemaName(name);
            var attributes = new NamedEntityAttributes(fieldName, aliases, doc);

            return new RecordField(attributes, type, orderValue, hasDefaultValue, defaultValue, null, position);
        }