diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 12989be..cf13713 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -28,13 +28,175 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); + this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); + this.classNameTextBox = new System.Windows.Forms.TextBox(); + this.convertBtn = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.button1 = new System.Windows.Forms.Button(); + this.splitContainer1 = new System.Windows.Forms.SplitContainer(); + this.jsonTextBox = new System.Windows.Forms.TextBox(); + this.dartTextBox = new System.Windows.Forms.TextBox(); + this.tableLayoutPanel1.SuspendLayout(); + this.tableLayoutPanel2.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); + this.splitContainer1.Panel1.SuspendLayout(); + this.splitContainer1.Panel2.SuspendLayout(); + this.splitContainer1.SuspendLayout(); + this.SuspendLayout(); + // + // tableLayoutPanel1 + // + this.tableLayoutPanel1.ColumnCount = 1; + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 0, 0); + this.tableLayoutPanel1.Controls.Add(this.splitContainer1, 0, 1); + this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; + this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); + this.tableLayoutPanel1.Name = "tableLayoutPanel1"; + this.tableLayoutPanel1.RowCount = 2; + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanel1.Size = new System.Drawing.Size(2037, 1325); + this.tableLayoutPanel1.TabIndex = 0; + // + // tableLayoutPanel2 + // + this.tableLayoutPanel2.ColumnCount = 4; + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 44.31138F)); + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 55.68862F)); + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 365F)); + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 664F)); + this.tableLayoutPanel2.Controls.Add(this.classNameTextBox, 3, 0); + this.tableLayoutPanel2.Controls.Add(this.convertBtn, 0, 0); + this.tableLayoutPanel2.Controls.Add(this.label1, 2, 0); + this.tableLayoutPanel2.Controls.Add(this.button1, 1, 0); + this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill; + this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 3); + this.tableLayoutPanel2.Name = "tableLayoutPanel2"; + this.tableLayoutPanel2.RowCount = 1; + this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanel2.Size = new System.Drawing.Size(2031, 73); + this.tableLayoutPanel2.TabIndex = 1; + this.tableLayoutPanel2.Paint += new System.Windows.Forms.PaintEventHandler(this.tableLayoutPanel2_Paint); + // + // classNameTextBox + // + this.classNameTextBox.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.classNameTextBox.Location = new System.Drawing.Point(1370, 3); + this.classNameTextBox.Name = "classNameTextBox"; + this.classNameTextBox.Size = new System.Drawing.Size(652, 50); + this.classNameTextBox.TabIndex = 1; + // + // convertBtn + // + this.convertBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); + this.convertBtn.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.convertBtn.Location = new System.Drawing.Point(3, 3); + this.convertBtn.Name = "convertBtn"; + this.convertBtn.Size = new System.Drawing.Size(187, 56); + this.convertBtn.TabIndex = 0; + this.convertBtn.Text = "Convert"; + this.convertBtn.UseVisualStyleBackColor = false; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.label1.Location = new System.Drawing.Point(1005, 0); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(202, 45); + this.label1.TabIndex = 2; + this.label1.Text = "Class Name: "; + // + // button1 + // + this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); + this.button1.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.button1.Location = new System.Drawing.Point(447, 3); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(361, 56); + this.button1.TabIndex = 1; + this.button1.Text = "Copy to Clipboard"; + this.button1.UseVisualStyleBackColor = false; + // + // splitContainer1 + // + this.splitContainer1.Cursor = System.Windows.Forms.Cursors.VSplit; + this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; + this.splitContainer1.Location = new System.Drawing.Point(3, 82); + this.splitContainer1.Name = "splitContainer1"; + // + // splitContainer1.Panel1 + // + this.splitContainer1.Panel1.Controls.Add(this.jsonTextBox); + // + // splitContainer1.Panel2 + // + this.splitContainer1.Panel2.Controls.Add(this.dartTextBox); + this.splitContainer1.Size = new System.Drawing.Size(2031, 1240); + this.splitContainer1.SplitterDistance = 1001; + this.splitContainer1.TabIndex = 0; + // + // jsonTextBox + // + this.jsonTextBox.BackColor = System.Drawing.Color.White; + this.jsonTextBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.jsonTextBox.Font = new System.Drawing.Font("Segoe UI", 10.875F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.jsonTextBox.Location = new System.Drawing.Point(0, 0); + this.jsonTextBox.Multiline = true; + this.jsonTextBox.Name = "jsonTextBox"; + this.jsonTextBox.PlaceholderText = "Paste json here"; + this.jsonTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both; + this.jsonTextBox.Size = new System.Drawing.Size(1001, 1240); + this.jsonTextBox.TabIndex = 0; + // + // dartTextBox + // + this.dartTextBox.BackColor = System.Drawing.Color.White; + this.dartTextBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.dartTextBox.Font = new System.Drawing.Font("Segoe UI", 10.875F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.dartTextBox.Location = new System.Drawing.Point(0, 0); + this.dartTextBox.Multiline = true; + this.dartTextBox.Name = "dartTextBox"; + this.dartTextBox.ReadOnly = true; + this.dartTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both; + this.dartTextBox.Size = new System.Drawing.Size(1026, 1240); + this.dartTextBox.TabIndex = 1; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(13F, 32F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + this.ClientSize = new System.Drawing.Size(2037, 1325); + this.Controls.Add(this.tableLayoutPanel1); + this.Name = "Form1"; + this.Text = "Json2Dart"; + this.Load += new System.EventHandler(this.Form1_Load); + this.tableLayoutPanel1.ResumeLayout(false); + this.tableLayoutPanel2.ResumeLayout(false); + this.tableLayoutPanel2.PerformLayout(); + this.splitContainer1.Panel1.ResumeLayout(false); + this.splitContainer1.Panel1.PerformLayout(); + this.splitContainer1.Panel2.ResumeLayout(false); + this.splitContainer1.Panel2.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); + this.splitContainer1.ResumeLayout(false); + this.ResumeLayout(false); + } #endregion + + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; + private System.Windows.Forms.SplitContainer splitContainer1; + private System.Windows.Forms.TextBox jsonTextBox; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; + private System.Windows.Forms.TextBox dartTextBox; + private System.Windows.Forms.Button convertBtn; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox classNameTextBox; } } diff --git a/Form1.cs b/Form1.cs index 551f2d5..7f10a18 100644 --- a/Form1.cs +++ b/Form1.cs @@ -17,5 +17,14 @@ public Form1() InitializeComponent(); } + private void Form1_Load(object sender, EventArgs e) + { + + } + + private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e) + { + + } } } diff --git a/Form1.resx b/Form1.resx index 1af7de1..f298a7b 100644 --- a/Form1.resx +++ b/Form1.resx @@ -1,64 +1,4 @@ - - - + diff --git a/Json/JsonArray.cs b/Json/JsonArray.cs new file mode 100644 index 0000000..ab8b0fe --- /dev/null +++ b/Json/JsonArray.cs @@ -0,0 +1,219 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Selim.Json +{ + public class JsonArray : JsonValue + { + List values = new List(); + + public JsonArray() + {} + + public JsonArray(params JsonValue[] values) + { + this.values.AddRange(values); + } + + public JsonArray add(params JsonValue[] values) + { + this.values.AddRange(values); + return this; + } + + public JsonArray add(JsonValue value) + { + this.values.Add(value); + return this; + } + + public JsonArray add(double value) + { + this.values.Add(new JsonNumber(value)); + return this; + } + + public JsonArray add(string value) + { + if(value!=null) + { + this.values.Add(new JsonString(value)); + } + else + { + this.values.Add(new JsonNull()); + } + return this; + } + + public JsonArray add(bool value) + { + this.values.Add(new JsonBoolean( value)); + return this; + } + + public JsonArray addAll(double[] arr) + { + foreach (double item in arr) + { + add(item); + } + return this; + } + + public JsonArray addAll(string[] arr) + { + foreach (string item in arr) + { + add(item); + } + return this; + } + + public JsonArray addAll(bool[] arr) + { + foreach (bool item in arr) + { + add(item); + } + return this; + } + + public int Length { get { return values.Count; } } + + public JsonValue[] getValues() + { + return values.ToArray(); + } + + public JsonValue getValue(int index) + { + return values.ElementAt(index); + } + + public string getString(int index) + { + JsonString tmp = (JsonString)getValue(index); + return tmp.value; + } + + public double getDouble(int index) + { + JsonNumber tmp = (JsonNumber)getValue(index); + return tmp.value; + } + + public int getInt(int index) + { + JsonNumber tmp = (JsonNumber)getValue(index); + return (int)tmp.value; + } + + public bool getBool(int index) + { + JsonBoolean tmp = (JsonBoolean)getValue(index); + return tmp.value; + } + + public JsonObject getObject(int index) + { + return (JsonObject)getValue(index); + } + + public JsonArray getArray(int index) + { + return (JsonArray)getValue(index); + } + + /// + /// gets a double[] of all elements, all elements must be of type JsonNumber + /// + public double[] getAllDoubles() + { + double[] tmp= new double[this.Length]; + for (int i = 0; i < Length; i++) + { + tmp[i] = getDouble(i); + } + return tmp; + } + + /// + /// gets an int[] of all elements, all elements must be of type JsonNumber + /// + public int[] getAllInts() + { + int[] tmp = new int[this.Length]; + for (int i = 0; i < Length; i++) + { + tmp[i] = getInt(i); + } + return tmp; + } + + /// + /// gets a bool[] of all elements, all elements must be of type JsonBoolean + /// + public bool[] getAllBools() + { + bool[] tmp = new bool[this.Length]; + for (int i = 0; i < Length; i++) + { + tmp[i] = getBool(i); + } + return tmp; + } + + /// + /// gets a string[] of all elements, all elements must be of type JsonString + /// + public string[] getAllStrings() + { + string[] tmp = new string[this.Length]; + for (int i = 0; i < Length; i++) + { + tmp[i] = getString(i); + } + return tmp; + } + + + public override void toString(StringBuilder sb, int? indents) + { + + if (indents != null) + { + //appendMany(sb, indentation, (int)indents); + sb.Append("["); sb.Append("\r\n"); + for (int i = 0; i < this.values.Count; i++) + { + appendMany(sb, indentation, (int)indents + 1); + values[i].toString(sb, indents+1); + if (i < values.Count - 1) + { + sb.AppendLine(","); + } + else + { + sb.AppendLine(); + } + } + appendMany(sb, indentation, (int)indents); + sb.Append("]"); + } + else + { + sb.Append("["); + foreach (JsonValue item in this.values) + { + item.toString(sb); sb.Append(","); + } + sb.Remove(sb.Length - 1, 1);//remove the last comma + sb.Append("]"); + } + + } + } +} diff --git a/Json/JsonBoolean.cs b/Json/JsonBoolean.cs new file mode 100644 index 0000000..56e3ccf --- /dev/null +++ b/Json/JsonBoolean.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Selim.Json +{ + class JsonBoolean : JsonValue + { + public bool value; + public JsonBoolean(bool value) + { + this.value = value; + } + + + + public override void toString(StringBuilder sb, int? indents) + { + sb.Append(value ? "true" : "false"); + } + } +} diff --git a/Json/JsonNull.cs b/Json/JsonNull.cs new file mode 100644 index 0000000..c9cb54a --- /dev/null +++ b/Json/JsonNull.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Selim.Json +{ + class JsonNull : JsonValue + { + + + public override void toString(StringBuilder sb, int? indents) + { + sb.Append("null"); + } + } +} diff --git a/Json/JsonNumber.cs b/Json/JsonNumber.cs new file mode 100644 index 0000000..7355567 --- /dev/null +++ b/Json/JsonNumber.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Selim.Json +{ + class JsonNumber : JsonValue + { + public double value; + public JsonNumber(double value) + { + this.value = value; + } + + + public override void toString(StringBuilder sb, int? indents) + { + sb.Append(value.ToString()); + } + } +} diff --git a/Json/JsonObject.cs b/Json/JsonObject.cs new file mode 100644 index 0000000..d5372d4 --- /dev/null +++ b/Json/JsonObject.cs @@ -0,0 +1,229 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Selim.Json +{ + public class JsonObject : JsonValue + { + + List nameValues = new List(); + + public JsonObject(){} + + public JsonObject(params NameValue[] pairs) + { + this.nameValues.AddRange(pairs); + } + + public JsonObject add(params NameValue[] pairs) + { + this.nameValues.AddRange(pairs); + return this; + } + + public JsonObject add(NameValue pair) + { + this.nameValues.Add(pair); + return this; + } + + public JsonObject add(string name, JsonValue value) + { + getOrNew(name).value = value; + return this; + } + + public JsonObject add(string name, string value) + { + var nv = getOrNew(name); + + if (value != null) + { + nv.value = new JsonString(value); + } + else + { + nv.value = new JsonNull(); + } + return this; + } + + public JsonObject add(string name, double value) + { + getOrNew(name).value =new JsonNumber(value); + return this; + } + + public JsonObject add(string name, bool value) + { + getOrNew(name).value= new JsonBoolean(value); + return this; + } + + /// + /// gets the name-value pair with this name if exists, or adds a new one and return it + /// + NameValue getOrNew(string name) + { + NameValue nv = nameValues.FirstOrDefault(n => n.name == name); + if (nv == null) + { + nv = new NameValue(name); + nameValues.Add(nv); + } + return nv; + } + + public int Length { get { return nameValues.Count; } } + + public NameValue[] getNameVlaues() + { + return nameValues.ToArray(); + } + + public JsonValue getValue(int index) + { + return nameValues.ElementAt(index).value; + } + + public JsonValue getValue(string name) + { + return nameValues.First(nv => nv.name == name).value; + } + + public string getString(string name) + { + JsonString tmp = (JsonString)getValue(name); + return tmp.value; + } + + public double getDouble(string name) + { + JsonNumber tmp = (JsonNumber)getValue(name); + return tmp.value; + } + + public int getInt(string name) + { + JsonNumber tmp = (JsonNumber)getValue(name); + return (int)tmp.value; + } + + public bool getBool(string name) + { + JsonBoolean tmp = (JsonBoolean)getValue(name); + return tmp.value; + } + + public JsonObject getObject(string name) + { + return (JsonObject)getValue(name); + } + + public JsonArray getArray(string name) + { + return (JsonArray)getValue(name); + } + + + + public override void toString(StringBuilder sb, int? indents) + { + + if (indents != null) + { + //appendMany(sb, indentation, (int)indents); + sb.Append("{");sb.Append("\r\n"); + + for (int i = 0; i < this.nameValues.Count; i++ ) + { + appendMany(sb, indentation, (int)indents + 1); + nameValues[i].render(sb, indents+1); + if (i < nameValues.Count - 1) + { + sb.AppendLine(","); + } + else + { + sb.AppendLine(); + } + } + appendMany(sb, indentation, (int)indents); + sb.Append("}"); + } + else + { + sb.Append("{"); + foreach (NameValue item in this.nameValues) + { + item.render(sb); sb.Append(","); + } + sb.Remove(sb.Length - 1, 1);//remove the last comma + sb.Append("}"); + } + } + + } + + + public class NameValue + { + public string name; + public JsonValue value; + + public NameValue(string name) + { + this.name = name; + } + + public NameValue(string name, JsonValue value) + { + this.name = name; + this.value = value; + } + + public NameValue(string name, string value) + { + this.name = name; + if (value != null) + { + this.value = new JsonString(value); + } + else + { + this.value = new JsonNull(); + } + } + + public NameValue(string name, bool value) + { + this.name = name; + this.value = new JsonBoolean(value); + } + + public NameValue(string name, double value) + { + this.name = name; + this.value = new JsonNumber(value); + } + + + internal void render(StringBuilder sb, int? indents= null) + { + sb.AppendFormat("\"{0}\":", name); + value.toString(sb, indents); + } + + + private void appendMany(StringBuilder sb, string s, int times) + { + for (int i = 0; i < times; i++) + { + sb.Append(s); + } + } + + } +} diff --git a/Json/JsonParser.cs b/Json/JsonParser.cs new file mode 100644 index 0000000..6d4f4ee --- /dev/null +++ b/Json/JsonParser.cs @@ -0,0 +1,240 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace Selim.Json +{ + public class JsonParser + { + static string string_pattern = "\"\"|\"(.|\n)*?[^\\\\]\""; //matches also empty string + static string number_pattern = @"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?";// @"[0-9]+(\.[0-9]+)?"; + + static string pattern; + + int state = 3; //instead of 0 to allow single value or an array + Stack stack = new Stack(); + string tmp_name=""; + + static JsonParser() + { + pattern ="("+ string_pattern + "|" +number_pattern +"|{|}|\\[|\\]|\\:|,|true|false|null" + ")"; + } + + public static JsonValue parse(string jsonString) + { + return new JsonParser().Parse(jsonString); + } + + public JsonValue Parse(string text) + { + foreach(Match token in Regex.Matches(text, pattern)) + { + switch (state) + { + //case 0: + // if (Regex.IsMatch(token.Value, "{")) + // { + // state = 1; + // stack.Push(new JsonObject()); + // } + // else + // { + // parsingError(); + // } + // break; + case 1: + if ( token.Value == "}") + { + state = 4; + JsonValue jo = stack.Pop(); + if (stack.Count == 0) + { + return jo;// (JsonObject)jo; + } + } + else if (Regex.IsMatch(token.Value, string_pattern)) + { + state = 2; + tmp_name = token.Value.Trim('"').Replace("\\\"","\""); + } + else + { + parsingError(); + } + break; + case 2: + if (token.Value == ":") + { + state = 3; + } + else + { + parsingError(); + } + break; + case 3: + if (token.Value == "]") + { + state = 4; + JsonValue jo = stack.Pop(); + if (stack.Count == 0) + { + return jo;// (JsonObject)jo; + } + } + else if ( token.Value == "{") + { + state = 1; + JsonObject new_object = new JsonObject(); + if(stack.Count == 0) + { + //do nothing + } + else if (stack.Peek() is JsonObject) + { + ((JsonObject)stack.Peek()).add(tmp_name, new_object); + } + else if (stack.Peek() is JsonArray) + { + ((JsonArray)stack.Peek()).add(new_object); + } + stack.Push( new_object ); + } + else if ( token.Value == "[") + { + state = 3; + JsonArray new_Array = new JsonArray(); + if (stack.Count == 0) + { + //do nothing + } + else if (stack.Peek() is JsonObject) + { + ((JsonObject)stack.Peek()).add(tmp_name, new_Array); + } + else if (stack.Peek() is JsonArray) + { + ((JsonArray)stack.Peek()).add(new_Array); + } + stack.Push( new_Array ); + } + else if (Regex.IsMatch(token.Value, string_pattern)) + { + state = 4; + if (stack.Count == 0) + { + stack.Push(new JsonString(token.Value.Trim('"').Replace("\\\"", "\""))); + } + else if (stack.Peek() is JsonObject) + { + ((JsonObject)stack.Peek()).add(tmp_name, token.Value.Trim('"').Replace("\\\"", "\"")); + } + else if (stack.Peek() is JsonArray) + { + ((JsonArray)stack.Peek()).add(token.Value.Trim('"').Replace("\\\"", "\"")); + } + } + else if (Regex.IsMatch(token.Value, number_pattern)) + { + state = 4; + if (stack.Count == 0) + { + stack.Push(new JsonNumber(Convert.ToDouble(token.Value))); + } + else if (stack.Peek() is JsonObject) + { + ((JsonObject)stack.Peek()).add(tmp_name, Convert.ToDouble(token.Value)); + } + else if (stack.Peek() is JsonArray) + { + ((JsonArray)stack.Peek()).add( Convert.ToDouble(token.Value)); + } + } + else if ( token.Value == "true" || token.Value == "false") + { + state = 4; + if (stack.Count == 0) + { + stack.Push(new JsonBoolean(token.Value == "true" ? true : false)); + } + else if (stack.Peek() is JsonObject) + { + ((JsonObject)stack.Peek()).add( tmp_name, token.Value == "true"? true : false ); + } + else if (stack.Peek() is JsonArray) + { + ((JsonArray)stack.Peek()).add(token.Value == "true" ? true : false); + } + } + else if (token.Value == "null") + { + state = 4; + if (stack.Count == 0) + { + stack.Push(new JsonNull()); + } + else if (stack.Peek() is JsonObject) + { + ((JsonObject)stack.Peek()).add(tmp_name, new JsonNull()); + } + else if (stack.Peek() is JsonArray) + { + ((JsonArray)stack.Peek()).add(new JsonNull()); + } + } + else + { + parsingError(); + } + break; + case 4: + if (token.Value=="]" || token.Value=="}") + { + state = 4; + JsonValue jo= stack.Pop(); + if (stack.Count == 0) + { + return jo;// (JsonObject)jo; + } + } + else if (token.Value == ",") + { + if (stack.Peek() is JsonObject) + { + state = 1; + } + else if (stack.Peek() is JsonArray) + { + state = 3; + } + else + { + parsingError(); + } + } + else + { + parsingError(); + } + + break; + default: + break; + } + }//end foreach + return stack.Pop(); + + } + + + + private void parsingError() + { + throw new Exception("Parsing Error"); + } + + + } +} diff --git a/Json/JsonString.cs b/Json/JsonString.cs new file mode 100644 index 0000000..62ca719 --- /dev/null +++ b/Json/JsonString.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Selim.Json +{ + class JsonString : JsonValue + { + public string value; + public JsonString(string value) + { + this.value = value; + } + + + public override void toString(StringBuilder sb, int? indents) + { + sb.AppendFormat("\"{0}\"", value.Replace("\"","\\\"")); + } + } +} diff --git a/Json/JsonValue.cs b/Json/JsonValue.cs new file mode 100644 index 0000000..93dc161 --- /dev/null +++ b/Json/JsonValue.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Selim.Json +{ + public abstract class JsonValue + { + + public static string indentation = " "; + public string toString(int? indent= null) + { + var sb = new StringBuilder(); + toString(sb, indent); + return sb.ToString(); + } + + + /// + /// this is to be called by toString(), as an optimization tech, + /// to make sure only one instance of StringBuilder is used across nested or successive elements + /// + public abstract void toString(StringBuilder sb, int? indents= null); + + /// + /// overrides the default behavior to get the text forming this element + /// + public override string ToString() + { + return this.toString(); + } + + + protected void appendMany(StringBuilder sb, string s,int times) + { + for (int i = 0; i < times; i++) + { + sb.Append(s); + } + } + + } +}