Exemple #1
0
        public static bool Parse(string Text, int Index, jsObject Storage = null)
        {
            if (string.IsNullOrEmpty(Text))
            {
                return(false);
            }

            if (Index < 0 || Index >= Text.Length)
            {
                return(false);
            }

            var X = Text[Index];

            if (X == '{')
            {
                return(_Object(Text, ref Index, Text.Length, Storage) != null);
            }
            else if (X == '[')
            {
                return(_Array(Text, ref Index, Text.Length, Storage) != null);
            }
            else
            {
                return(_Object("{" + Text + "}", ref Index, Text.Length + 2, Storage) != null);
            }
        }
Exemple #2
0
        public void Set <T>(string Key, T Value)
        {
            if (StringExtensions.Contains(Key, '.'))
            {
                jsObject Current = this;
                int      Open = 0, Close = Key.Find('.');

                while (Current != null && Open != -1 && Close != -1)
                {
                    var Sub = Key.Substring(Open, Close - Open);

                    if (Close == Key.Length)
                    {
                        if (Value == null)
                        {
                            Current.Remove(Sub);
                        }
                        else
                        {
                            Current.AssignValue <T>(Sub, Value);
                        }
                        return;
                    }

                    var Next = Current.SearchForItem <jsObject>(Sub);

                    if (Next == null)
                    {
                        Next = new jsObject();
                        Current.AssignValue(Sub, Next);
                    }
                    Current = Next;

                    Open  = Close + 1;
                    Close = Key.Find('.', Open);

                    if (Close == -1)
                    {
                        Close = Key.Length;
                    }
                }
            }

            if (Value == null)
            {
                this.Remove(Key);
            }
            else
            {
                this.AssignValue(Key, Value);
            }
        }
Exemple #3
0
        public static bool Extract(this jsObject This, string Template, string Data, bool IgnoreCase = false)
        {
            if (string.IsNullOrEmpty(Template) || string.IsNullOrEmpty(Data))
            {
                return(false);
            }

            if (Data.Match(Template, IgnoreCase, This) == null)
            {
                return(false);
            }
            return(true);
        }
Exemple #4
0
        private static jsObject _Array(string Text, ref int Index, int LastIndex, jsObject Storage)
        {
            int Open = Index, Close = Index;

            if (Text.FindMatchingBrackets("[", "]", ref Open, ref Close))
            {
                do
                {
                    if (Text[Open] == ',')
                    {
                        Open++;
                    }

                    while (Open < Close && char.IsWhiteSpace(Text[Open]))
                    {
                        Open++;
                    }

                    if (Open == Close)
                    {
                        break;
                    }


                    var X    = Text[Open];
                    var Name = Storage.Count.ToString();

                    if (X == '"' || X == '\'')
                    {
                        Storage.Set(Name, _String(Text, ref Open, Close));
                    }
                    else if (X == '{')
                    {
                        Storage.Set(Name, _Object(Text, ref Open, Close, jsObject.NewObject()));
                    }
                    else if (X == '[')
                    {
                        Storage.Set(Name, _Array(Text, ref Open, Close, jsObject.NewArray()));
                    }
                    else
                    {
                        Storage.Set(Name, _Raw(Text, ref Open, Close));
                    }
                }while (Open < Close);

                Index = Close + 1;
                return(Storage);
            }
            return(null);
        }
Exemple #5
0
        public static jsObject FromUrl(string Url, string Data = "")
        {
            System.Net.WebClient Client = new System.Net.WebClient();
            if (string.IsNullOrEmpty(Data))
            {
                Data = Client.DownloadString(Url);
            }
            else
            {
                Data = Client.UploadString(Url, Data);
            }

            jsObject Object = new jsObject();

            if (Object.Parse(Data))
            {
                return(Object);
            }

            return(default(jsObject));
        }
Exemple #6
0
        public void Set <T>(string[] Keys, T Value)
        {
            jsObject Current = this;

            for (int i = 0; i < Keys.Length; i++)
            {
                var Sub = Keys[i];

                if ((Keys.Length - i) == 1)
                {
                    if (Sub.Contains('.'))
                    {
                        Current.Set(Sub, Value);
                    }
                    else
                    {
                        Current.AssignValue <T>(Sub, Value);
                    }
                    break;
                }

                var Next = Current.Get <jsObject>(Sub);

                if (Next == null)
                {
                    Next = new jsObject();

                    if (Sub.Contains('.'))
                    {
                        Current.Set(Sub, Next);
                    }
                    else
                    {
                        Current.AssignValue <jsObject>(Sub, Next);
                    }
                }

                Current = Next;
            }
        }
Exemple #7
0
        public T Get <T>(string[] Keys)
        {
            jsObject Current = this;

            for (int i = 0; Current != null && i < Keys.Length; i++)
            {
                var Sub = Keys[i];

                if (StringExtensions.Contains(Sub, '.'))
                {
                    Current = Current.Get <jsObject>(Sub);
                    continue;
                }

                if ((Keys.Length - i) == 1)
                {
                    return(Current.SearchForItem <T>(Sub));
                }

                Current = Current.SearchForItem <jsObject>(Sub);
            }
            return(default(T));
        }
Exemple #8
0
        public T Get <T>(string Key)
        {
            if (StringExtensions.Contains(Key, '.'))
            {
                int      Open = 0, Close = Key.Find('.');
                jsObject Current = this;

                while (Current != null && Open != -1 && Close != -1)
                {
                    var Sub = Key.Substring(Open, Close - Open);

                    if (Close == Key.Length)
                    {
                        return(Current.SearchForItem <T>(Sub));
                    }

                    var Next = Current.SearchForItem <jsObject>(Sub);

                    if (Next == null)
                    {
                        return(default(T));
                    }
                    Current = Next;


                    Open  = Close + 1;
                    Close = Key.Find('.', Open);

                    if (Close == -1)
                    {
                        Close = Key.Length;
                    }
                }
            }

            return(SearchForItem <T>(Key));
        }
Exemple #9
0
        private static jsObject _Object(string Text, ref int Index, int LastIndex, jsObject Storage)
        {
            int Open = Index, Close = Index;

            if (Text.FindMatchingBrackets("{", "}", ref Open, ref Close))
            {
                do
                {
                    if (Text[Open] == ',')
                    {
                        Open++;
                    }

                    while (Open < Close && char.IsWhiteSpace(Text[Open]))
                    {
                        Open++;
                    }

                    if (Open == Close)
                    {
                        break;
                    }

                    var Obj = _NamedValue(Text, ref Open, Close, Storage);

                    if (Obj == null)
                    {
                        return(null);
                    }
                }while (Open < Close);

                Index = Close + 1;
                return(Storage);
            }
            return(null);
        }
Exemple #10
0
        public static string Template(this jsObject This, string Template)
        {
            List <string> List = new List <string>();

            for (int i = 0, Last = 0; i < Template.Length; i++)
            {
                string Next = Template.FirstPossible(i, "{", "[", "]", "\\");

                switch (Next)
                {
                case "{":
                    var Open  = Template.Find("{", i);
                    var Close = Template.Find("}", Open);
                    var Start = Close + 1;

                    if (i != Open)
                    {
                        List.Add(Template.SubString(i, Open - i));
                    }

                    var Mod = Template.Find(':', Open, Close);

                    if (Mod == -1)
                    {
                        Mod = Close;
                    }

                    var Name = Template.SubString(Open + 1, Mod - Open - 1);
                    var Obj  = This.Get <object>(Name);

                    Open = Template.Find("{/" + Name);

                    if (Open != -1 && Obj is jsObject)
                    {
                        Close = Template.Find("}", Open);

                        var SubTemplate = Template.SubString(Start, Open - Start);
                        var jsObj       = (Obj as jsObject);

                        foreach (jsObject Sub in jsObj.Values)
                        {
                            List.Add(Sub.Template(SubTemplate));
                        }
                    }
                    else
                    {
                        if (Obj != null)
                        {
                            List.Add(Obj.ToString());
                        }
                    }

                    i    = Close;
                    Last = i;
                    break;

                case "[":
                case "]": {
                    var Index = Template.Find(Next, i);
                    List.Add(Template.SubString(i, Index - i));
                    i = Index + 1;
                    break;
                }

                case "\\": {
                    var Index = Template.Find(Next, i);
                    List.Add(Template.SubString(i, Index - i));
                    Close = Template.FirstPossibleIndex(Index, "{", "[", "]", "\\");

                    if (Close == -1)
                    {
                        Close = Template.Length;
                    }

                    List.Add(Template.SubString(Index, Close - Index));
                    i = Close;
                    break;
                }

                default:
                    List.Add(Template.SubString(i, Template.Length - i));
                    i = Template.Length;
                    break;
                }
            }

            return(string.Join("", List));
        }
Exemple #11
0
        private static jsObject _NamedValue(string Text, ref int Index, int LastIndex, jsObject Storage)
        {
            var Name = "";
            var X    = Text[Index];

            if (X == '"')
            {
                Name = Text.FindMatchingBrackets("\"", "\"", Index);

                Index += Name.Length + 2;
            }
            else if (X == '\'')
            {
                Name = Text.FindMatchingBrackets("'", "'", Index);

                Index += Name.Length + 2;
            }
            else
            {
                Name = Text.Substring("", ":", Index, false).Trim();
            }

            Index = Text.IndexOf(':', Index);

            if (Index != -1)
            {
                Index++;

                while (char.IsWhiteSpace(Text[Index]))
                {
                    Index++;
                }

                X = Text[Index];

                if (X == '"' || X == '\'')
                {
                    Storage.Set(Name, _String(Text, ref Index, LastIndex));
                }
                else if (X == '{')
                {
                    Storage.Set(Name, _Object(Text, ref Index, LastIndex, jsObject.NewObject()));
                }
                else if (X == '[')
                {
                    Storage.Set(Name, _Array(Text, ref Index, LastIndex, jsObject.NewArray()));
                }
                else
                {
                    Storage.Set(Name, _Raw(Text, ref Index, LastIndex));
                }

                return(Storage);
            }

            return(null);
        }
Exemple #12
0
 public void CopyTo(jsObject Object)
 {
     ForEach((K, V) => {
         Object[K] = V;
     });
 }
Exemple #13
0
        public static string Stringify(jsObject This, bool HumanFormat, int Reserved = 1)
        {
            StringBuilder Output = new StringBuilder();

            Output.Append(This.IsArray ? '[' : '{');

            if (HumanFormat)
            {
                Output.AppendLine();
                Output.Append('\t', Reserved);
            }

            int Index = 0;

            foreach (var Pair in This)
            {
                var K = Pair.Key;
                var V = Pair.Value;

                if (!This.IsArray)
                {
                    Output.AppendFormat("'{0}':", K);
                }

                var Obj = V as jsObject;

                if (Obj != null)
                {
                    Output.Append(
                        Stringify(Obj, HumanFormat, Reserved + 1)
                        );
                }
                else if (V is string)
                {
                    Output.AppendFormat("'{0}'", V);
                }
                else
                {
                    Output.Append(V);
                }

                if (This.Count > 1 && (This.Count - Index) != 1)
                {
                    Output.Append(',');
                    Index++;
                }
                else
                {
                    Reserved--;
                }

                if (HumanFormat)
                {
                    Output.Append(Environment.NewLine);
                    Output.Append('\t', Reserved);
                }
            }
            ;

            Output.Append(This.IsArray ? ']' : '}');

            return(Output.ToString());
        }