private static Json5Value Transform(Json5Value value, Func <Json5Container, string, Json5Value, Json5Value> transformer) { Json5Object holder = new Json5Object(); holder[""] = value; return(Walk(holder, "", transformer)); }
private static Json5Value Walk(Json5Container holder, string key, Func <Json5Container, string, Json5Value, Json5Value> transformer) { Json5Value value = holder[key]; if (value is Json5Container) { Json5Container c = (Json5Container)value; string[] keys = c.Keys.ToArray(); foreach (string k in keys) { Json5Value v = Walk(c, k, transformer); if (v != null) { c[k] = v; } else { c.Remove(k); } } } // Special case for holder if (key == "") { return(value); } return(transformer(holder, key, value)); }
public static string Stringify(Json5Value value, Func <Json5Container, string, Json5Value, Json5Value> replacer, string space = null) { if (replacer != null) { value = Transform(value, replacer); } return(value.ToJson5String(space)); }
public static string Stringify(Json5Value value, IEnumerable <string> keys, string space = null) { Func <Json5Container, string, Json5Value, Json5Value> replacer = null; if (keys != null) { replacer = (t, k, v) => keys.Contains(k) ? v : null; } return(Stringify(value, replacer, space)); }
/// <summary> /// Parses JSON5 text into a JSON5 value. /// </summary> /// <param name="text">The text to parse.</param> /// <param name="reviver">An optional function to tranform the parsed values.</param> /// <returns>A JSON5 value.</returns> public static Json5Value Parse(string text, Func <Json5Container, string, Json5Value, Json5Value> reviver = null) { Json5Parser parser = new Json5Parser(new StringReader(text)); Json5Value value = parser.Parse(); if (reviver != null) { return(Transform(value, reviver)); } return(value); }
private static Json5Value Walk(Json5Container holder, string key, Func <Json5Container, string, Json5Value, Json5Value> transformer) { Json5Value value = holder[key]; if (value is Json5Container) { Json5Container c = (Json5Container)value; foreach (string k in c.Keys) { Json5Value v = Walk(c, k, transformer); if (v != null) { c[k] = v; } else { c.Remove(k); } } } return(transformer(holder, key, value)); }
public static string Stringify(Json5Value value, int space) { return(Stringify(value, (Func <Json5Container, string, Json5Value, Json5Value>)null, space)); }
public static string Stringify(Json5Value value, IEnumerable <string> keys, int space) { return(Stringify(value, keys, new string(' ', Math.Max(space, 10)))); }
public static string Stringify(Json5Value value, Func <string, Json5Value, Json5Value> replacer, int space) { throw new NotImplementedException(); }
public static string Stringify(Json5Value value, Func <Json5Container, string, Json5Value, Json5Value> replacer, int space) { return(Stringify(value, replacer, new string(' ', Math.Max(space, 10)))); }