public static string StringifyArray(Array arr, SimpleJsonConfig cfg) { if (null == arr) { return(GetNullObjectIndicator(cfg)); } string seperator = GetSeperator(cfg); StringBuilder json = new StringBuilder(""); json.Append(GetArrBracketIndicator(cfg, true)); bool isEmpty = true; foreach (Object o in arr) { json.Append(StringifyInternal(o.GetType(), o, cfg)); json.Append(seperator); isEmpty = false; } if (!isEmpty) { json.Remove(json.Length - seperator.Length, seperator.Length); } json.Append(GetArrBracketIndicator(cfg, false)); return(json.ToString()); }
private static string StringifyInternal(Type T, Object obj, SimpleJsonConfig cfg) { // Dispatch if (TypeHelper.IsEnum(T)) { return(StringifyEnum(obj)); } else if (TypeHelper.IsValue(T)) { return(StringifyValue(obj)); } else if (TypeHelper.IsString(T)) { return(StringifyString(obj)); } else if (TypeHelper.IsArrayType(T)) { return(StringifyArray((Array)obj, cfg)); } else if (TypeHelper.IsList(T)) { return(StringifyList((IList)obj, cfg)); } else if (TypeHelper.IsDictionary(T)) { return(StringifyDict((IDictionary)obj, cfg)); } else { return(StringifyObject(obj, cfg)); } }
static string GetPerModulePrefix(SimpleJsonConfig cfg, int step) { if (null != cfg && null != cfg.linePrefix) { return(cfg.linePrefix(step)); } return(""); }
static string GetNullObjectIndicator(SimpleJsonConfig cfg) { if (null != cfg && null != cfg.nullObjectIndicator) { return(cfg.nullObjectIndicator()); } return(DEFAULT_NULL_OBJECT_INDICATOR); }
static string GetNullStringIndicator(SimpleJsonConfig cfg) { if (null != cfg && null != cfg.nullStringIndicator) { return(cfg.nullStringIndicator()); } return(DEFAULT_NULL_STRING_INDICATOR); }
static string GetContent(string metaContent, SimpleJsonConfig cfg) { if (null != cfg && null != cfg.bodyFilter) { return(cfg.KeyFilter(metaContent)); } return(metaContent); }
static string GetKey(string metaKey, SimpleJsonConfig cfg) { if (null != cfg && null != cfg.KeyFilter) { return(cfg.KeyFilter(metaKey)); } return(metaKey); }
public static string ToJson <T>(T data) { SimpleJsonConfig cfg = new SimpleJsonConfig(); cfg.KeyFilter = FieldNameFilter; // cfg.excludes = excludes; return(SimpleJsonSerializer.Stringify(data, cfg)); }
static string GetArrBracketIndicator(SimpleJsonConfig cfg, bool is_left) { string bracket = is_left ? "[" : "]"; if (null != cfg && null != cfg.arrBracketFilter) { bracket = cfg.arrBracketFilter(bracket); } return(bracket); }
static string GetSeperator(SimpleJsonConfig cfg) { string ret = DEFAULT_SEPERATOR; if (null != cfg && null != cfg.seperatorFilter) { ret = cfg.seperatorFilter(ret); } return(ret); }
// internal procs static bool IsValidType(Type t, SimpleJsonConfig cfg) { if (null == cfg) { return(true); } Type target_type = TypeHelper.IsGenericType(t) ? TypeHelper.GetElementType(t) : t; return(cfg.ScreenType(target_type)); }
public static string StringifyString(object v, SimpleJsonConfig cfg = null) { if (null == v) { return(GetNullStringIndicator(cfg)); } else { string str = (v as string); return("\"" + str + "\""); } }
public static string Stringify(Object obj, SimpleJsonConfig cfg = null) { if (null == obj) { return(GetNullObjectIndicator(cfg)); } Type T = obj.GetType(); if (!IsValidType(T, cfg)) { return(GetNullObjectIndicator(cfg)); } return(StringifyInternal(T, obj, cfg)); }
private static string StringifyObject(Object obj, SimpleJsonConfig cfg) { if (null == obj) { return(GetNullObjectIndicator(cfg)); } Type T = obj.GetType(); StringBuilder json = new StringBuilder(""); json.Append(GetObjBracketIndicator(cfg, true)); bool isEmpty = true; string seperator = GetSeperator(cfg); foreach (FieldInfo field in T.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { Type fieldType = field.FieldType; if (!IsValidType(fieldType, cfg)) { continue; } string fieldKey = GetKey(field.Name, cfg); json.AppendFormat("\"{0}\":", fieldKey); object fieldObj = field.GetValue(obj); json.Append(StringifyInternal(fieldType, fieldObj, cfg)); json.Append(seperator); isEmpty = false; } if (!isEmpty) { json.Remove(json.Length - seperator.Length, seperator.Length); } json.Append(GetObjBracketIndicator(cfg, false)); return(json.ToString()); }
public static string StringifyDict(IDictionary dict, SimpleJsonConfig cfg) { if (null == dict) { return(GetNullObjectIndicator(cfg)); } string seperator = GetSeperator(cfg); StringBuilder json = new StringBuilder(""); json.Append(GetObjBracketIndicator(cfg, true)); bool isEmpty = true; Type[] temp = dict.GetType().GetGenericArguments(); Type keyType = temp[0]; Type valueType = temp[1]; IDictionaryEnumerator enumlator = dict.GetEnumerator(); while (enumlator.MoveNext()) { string keyStr = StringifyInternal(keyType, enumlator.Key, cfg); if (!keyStr.StartsWith("\"")) { keyStr = "\"" + keyStr + "\""; } string valueStr = StringifyInternal(valueType, enumlator.Value, cfg); json.AppendFormat("{0}:{1}", keyStr, valueStr); json.Append(seperator); isEmpty = false; } if (!isEmpty) { json.Remove(json.Length - seperator.Length, seperator.Length); } json.Append(GetObjBracketIndicator(cfg, false)); return(json.ToString()); }