Example #1
0
 public void Save(string Path, JSONObject JSON, Format Format)
 {
     Serializer s = new Serializer();
     string text = s.GetText(JSON);
     text = s.FormatText(text, Format);
     File.WriteAllText(Path, text);
 }
Example #2
0
        public JSONObject Open(string Path)
        {
            this.FilePath = Path;
            if (!File.Exists(this.FilePath))
            {
                throw new Exception("Cannot find file.");
            }
            TextReader tr = File.OpenText(Path);
            string contents = tr.ReadToEnd();
            tr.Close();

            contents = contents.RemoveIfFirst('[').RemoveIfLast(']');
            this.RootObject = new JSONObject();
            this.RootObject.Deserialize(contents);

            return this.RootObject;
        }
Example #3
0
 public JSONObject AddNode()
 {
     JSONObject child = new JSONObject();
     child.Parent = this;
     child.ValueType = JSONType.Object;
     this.ChildObjects.Add(child);
     if (NodeMenu != null) { child.NodeMenu = NodeMenu; child.TreeNode.ContextMenuStrip = NodeMenu; }
     child.SetImage();
     this.TreeNode.Nodes.Add(child.TreeNode);
     this.TreeNode.Expand();
     this.SetImage();
     return child;
 }
Example #4
0
        public void AddChildObject(string Raw)
        {
            JSONObject child = new JSONObject();
            child.Parent = this;
            child.ValueType = JSONType.Object;
            if (NodeMenu != null) { child.NodeMenu = NodeMenu; }
            this.ChildObjects.Add(child);

            child.Deserialize(Raw);
        }
Example #5
0
        public void AddChildAttribute(JSONObject child, string Raw)
        {
            child.Parent = this;
            child.Raw = Raw;

            int split = Raw.IndexOf(':');
            if (split == -1) { return; }
            string[] items = new string[2];
            items[0] = Raw.Substring(0, split);
            items[1] = Raw.Substring(split + 1);
            child.Name = items[0].Replace("\"", "").Trim();

            if (items[1].Trim().StartsWith("\""))
            {
                child.ValueType = JSONType.String;
                string v = items[1].Replace("\\\"", "|||||");
                v = v.Replace("\"", "");
                v = v.Replace("|||||", "\\\"");
                child.Value = v.Trim();
            }
            else if (items[1].Trim().StartsWith("'"))
            {
                child.ValueType = JSONType.String;
                child.Value = items[1].Replace("'", "").Trim();
            }
            else if (items[1].Trim().CanConvert(typeof(System.Int32)))
            {
                child.ValueType = JSONType.Number;
                child.Value = Convert.ToInt32(items[1]);
            }
            else if (items[1].Trim().CanConvert(typeof(bool)))
            {
                child.ValueType = JSONType.Boolean;
                child.Value = Convert.ToBoolean(items[1].Trim());
            }
            else if (items[1].FirstVisibleCharacter() == '[' && items[1].LastVisibleCharacter() == ']')
            {
                items[1] = items[1].RemoveIfFirst('[').RemoveIfLast(']');
                char next = items[1].FirstVisibleCharacter();
                if (next == '{') //We are dealing with Objects here.
                {
                    child.ValueType = JSONType.ObjectArray;
                    child.Deserialize(items[1]);
                }
                else  // Raw text
                {
                    child.ValueType = JSONType.Raw;
                    child.Value = items[1];
                }
            }
            else if (items[1].FirstVisibleCharacter() == '{')
            {
                child.ValueType = JSONType.Object;
                items[1] = items[1].RemoveIfFirst('{').RemoveIfLast('}');
                List<JSONObject> props = SplitObjects(items[1]);
                foreach (JSONObject prop in props)
                {
                    child.AddChildAttribute(prop, prop.Raw);
                }
            }
            else
            {
                child.ValueType = JSONType.Raw;
                child.Value = items[1].Trim();
            }

            this.ChildObjects.Add(child);
        }
Example #6
0
 public void AddChildAttribute(string Raw)
 {
     JSONObject child = new JSONObject();
     AddChildAttribute(child, Raw);
 }
Example #7
0
        private void LoadGrid(JSONObject JSON)
        {
            try
            {
                PropertyGrid.Rows.Clear();
                foreach (JSONObject child in JSON.ChildObjects)
                {
                    PropertyGrid.Rows.Add(child.Name, child.Value, child.ValueType.ToString());
                }

                PropertyGrid.Rows[PropertyGrid.Rows.Count - 1].Cells[0].Selected = true;
                PropertyGrid.Rows[PropertyGrid.Rows.Count - 1].Cells[2].Value = "String";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            PropertyGrid.Focus();
        }
Example #8
0
 public JSONObject GetLastObject(JSONObject Input)
 {
     if (!Input.ContainsObjects)
     {
         return Input;
     }
     else
     {
         return GetLastObject(Input.ChildJSONObjects[Input.ChildJSONObjects.Count - 1]);
     }
 }
Example #9
0
        private void PropertyGrid_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = PropertyGrid.HitTest(e.X, e.Y);
                PropertyGrid.ClearSelection();
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    PropertyGrid.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Selected = true;
                    GridMenu.Show(PropertyGrid, new Point(e.X, e.Y));
                }
            }
            else if (e.Clicks == 2)
            {
                DataGridView.HitTestInfo hti = PropertyGrid.HitTest(e.X, e.Y);
                PropertyGrid.ClearSelection();
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    PropertyGrid.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Selected = true;
                    DataGridViewCell cell = PropertyGrid.SelectedCells[0];
                    if (cell == null) { return; }
                    DataGridViewRow row = PropertyGrid.Rows[cell.RowIndex];
                    string cellvalue = row.Cells[1].Value == null ? "" : row.Cells[1].Value.ToString();
                    string typevalue = row.Cells[2].Value == null ? "" : row.Cells[2].Value.ToString();

                    if (typevalue.ToUpper() == "OBJECT" ||
                        typevalue.ToUpper() == "OBJECTARRAY")
                    {
                        string id = row.Cells[0].Value.ToString();
                        if (string.IsNullOrEmpty(id)) { return; }
                        CurrentObject = CurrentObject.GetChild(id);
                        Tree.SelectedNode = CurrentObject.TreeNode;
                        return;
                    }

                    EditBox edit = new EditBox();
                    edit.StartForm = this;
                    edit.EditText = cellvalue;

                    edit.ShowDialog(this);
                }
            }
        }
Example #10
0
        private void PropertyGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = PropertyGrid.Rows[e.RowIndex];
            if (CurrentObject == null) { CurrentObject = rootObject; }
            string id = row.Cells[0].Value == null ? "" : row.Cells[0].Value.ToString();
            object value = row.Cells[1].Value;
            row.Cells[2].Value = row.Cells[2].Value == null ? "String" : row.Cells[2].Value.ToString();
            string type = row.Cells[2].Value.ToString();

            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(type))
            {
                CurrentObject.AddNode(id, value, type);
                if (id.ToUpper() == "ID" && value != null && type == "String")
                {
                    CurrentObject.ChangeName(value.ToString());
                }
            }
            SetHasChanged();
        }
Example #11
0
 private void NodeMenuDeleteNode_Click(object sender, EventArgs e)
 {
     if (CurrentObject == rootObject)
     {
         MessageBox.Show("You cannot delete the Root node of the document.");
         return;
     }
     else
     {
         if (MessageBox.Show("Are you sure?  This will remove this node and ALL nodes underneath it.", "Are you sure?", MessageBoxButtons.OKCancel) == DialogResult.OK)
         {
             CurrentObject = CurrentObject.Delete();
             UpdateTabs();
             hasChanged = true;
         }
     }
 }
Example #12
0
 private void NodeMenuAddNode_Click(object sender, EventArgs e)
 {
     CurrentObject = CurrentObject.AddNode();
     CurrentObject.TreeNode.BeginEdit();
 }
Example #13
0
        private void MenuFileNew_Click(object sender, EventArgs e)
        {
            if (!Confirm()) { return; }
            this.rootObject = new JSONObject();
            this.rootObject.BuildTree(Tree);

            UpdateTabs();
            this.Text = "JSONTools - [New Document]";
            hasChanged = false;
            settings.LastFilePath = "";
        }
Example #14
0
        private void Main_Load(object sender, EventArgs e)
        {
            #region Load Settings
            settingsPath = Application.StartupPath + "\\" + "usersettings.xml";
            if (File.Exists(settingsPath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(UserSettings));
                FileStream fs = new FileStream(settingsPath, FileMode.Open);
                settings = (UserSettings)serializer.Deserialize(fs);
                fs.Close();
            }
            else
            {
                settings = new UserSettings();
            }
            #endregion
            #region Set Settings
            settingUp = true;
            if (settings.MainLocation != null) { this.Location = settings.MainLocation; }
            this.WindowState = settings.MainState;
            this.Height = this.settings.MainHeight;
            this.Width = this.settings.MainWidth;

            if (this.Height < 50) { this.Height = 500; }
            if (this.Width < 200) { this.Width = 700; }

            this.splitContainer1.SplitterDistance = settings.SplitterWidth;
            if (this.splitContainer1.SplitterDistance < 50) { this.splitContainer1.SplitterDistance = 200; }

            settingUp = false;

            CodeBox.WordWrap = settings.WordWrap;
            MenuPrefWordWrap.Checked = settings.WordWrap;
            MenuPrefBackup.Checked = settings.SaveBackup;

            MenuFormatFull.Checked = settings.TextFormat == Format.Full;
            MenuFormatPartial.Checked = settings.TextFormat == Format.Partial;
            MenuFormatMin.Checked = settings.TextFormat == Format.Minimized;

            if (args != null && args.Length > 0 && !string.IsNullOrEmpty(args[0]) && File.Exists(args[0]))
            {
                settings.LastFilePath = args[0];
                FileDialog.FileName = settings.LastFilePath;
                LoadFile(settings.LastFilePath);
            }
            else if (settings.OpenLastFileOnStartup && !string.IsNullOrEmpty(settings.LastFilePath) && File.Exists(settings.LastFilePath))
            {
                FileDialog.FileName = settings.LastFilePath;
                LoadFile(settings.LastFilePath);
            }
            else
            {
                this.rootObject = new JSONObject();
                this.rootObject.BuildTree(Tree);

                UpdateTabs();
                this.Text = "JSONTools - [New Document]";
                hasChanged = false;
                settings.LastFilePath = "";
            }
            #endregion
        }
Example #15
0
 private void Main_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.Control && e.KeyCode == Keys.N)
     {
         MenuFileNew_Click(sender, null);
         e.Handled = true;
         return;
     }
     else if (e.Shift && e.KeyCode == Keys.Insert)
     {
         if (CurrentObject == rootObject)
         {
             MessageBox.Show("You cannot add a peer node to the root node.");
             return;
         }
         CurrentObject = CurrentObject.Parent.AddNode();
         CurrentObject.TreeNode.BeginEdit();
         e.Handled = true;
         return;
     }
     else if (e.Shift && e.KeyCode == Keys.Delete)
     {
         NodeMenuDeleteNode_Click(sender, null);
         e.Handled = true;
         return;
     }
     else if (e.KeyCode == Keys.Insert)
     {
         CurrentObject = CurrentObject.AddNode();
         CurrentObject.TreeNode.BeginEdit();
         e.Handled = true;
         return;
     }
     else if (e.Control && e.KeyCode == Keys.O)
     {
         MenuFileOpen_Click(sender, null);
         e.Handled = true;
         return;
     }
     else if (e.Control && e.Shift && e.KeyCode == Keys.S)
     {
         MenuFileSaveAs_Click(sender, null);
         e.Handled = true;
         return;
     }
     else if (e.Control && e.KeyCode == Keys.S)
     {
         MenuFileSave_Click(sender, null);
         e.Handled = true;
         return;
     }
     else if (e.KeyCode == Keys.F5)
     {
         if (!Confirm()) { return; }
         LoadFile(settings.LastFilePath);
         e.Handled = true;
         return;
     }
     else if (e.Shift && e.KeyCode == Keys.Up)
     {
         CurrentObject = ((JSONObject)Tree.SelectedNode.Tag).GetPreviousObject();
         e.Handled = true;
         return;
     }
     else if (e.Shift && e.KeyCode == Keys.Down)
     {
         JSONObject next = ((JSONObject)Tree.SelectedNode.Tag).GetNextObject();
         CurrentObject = next != null ? next : CurrentObject;
         e.Handled = true;
         return;
     }
 }
Example #16
0
 public void AddNode(string Name, object Value, string Type)
 {
     JSONObject child = this.GetChild(Name);
     if (child == null)
     {
         child = new JSONObject();
         child.Parent = this;
         this.ChildObjects.Add(child);
     }
     child.Name = Name;
     child.Value = Value;
     child.ValueType = (JSONType)Enum.Parse(typeof(JSONType), Type);
     if (child.ValueType == JSONType.Object ||
         child.ValueType == JSONType.ObjectArray)
     {
         if (NodeMenu != null) { child.TreeNode.ContextMenuStrip = NodeMenu; }
         child.SetImage();
         this.TreeNode.Nodes.Add(child.TreeNode);
         this.TreeNode.Expand();
         this.SetImage();
     }
 }
Example #17
0
        public void Deserialize(string Text)
        {
            this.Raw = Text;

            //this.Raw = this.Raw.RemoveIfFirst('{').RemoveIfLast('}');

            bool insideD = false;
            bool insideS = false;
            bool isAttribute = false;
            int bDepth = 0;
            int brDepth = 0;
            string buffer = "";

            for (int x = 0; x < this.Raw.Length; x++)
            {
                switch (this.Raw[x])
                {
                    case '"':
                        if (!insideS) { insideD = !insideD; }
                        buffer += '"';
                        break;
                    case '\'':
                        if (!insideD) { insideS = !insideS; }
                        buffer += '\'';
                        break;
                    case '[':
                        brDepth++;
                        buffer += '[';
                        break;
                    case ']':
                        brDepth--;
                        buffer += ']';
                        break;
                    case '{':
                        bDepth++;
                        buffer += '{';
                        break;
                    case '}':
                        bDepth--;
                        buffer += '}';
                        break;
                    case ':':
                        if (!insideD && !insideS && bDepth == 0 && brDepth == 0)
                        {
                            isAttribute = true;
                        }
                        buffer += ':';
                        break;
                    case ',':
                        if (!insideD && !insideS && bDepth == 0 && brDepth == 0)
                        {
                            if (isAttribute)
                            {
                                this.AddChildAttribute(buffer);
                            }
                            else
                            {
                                buffer = buffer.RemoveIfFirst('{').RemoveIfLast('}');
                                this.AddChildObject(buffer);
                            }
                            buffer = "";
                            isAttribute = false;
                        }
                        else
                        {
                            buffer += ',';
                        }
                        break;
                    case '\r':
                    case '\n':
                    case '\t':
                        if (insideD || insideS)
                        {
                            buffer += this.Raw[x];
                        }
                        break;
                    default:
                        buffer += this.Raw[x];
                        break;
                }
            }

            if (!string.IsNullOrEmpty(buffer))
            {
                if (isAttribute)
                {
                    this.AddChildAttribute(buffer);
                }
                else
                {
                    buffer = buffer.RemoveIfFirst('{').RemoveIfLast('}');
                    this.AddChildObject(buffer);
                }
            }

            if (this.GetChild("id") != null)
            {
                this.Name = this.GetChild("id").Value.ToString();
            }

            #region May not need
            int start = this.Raw.IndexOf('{') + 1;
            int end = -1;
            int bcount = 1;

            for (int x = start; x < this.Raw.Length; x++)
            {
                if (this.Raw[x] == '{') { bcount++; }
                if (this.Raw[x] == '}')
                {
                    bcount--;
                    if (bcount <= 1)
                    {
                        end = x;
                        break;
                    }
                }

                if (start > -1 && end > -1)
                {
                    JSONObject newNode = new JSONObject();
                    newNode.ValueType = JSONType.Object;
                    newNode.Parent = this;
                    newNode.Deserialize(this.Raw.Substring(start, end - start));
                }
            }
            #endregion
        }
Example #18
0
 private void Tree_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
 {
     if (string.IsNullOrEmpty(e.Label))
     {
         if (CurrentObject.IsEmpty)
         {
             CurrentObject = CurrentObject.Delete();
         }
         e.CancelEdit = true; return;
     }
     else
     {
         CurrentObject.ChangeName(e.Label);
     }
     UpdateTabs();
     PropertyGrid.Focus();
     PropertyGrid.Rows[PropertyGrid.Rows.Count - 1].Cells[0].Selected = true;
     SetHasChanged();
 }
Example #19
0
 private void Tree_MouseDown(object sender, MouseEventArgs e)
 {
     Point p = new Point(e.X, e.Y);
     TreeNode nodeUnderMouse = Tree.GetNodeAt(p);
     if (nodeUnderMouse != null)
     {
         CurrentObject = (JSONObject)nodeUnderMouse.Tag;
     }
 }
Example #20
0
 private void UpdateTabs()
 {
     if (CurrentObject == null) { CurrentObject = rootObject; }
     if (Tabs.SelectedTab == PropertyTab)
     {
         CannotEditPanel.Visible = false; //CurrentObject == rootObject;
         if (!CannotEditPanel.Visible)
         {
             LoadGrid(CurrentObject);
         }
         else
         {
             NoticeLabel.Text = "You cannot edit the root node of the data hierarchy. Add or select another node.";
         }
     }
     else if (Tabs.SelectedTab == TextTab)
     {
         trackChanges = false;
         LoadText();
         trackChanges = true;
     }
 }
Example #21
0
        public string GetText(JSONObject Input)
        {
            output = new StringBuilder();
            bool isArray = Input.ContainsOnlyObjects && Input.ChildJSONObjects.Count > 1;
            string b = isArray ? "[" : "{";
            string e = isArray ? "]" : "}";
            output.Append(b);

            foreach (JSONObject child in Input.ChildAttributes)
            {
                if (!isArray && !string.IsNullOrEmpty(child.Name))
                {
                    output.AppendFormat("\"{0}\":", child.Name);
                }

                switch (child.ValueType)
                {
                    case JSONType.String:
                        output.AppendFormat("\"{0}\"", child.Value.ToString());
                        output.Append(",");
                        break;
                    case JSONType.Number:
                    case JSONType.Raw:
                        output.AppendFormat("{0}", child.Value.ToString());
                        output.Append(",");
                        break;
                    case JSONType.Boolean:
                        output.AppendFormat("{0}", child.Value.ToString().ToLower());
                        output.Append(",");
                        break;
                    default:
                        output.AppendFormat("\"{0}\"", child.Value.ToString());
                        output.Append(",");
                        break;
                }
            }
            foreach (JSONObject child in Input.ChildJSONObjects)
            {
                if (!isArray)
                {
                    output.AppendFormat("\"{0}\":", child.Name);
                }

                Serializer s = new Serializer();
                string objecttext = s.GetText(child);
                objecttext = objecttext.TrimStart(new char[] { '\t' });
                output.AppendFormat("{0}", objecttext);
                output.Append(",");
            }

            RemoveLastComma();

            output.Append(e);
            return output.ToString();
        }
Example #22
0
        private void LoadFile(string FileName)
        {
            JSONFile file = new JSONFile(FileName);
            rootObject = file.Open();
            rootObject.NodeMenu = this.NodeMenu;
            rootObject.BuildTree(Tree);
            CurrentObject = rootObject;
            UpdateTabs();

            settings.LastFilePath = FileDialog.FileName;
            this.Text = "JSONTools - " + settings.LastFilePath;
            hasChanged = false;
        }