/*
JSONSharp, a c# library for generating strings in JSON format
Copyright (C) 2007 Jeff Rodenburg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
If you have questions about the library, please contact me at jeff.rodenburg@gmail.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using JSONSharp;
using JSONSharp.Values;
namespace JSONSharp.Collections
{
///
/// JSONObjectCollection is an unordered set of name/value pairs. An object begins
/// with "{" (left brace) and ends with "}" (right brace). Each name is followed
/// by ":" (colon) and the name/value pairs are separated by "," (comma).
///
public class JSONObjectCollection : JSONValueCollection
{
private Dictionary _namevaluepairs;
private readonly string NAMEVALUEPAIR_SEPARATOR = ":";
///
/// Public constructor that accepts a Dictionary of name/value pairs.
///
/// Dictionary collection of name/value pairs (JSONStringValue=name,JSONValue=pair)
public JSONObjectCollection(Dictionary namevaluepairs)
: base()
{
this._namevaluepairs = namevaluepairs;
}
///
/// Empty public constructor. Use this method in conjunction with
/// the Add method to populate the internal dictionary of name/value pairs.
///
public JSONObjectCollection()
: base()
{
this._namevaluepairs = new Dictionary();
}
///
/// Adds a JSONStringValue as the "name" and a JSONValue as the "value" to the
/// internal Dictionary. Values are checked to ensure no duplication occurs
/// in the internal Dictionary.
///
/// JSONStringValue "name" to add to the internal dictionary
/// JSONValue "value" to add to the internal dictionary
public void Add(JSONStringValue name, JSONValue value)
{
if (!this._namevaluepairs.ContainsKey(name))
this._namevaluepairs.Add(name, value);
}
///
/// Required override of the CollectionToPrettyPrint() method.
///
/// the entire dictionary as a string in JSON-compliant format, with indentation for readability
protected override string CollectionToPrettyPrint()
{
JSONValue.CURRENT_INDENT++;
List output = new List();
List nvps = new List();
foreach (KeyValuePair kvp in this._namevaluepairs)
nvps.Add("".PadLeft(JSONValue.CURRENT_INDENT, Convert.ToChar(base.HORIZONTAL_TAB)) + kvp.Key.PrettyPrint() + this.NAMEVALUEPAIR_SEPARATOR + kvp.Value.PrettyPrint());
output.Add(string.Join(base.JSONVALUE_SEPARATOR + Environment.NewLine, nvps.ToArray()));
JSONValue.CURRENT_INDENT--;
return string.Join("", output.ToArray());
}
///
/// Required override of the CollectionToString() method.
///
/// the entire collection as a string in JSON-compliant format
protected override string CollectionToString()
{
List output = new List();
List nvps = new List();
foreach (KeyValuePair kvp in this._namevaluepairs)
nvps.Add(kvp.Key.ToString() + this.NAMEVALUEPAIR_SEPARATOR + kvp.Value.ToString());
output.Add(string.Join(base.JSONVALUE_SEPARATOR, nvps.ToArray()));
return string.Join("", output.ToArray());
}
///
/// Required override of the BeginMarker property
///
protected override string BeginMarker
{
get { return "{"; }
}
///
/// Required override of the EndMarker property
///
protected override string EndMarker
{
get { return "}"; }
}
}
}