Summary
Replaces a value in a json object. Can be a simple value or complex object (or anonymous type)
This will not merge any values.
Syntax
public static string AddOrUpdateJsonValue<T>(this string json, string keyPath, T value, char delimiter = ':')
Examples
Set the connection string for an application
var json = System.IO.File.ReadAllText("path/to/appsettings.json");
json = PokeJson.AddOrUpdateJsonValue(json, "ConnectionStrings:DefaultConnection", "MyConnectionString");
System.IO.File.WriteAllText("path/to/appsettings.json", json);
Key's don't need to exist yet so the following calls will be equivalent:
var json1 = PokeJson.AddOrUpdateJsonValue(string.Empty, "MySection:Nested:Val", 1);
var json2 = PokeJson.AddOrUpdateJsonValue(string.Empty, "MySection", new
{
Nested = new
{
Val = 1
}
});
json1.ShouldBe(json2);
Type Parameters
Name |
Description |
T |
type of the value. Should be inferred by the compiler |
Parameters
Name |
Type |
Description |
json |
string |
A json object string. Will replace empty string with empty json object |
keyPath |
string |
path splited by delimiter |
value |
T |
the value to use. Can be null to erase an object |
delimiter |
char |
the delimiter to split the keyPath |
Return Value
Type |
Description |
string |
formated json with value replaced |