Example #1
0
        public string getString(string name)
        {
            JsonVar var = this.get(name);

            if (var != null)
            {
                if (var is JsonString)
                {
                    return(((JsonString)var).getValue());
                }
                else if ((var is JsonObject) || (var is JsonArray))
                {
                    throw new System.InvalidOperationException("Key " + name + " is not convertible to string.");
                }
                else
                {
                    if (var is JsonNumber)
                    {
                        return(Convert.ToString(((JsonNumber)var).getDoubleValue()));
                    }
                    else if (var is JsonBool)
                    {
                        return(Convert.ToString(((JsonBool)var).getValue()));
                    }
                    else
                    {
                        throw new System.Exception();
                    }
                }
            }
            else
            {
                throw new System.InvalidOperationException("No such key: " + name);
            }
        }
Example #2
0
        public JsonArray getArray(string name)
        {
            JsonVar var = this.get(name);

            if (var != null)
            {
                if (var is JsonArray)
                {
                    return((JsonArray)var);
                }
                else
                {
                    throw new System.InvalidOperationException("Key " + name + " is not an array");
                }
            }
            else
            {
                throw new System.InvalidOperationException("No such key: " + name);
            }
        }
Example #3
0
        public int getInt(string name)
        {
            JsonVar var = this.get(name);

            if (var != null)
            {
                if (var is JsonNumber)
                {
                    return(((JsonNumber)var).getIntValue());
                }
                else
                {
                    throw new System.InvalidOperationException("Key " + name + " is not a number");
                }
            }
            else
            {
                throw new System.InvalidOperationException("No such key: " + name);
            }
        }
Example #4
0
        public bool getBool(string name)
        {
            JsonVar var = this.get(name);

            if (var != null)
            {
                if (var is JsonBool)
                {
                    return(((JsonBool)var).getValue());
                }
                else
                {
                    throw new System.InvalidOperationException("Key " + name + " is not a bool");
                }
            }
            else
            {
                throw new System.InvalidOperationException("No such key: " + name);
            }
        }
Example #5
0
        public JsonObject getObject(string name)
        {
            JsonVar var = this.get(name);

            if (var != null)
            {
                if (var is JsonObject)
                {
                    return((JsonObject)var);
                }
                else
                {
                    throw new System.InvalidOperationException("Key " + name + " is not an object");
                }
            }
            else
            {
                throw new System.InvalidOperationException("No such key: " + name);
            }
        }
Example #6
0
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder(128);
            List <string> keys    = this.getChildren();

            builder.Append('{');

            for (int i = 0; i < keys.Count - 1; i++)
            {
                JsonVar var = this.children[keys[i]];

                builder.Append('\"');
                builder.Append(var.getName());
                builder.Append('\"');

                builder.Append(" : ");

                builder.Append(var.ToString());

                builder.Append(", ");
            }

            if (keys.Count > 0)
            {
                JsonVar var = this.children[keys[keys.Count - 1]];

                builder.Append('\"');
                builder.Append(var.getName());
                builder.Append('\"');

                builder.Append(" : ");

                builder.Append(var.ToString());
            }

            builder.Append('}');
            return(builder.ToString());
        }
Example #7
0
        public override void writeToStream(System.IO.StreamWriter stream)
        {
            List <string> keys = this.getChildren();

            stream.Write('{');

            for (int i = 0; i < keys.Count - 1; i++)
            {
                JsonVar var = this.children[keys[i]];

                stream.Write('\"');
                stream.Write(var.getName());
                stream.Write('\"');

                stream.Write(" : ");

                var.writeToStream(stream);

                stream.Write(", ");
            }

            if (keys.Count > 0)
            {
                JsonVar var = this.children[keys[keys.Count - 1]];

                stream.Write('\"');
                stream.Write(var.getName());
                stream.Write('\"');

                stream.Write(" : ");

                var.writeToStream(stream);
            }

            stream.Write('}');
        }
Example #8
0
 public bool contains(JsonVar var)
 {
     return(this.items.Contains(var));
 }
Example #9
0
 /*
  * public void add(JsonObject obj) {
  *  this.items.Add(obj);
  *  this.dirty = true;
  * }
  * */
 public void add(JsonVar var)
 {
     this.items.Add(var);
     this.dirty = true;
 }
Example #10
0
 public void set(string name, JsonVar var)
 {
     var.setName(name);
     this.children[name] = var;
     this.dirty          = true;
 }