using System.Collections.Generic;
using System.Text;
namespace Facebook.Utility
{
///
/// JSON (JavaScript Object Notation) Utility Methods.
///
public static class JSONHelper
{
///
/// Converts a Dictionary to a JSON-formatted Associative Array.
///
///Source Dictionary collection [string|string].
///JSON Associative Array string.
public static string ConvertToJSONAssociativeArray(Dictionary dict)
{
var elements = new List();
foreach (var pair in dict)
{
if(!string.IsNullOrEmpty(pair.Value))
{
elements.Add(string.Format("\"{0}\":{2}{1}{2}", EscapeJSONString(pair.Key), EscapeJSONString(pair.Value), IsJSONArray(pair.Value) || IsBoolean(pair.Value) ? string.Empty : "\""));
}
}
return "{" + string.Join(",", elements.ToArray()) + "}";
}
///
/// Determines if input string is a formatted JSON Array.
///
/// string
/// bool
public static bool IsJSONArray(string test)
{
return test.StartsWith("{") && !test.StartsWith("{*") || test.StartsWith("[");
}
///
/// Determines if input string is a boolean value.
///
/// string
/// bool
public static bool IsBoolean(string test)
{
return test.Equals("false") || test.Equals("true");
}
///
/// Converts a List collection of type string to a JSON Array.
///
/// List of strings
/// string
public static string ConvertToJSONArray(List list)
{
if (list == null || list.Count == 0)
{
return "[]";
}
StringBuilder builder = new StringBuilder();
builder.Append("[");
foreach (var item in list)
{
builder.Append(string.Format("{0}{1}{0},", IsJSONArray(item) || IsBoolean(item) ? string.Empty : "\"", EscapeJSONString(item)));
}
builder.Replace(",", "]", builder.Length - 1, 1);
return builder.ToString();
}
///
/// Converts a List collection of type long to a JSON Array.
///
/// List of longs
/// string
public static string ConvertToJSONArray(List list)
{
if (list == null || list.Count == 0)
{
return "[]";
}
StringBuilder builder = new StringBuilder();
builder.Append("[");
foreach (var item in list)
{
builder.Append(string.Format("{0}{1}{0},", IsJSONArray(item.ToString()) || IsBoolean(item.ToString()) ? string.Empty : "\"", EscapeJSONString(item.ToString())));
}
builder.Replace(",", "]", builder.Length - 1, 1);
return builder.ToString();
}
///
/// Converts a JSON Array string to a List collection of type string.
///
/// JSON Array string
/// List of strings
public static List ConvertFromJSONArray(string array)
{
if (!string.IsNullOrEmpty(array))
{
array = array.Replace("[", "").Replace("]", "").Replace("\"", "");
return new List(array.Split(','));
}
return new List();
}
///
/// Converts a JSON Array string to a Dictionary collection of type string, string.
///
/// JSON Array string
/// Dictionary of string, string
public static Dictionary ConvertFromJSONAssoicativeArray(string array)
{
var dict = new Dictionary();
if (!string.IsNullOrEmpty(array))
{
array = array.Replace("{", "").Replace("}", "").Replace("\":", "|").Replace("\"", "").Replace("\\/", "/");
var pairs = new List(array.Split(','));
foreach (var pair in pairs)
{
if (!string.IsNullOrEmpty(pair))
{
var pairArray = pair.Split('|');
dict.Add(pairArray[0], pairArray[1]);
}
}
return dict;
}
return new Dictionary();
}
///
/// Escape backslashes and double quotes of valid JSON content string.
///
/// string
/// string
public static string EscapeJSONString(string originalString)
{
return IsJSONArray(originalString) ? originalString : originalString.Replace("\\/", "/").Replace("/", "\\/").Replace("\\\"", "\"").Replace("\"", "\\\"").Replace("\r", "\\r").Replace("\n", "\\n");
}
}
}