Show / Hide Table of Contents

Class JsonHelper

Common class with helper methods for JSON.

Inheritance
System.Object
JsonHelper
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ToString()
System.Object.ReferenceEquals(System.Object, System.Object)
Namespace: StoneCo.Framework.Common.Helpers
Assembly: cs.temp.dll.dll
Syntax
public class JsonHelper

Methods

IsValidJson(String)

Verify if the JSON string is valid.

Declaration
public static bool IsValidJson(string json)
Parameters
Type Name Description
System.String json

JSON on string format.

Returns
Type Description
System.Boolean

True if this JSON string is valid, otherwise, false.

Examples
var jsonString = @"{""Cpf"":""15486325787"", ""Name"":""Stephen William Hawking""}";

var validation = JsonHelper.IsValidJson(jsonString);

MaskSensitiveData(String, List<String>)

Mask the sensitive data into a JSON.

A recursive query is executed inside the JSON string to fetch the sensitive properties that are within the list passed by parameter. After you find the properties, their values ​​are replaced by ***.

note

If the JSON string is null, empty, or not a valid JSON object, the same JSON string is returned without masking the sensitive data.

warning

The value can only be replaced by *** if the field is of type string, otherwise an exception will be thrown.

Declaration
public static string MaskSensitiveData(string json, List<string> sensitiveProperties)
Parameters
Type Name Description
System.String json

JSON on string format.

List<System.String> sensitiveProperties

List of string with the properties names.

Returns
Type Description
System.String

JSON on string format with sensitive data masked.

Examples

Foo class used in this example.

public class Foo
{
    public string Name { get; set; }

    public string Cpf { get; set; }
}

Bar class to use JsonHelper sample.

public class Bar
{
    public void MaskData()
    {
        var foo = new Foo { Cpf = "15486325787", Name = "Stephen William Hawking" };
        var sensitiveProperties = new List<string> { "cpf" };
        var jsonString = JsonConvert.SerializeObject(foo);

        var result = JsonHelper.MaskSensitiveData(jsonString, sensitiveProperties);
    }
}

Result value is...

{
   "Cpf":"***",
   "Name":"Stephen William Hawking"
}
Back to top Generated by DocFX