With json.net, is there a shortish way to manipulate all string fields? -
given arbitrary newtonsoft.json.linq.jobject, if want apply function string values appear in (wherever may be) - whether basic value of property, or in array in json, best way this?
one simple way use jcontainer.descendantsandself()
find descendants of root jobject
string values, replace value remapped string using jtoken.replace()
:
public static class jsonextensions { public static jtoken mapstringvalues(this jcontainer root, func<string, string> func) { foreach (var value in root.descendantsandself().oftype<jvalue>().where(v => v.type == jtokentype.string).tolist()) value.replace((jvalue)func((string)value.value)); return root; } }
then use like:
jobj.mapstringvalues(s => "remapped " + s);
Comments
Post a Comment