public static string Stringify(object ObjectToEncode, bool SingleQuotes = false)
{
if (ObjectToEncode == null)
{
return "null";
}
if (ObjectToEncode is string)
{
var Quote = SingleQuotes ? '\'' : '"';
return Quote + Escape(ObjectToEncode as string) + Quote;
}
if (ObjectToEncode is bool)
{
return (((bool)ObjectToEncode) == true) ? "true" : "false";
}
if (ObjectToEncode is IDictionary) {
var Dict = ObjectToEncode as IDictionary;
var Str = "";
foreach (var Key in Dict.Keys)
{
var Value = Dict[Key];
if (Str.Length > 0) Str += ",";
Str += Stringify(Key.ToString(), SingleQuotes) + ":" + Stringify(Value, SingleQuotes);
}
return "{" + Str + "}";
}
if (ObjectToEncode is IEnumerable)
{
var List = ObjectToEncode as IEnumerable;
var Str = "";
foreach (var Item in List)
{
if (Str.Length > 0) Str += ",";
Str += Stringify(Item, SingleQuotes);
}
return "[" + Str + "]";
}
var IJsonSerializable = ObjectToEncode as IJsonSerializable;
if (IJsonSerializable != null)
{
return IJsonSerializable.ToJson();
}
double NumericResult;
string NumericStr = Convert.ToString(ObjectToEncode, CultureInfo.InvariantCulture.NumberFormat);
if (Double.TryParse(NumericStr, out NumericResult))
{
return NumericStr;
}
else
{
//throw (new NotImplementedException("Don't know how to encode '" + ObjectToEncode + "'."));
return Stringify(ObjectToEncode.ToString(), SingleQuotes);
}
}