Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1.     public static class JsonProcessStringHelper
  2.     {
  3.         public static void ProcessStrings(this JObject jObject, Func<string, string> processFunc)
  4.         {
  5.             foreach (var property in jObject.Properties())
  6.             {
  7.                 var token = property.Value;
  8.                 token.ProcessStrings(processFunc);
  9.             }
  10.         }
  11.  
  12.         public static void ProcessStrings(this JToken token, Func<string, string> processFunc)
  13.         {
  14.             if (token.Type == JTokenType.Array)
  15.             {
  16.                 var arrayToken = (JArray)token;
  17.                 foreach (var item in arrayToken)
  18.                 {
  19.                     item.ProcessStrings(processFunc);
  20.                 }
  21.             }
  22.             if (token.Type == JTokenType.Object)
  23.             {
  24.                 var tokenObject = token as JObject;
  25.                 tokenObject.ProcessStrings(processFunc);
  26.             }
  27.             if (token.Type == JTokenType.String)
  28.             {
  29.                 var jValue = token as JValue;
  30.                 if (jValue != null)
  31.                 {
  32.                     var value = jValue;
  33.                     value.Value = processFunc(value.Value.ToString());
  34.                 }
  35.             }
  36.         }
  37.  
  38.         public static string SubstringChecked(this string value, int startIndex, int length)
  39.         {
  40.             if ((length - startIndex) >=value.Length)
  41.             return value;
  42.             return value.Substring(startIndex, length);
  43.         }
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement