Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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());
        }
Пример #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('}');
        }
Пример #8
0
 public bool contains(JsonVar var)
 {
     return(this.items.Contains(var));
 }
Пример #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;
 }
Пример #10
0
 public void set(string name, JsonVar var)
 {
     var.setName(name);
     this.children[name] = var;
     this.dirty          = true;
 }