0

I want to log the serialized string but it is a sensitive data.

I only need to log property names, not values for example if I get a null reference error, I only need to know which property exist in the object. I actually use the original object to get a PDF from a PDF template.

I need to redact strings, integers etc but not the lists or arrays etc. so the serializer should be responsible to do this redacting.

Is there any way to achieve this in Newtonsoft.Json.JsonConvert

Of course i dont need to deserialize the object later.

yılmaz
  • 365
  • 1
  • 14
  • 1
    Seems you need to write your own [custom converter](https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm). – Oliver Mar 14 '22 at 08:15
  • This doesn't really sound like something the JSON conversion step should be responsible for. Consider changing the object itself, or else explicitly having a `SomethingSomethingRedacted` class that you fill manually or with something like Automapper. You can't inadvertently expose what isn't there. – Jeroen Mostert Mar 14 '22 at 08:15
  • @JeroenMostert this is the same as doing the serialization myself. I dont know the structure of the data model, it can be anything. – yılmaz Mar 14 '22 at 08:18
  • What format do you have the value in at the moment? If it's a JObject for example - or if you're happy to parse it to that - it wouldn't be too hard to replace all string JSON tokens with "xxx" for example. – Jon Skeet Mar 14 '22 at 11:23
  • Can you show a small example? @JonSkeet – yılmaz Mar 14 '22 at 12:08
  • Not without knowing whether or not it would be suitable - that would be a waste of my time. If you can clarify your requirements (e.g. with sample code showing the format you've already got the value in, and any performance requirements) then I'll happily write an answer. – Jon Skeet Mar 14 '22 at 12:14
  • If you want to redact values during serialization you could do it with a custom value provider, see [Replace sensitive data value on JSON serialization](https://stackoverflow.com/q/33148957/3744182). You would need to generalize the code to test for all possible types to redact. If you have a `JObject` or `JArray` and want to replace primitive values, you could start with [With json.net, is there a shortish way to manipulate all string fields?](https://stackoverflow.com/q/46148971/3744182). You would need to generalize the code to handle all primitive types. – dbc Mar 14 '22 at 16:04
  • Do either of the approaches from those two questions meet your needs? If so, which question? And does that question answer yours sufficiently, or do you need more specific help? – dbc Mar 14 '22 at 16:05
  • Possible duplicate of [How to mask sensitive values in JSON for logging purposes](https://stackoverflow.com/q/37821298/10263) – Brian Rogers Mar 15 '22 at 02:54

0 Answers0