public static DateTime ToDateTime(object input)
        {
            DateTime output = new DateTime();

            try
            {
                output = DateTime.Parse(ContentstackConvert.ToString(input));
            }
            catch { }

            return(output);
        }
Exemple #2
0
        public static DateTime ToDateTime(object input)
        {
            DateTime output = new DateTime();

            try
            {
                output = DateTime.Parse(ContentstackConvert.ToString(input));
            }
            catch (Exception e)
            {
                if (e.Source != null)
                {
                    Console.WriteLine("IOException source: {0}", e.Source);
                }
            }

            return(output);
        }
        public Dictionary <string, object> GetObjectDict(JContainer container)
        {
            Dictionary <string, object> output = new Dictionary <string, object>();

            try
            {
                foreach (JToken token in container.Children())
                {
                    if (token is JProperty)
                    {
                        JProperty item = token as JProperty;

                        if (item.Value == null)
                        {
                            output.Add(item.Name, null);
                        }
                        else
                        {
                            string valueStr = string.Empty;

                            try
                            {
                                //valueStr = ((JProperty)item).Value.ToString();
                                //test
                                if (item.Value.Type == JTokenType.Date)
                                {
                                    valueStr = JsonConvert.SerializeObject(((JProperty)item).Value, ContentstackConvert.JsonSerializerSettings);
                                    valueStr = valueStr.Replace("\"", String.Empty);
                                }
                                else
                                {
                                    valueStr = ((JProperty)item).Value.ToString();
                                }
                                //
                            }
                            catch
                            { }

                            try
                            {
                                if ((valueStr.StartsWith("{") && valueStr.EndsWith("}")) || (valueStr.StartsWith("[") && valueStr.EndsWith("]")))
                                {
                                    if ((valueStr.StartsWith("{") && valueStr.EndsWith("}")))
                                    {
                                        JContainer jContainer = (JContainer)JsonConvert.DeserializeObject(valueStr, ContentstackConvert.JsonSerializerSettings);
                                        output.Add(item.Name, this.GetObjectDict(jContainer));
                                    }
                                    else
                                    {
                                        JArray        itemArr = (JArray)JsonConvert.DeserializeObject(valueStr, ContentstackConvert.JsonSerializerSettings);
                                        List <object> objArr  = new List <object>();

                                        for (int i = 0, length = itemArr.Count(); i < length; i++)
                                        {
                                            try
                                            {
                                                var tempItem = itemArr[i];

                                                if (tempItem.GetType() == typeof(JValue))
                                                {
                                                    objArr.Add(this.GetValue(ContentstackConvert.ToString(tempItem)));
                                                }
                                                else
                                                {
                                                    objArr.Add(this.GetObjectDict((JContainer)tempItem));
                                                }
                                            }
                                            catch
                                            { }
                                        }

                                        /*
                                         * foreach (var tempItem in itemArr)
                                         * {
                                         *  try
                                         *  {
                                         *      if (tempItem.GetType() == typeof(JValue))
                                         *      {
                                         *          objArr.Add(this.GetValue(BuiltConvert.ToString(tempItem)));
                                         *      }
                                         *      else
                                         *      {
                                         *          objArr.Add(this.GetObjectDict((JContainer)tempItem));
                                         *      }
                                         *  }
                                         *  catch
                                         *  { }
                                         * }
                                         */

                                        output.Add(item.Name, objArr.ToArray());
                                    }
                                }
                                else
                                {
                                    //output[item.Name] = this.GetValue(valueStr);
                                    //test
                                    if (item.Value.Type == JTokenType.Integer)
                                    {
                                        output[item.Name] = item.Value.Value <Int64>();
                                    }
                                    else if (item.Value.Type == JTokenType.Float)
                                    {
                                        output[item.Name] = item.Value.Value <Double>();
                                    }
                                    else
                                    {
                                        output[item.Name] = this.GetValue(valueStr);
                                    }
                                    //
                                }
                            }
                            catch
                            {
                                output[item.Name] = this.GetValue(valueStr);
                            }
                        }
                    }
                }
            }
            catch
            {
                output.Add("__built__io__static__value__", this.GetValue(container.ToString()));
            }

            return(output);
        }
        private void SetOutput(string json)
        {
            try
            {
                this._Json   = json;
                this._Output = default(object);
                //bool isOtherTypeOfJson = true;
                //Newtonsoft.Json.Linq.JObject data = Newtonsoft.Json.Linq.JObject.Parse(json.Replace("\r\n", ""));
                JObject data = JsonConvert.DeserializeObject <JObject>(json.Replace("\r\n", ""), ContentstackConvert.JsonSerializerSettings);
                //var tempData = data["result"];
                JContainer dataContainer = ((JContainer)data);

                /*
                 * if (tempData != null)
                 * {
                 *  //string tempJson = tempData.ToString().Replace("\r\n", string.Empty).Replace("\n", string.Empty);
                 *  //data = JsonConvert.DeserializeObject<JObject>(tempJson, BuiltConvert.JsonSerializerSettings);
                 *
                 *  dataContainer = (JContainer)tempData;
                 * }
                 */

                Dictionary <string, object> output = this.GetObjectDict(dataContainer);
                this._Output = output;

                if (output.ContainsKey("count"))
                {
                    this._TotalCount = ContentstackConvert.ToInt32(output["count"]);
                }

                if (output.ContainsKey("schema"))
                {
                    this._Schema = output["schema"];
                }

                if (output.ContainsKey("notice"))
                {
                    this._Notice = ContentstackConvert.ToString(output["notice"]);
                }

                if (output.ContainsKey("uid"))
                {
                    this._Uid = ContentstackConvert.ToString(output["uid"]);
                }

                if (output.ContainsKey("result"))
                {
                    this._Result = output["result"];
                }



                if (output.ContainsKey("entry"))
                {
                    this._Object = output["entry"];

                    if (this._Object != null)
                    {
                        Dictionary <string, object> temp = (Dictionary <string, object>) this._Object;

                        if (temp.ContainsKey("uid"))
                        {
                            this._Uid = ContentstackConvert.ToString(temp["uid"]);
                        }

                        if (temp.ContainsKey("_owner"))
                        {
                            //this._Owner = temp["_owner"];
                        }

                        if (temp.ContainsKey("tags"))
                        {
                            this._Tags = temp["tags"];
                        }
                    }
                }

                if (output.ContainsKey("entries"))
                {
                    if (output["entries"].GetType() == typeof(string))
                    {
                        this._Objects = this.GetValue(ContentstackConvert.ToString(output["entries"]));
                    }
                    else
                    {
                        this._Objects = output["entries"];
                        var tmp = this._Objects as object[];
                        //if (tmp != null)
                        //    this._Object = tmp.FirstOrDefault();
                    }
                }

                if (output.ContainsKey("response"))
                {
                    if (output["response"].GetType() == typeof(string))
                    {
                        var temp = (Dictionary <string, object>) this.GetValue(ContentstackConvert.ToString(output["response"]));
                        this._Objects = temp["entries"];
                    }
                    else
                    {
                        var tempResult = (Dictionary <string, object>)output["response"];
                        if (tempResult.ContainsKey("entry"))
                        {
                            this._Object = tempResult["entry"];

                            if (this._Object != null)
                            {
                                Dictionary <string, object> temp = (Dictionary <string, object>) this._Object;

                                if (temp.ContainsKey("uid"))
                                {
                                    this._Uid = ContentstackConvert.ToString(temp["uid"]);
                                }

                                if (temp.ContainsKey("_owner"))
                                {
                                    //this._Owner = temp["_owner"];
                                }

                                if (temp.ContainsKey("tags"))
                                {
                                    this._Tags = temp["tags"];
                                }
                            }
                        }
                        if (tempResult.ContainsKey("entries"))
                        {
                            if (tempResult["entries"].GetType() == typeof(string))
                            {
                                this._Objects = tempResult["entries"];
                            }
                            else
                            {
                                this._Objects = tempResult["entries"];
                                //var tmp = this._Objects as object[];
                                //if (tmp != null)
                                //    this._Object = tmp.FirstOrDefault();
                            }
                        }
                        //if (tmp != null)
                        //    this._Object = tmp.FirstOrDefault();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ContentstackError(ex);
            }
        }