Created
December 19, 2013 17:29
-
-
Save samsalisbury/8043044 to your computer and use it in GitHub Desktop.
Anonymise certain fields in a blob of JSON using Json.Net + JToken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
namespace ExternalApi.Logging | |
{ | |
public static class UserInputAnonymiser | |
{ | |
public static JToken Anonymise(JToken data) | |
{ | |
foreach (var prop in data.Children<JProperty>()) | |
{ | |
if (ShouldBeAnonymised(prop.Name)) | |
prop.Value = "-{anonymised}-"; | |
else if (prop.Value.HasValues) | |
prop.Value = Anonymise(prop.Value); | |
} | |
return data; | |
} | |
private readonly static string[] AnonymousProps = { "firstname", "lastname", "emailaddress", "phonenumber" }; | |
private static bool ShouldBeAnonymised(string propertyName) | |
{ | |
return AnonymousProps.Any(x => propertyName.ToLowerInvariant().Contains(x)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment