public static SqlMap <string, string> ExtractBody(String json)
        {
            JToken root = null;

            try
            {
                root = string.IsNullOrEmpty(json) ? new JObject() : JToken.Parse(json);
            }
            catch (Newtonsoft.Json.JsonReaderException e)
            {
                Console.WriteLine("Error while reading json string");
                return(SqlMap.Create(EMPTYRESULT));
            }

            var o = root as JObject;

            IEnumerable <JToken> tokens;

            if (o != null)
            {
                tokens = o.PropertyValues();
            }
            else
            {
                tokens = root.Children();
            }

            return(SqlMap.Create(TransformTokens(tokens)));
        }
示例#2
0
        public static SqlMap <string, T> JsonTuple <T>(string json, params string[] paths)
        {
            // Parse (once)
            //  Note: Json.Net NullRefs on <null> input Json
            //        Given <null> is a common column/string value, map to empty set for composability
            var root = string.IsNullOrEmpty(json) ? new JObject() : JToken.Parse(json);

            // Apply paths
            if (paths != null && paths.Length > 0)
            {
                return(SqlMap.Create(paths.SelectMany(path => ApplyPath <T>(root, path))));
            }

            // Children
            return(SqlMap.Create(ApplyPath <T>(root, null)));
        }
        private void ExtractJson(string json, IUpdatableRow output)
        {
            //var jsonReader = new JsonTextReader(new StringReader(tokens[0]));
            var jsonReader = new JsonTextReader(new StringReader(json));

            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.StartObject)
                {
                    var token = JToken.Load(jsonReader);
                    // Rows
                    //  All objects are represented as rows
                    foreach (JObject o in token.SelectTokens("data[*]"))
                    {
                        //Console.WriteLine($"JObject: {o}");
                        // All fields are represented as columns
                        //this.JObjectToRow(o, output);
                        //mapToColumns(o, output);
                        //yield return output.AsReadOnly();
                        //var tokens = o.SelectTokens("$.data.author");
                        var sqlMap = SqlMap.Create(MapHelper <string>(o, "$.data.author"));
                        if (sqlMap != null && sqlMap.Count > 0)
                        {
                            Console.WriteLine("SqlMap exists");
                            var tempMap = output.Get <SqlMap <string, string> >("contexts.data.author");

                            if (tempMap != null && tempMap.Count > 0)
                            {
                                Console.WriteLine("tempMAp exists");

                                var combinedDict = new Dictionary <string, string>();
                                tempMap.Keys.ToList().ForEach(key => combinedDict.Add(key, tempMap[key]));
                                sqlMap.Keys.ToList().ForEach(key => combinedDict.Add(key, sqlMap[key]));

                                output.Set("contexts.data.author", new SqlMap <string, string>(combinedDict));
                            }
                            else
                            {
                                output.Set("contexts.data.author", sqlMap);
                            }
                        }
                    }
                }
            }
        }
        /// <summary/>
        public static SqlMap <string, T> JsonTuple <T>(string json, params string[] paths)
        {
            // Parse (once)
            //  Note: Json.Net NullRefs on <null> input Json
            //        Given <null> is a common column/string value, map to empty set for composability
            var root = string.IsNullOrEmpty(json) ? new JObject() : JToken.Parse(json);

            //Console.WriteLine($"Root: {root}");
            //Console.WriteLine($"Path length: {paths.Length}, path: {paths[0]}");
            // Apply paths
            if (paths != null && paths.Length > 0)
            {
                var schemaMap = SqlMap.Create(paths.SelectMany(path => ApplyPath <T>(root, path)));
                //  Console.WriteLine($"Schema Map Count: {schemaMap.Count}");

                return(schemaMap);
            }

            // Children
            return(SqlMap.Create(ApplyPath <T>(root, null)));
        }