Functionality of this library includes:
- YieldWhen
- Filter
- [Path based Filter][#Path-based-Filter]
- Exclude
- TryAddProperty
- Merge
- Serialization
Enumerate over json elements.
Rely on path convention:
json.YieldWhen(/path convention/);
{
"friends": [
{
"name": "Yaron",
"id": 1
},
{
"name": "Aviad",
"id": 2
}
]
}
- "friends.[].name" or "friends.*.name" will result with ["Yaron", "Aviad"]
- "friends.[0].name" or "friends.*.name" will result with ["Yaron"]
- "friends.[0].*" or "friends.*.name" will result with ["Yaron",1]
See: YieldWhen_Path_Test
using static System.Text.Json.TraverseFlowInstruction;
var items = source.YieldWhen((json, deep, breadcrumbs) =>
{
string last = breadcrumbs[^1];
if(deep == 0 && last == "skills")
return Drill;
return deep switch
{
< 3 => Drill,
_ => Do(TraverseFlow.SkipToParent),
};
});
Filter operation, clean up the element according to a filter.
It excludes whatever doesn't match the filter
{
"A": 10,
"B": [
{ "Val": 40 },
{ "Val": 20 },
{ "Factor": 20 }
],
"C": [0, 25, 50, 100 ],
"Note": "Re-shape json"
}
Filter:
JsonElement source = ..;
var target = source.Filter((e, _, _) =>
e.ValueKind != JsonValueKind.Number || e.GetInt32() > 30
? TraverseFlowWrite.Drill
: TraverseFlowWrite.Skip);
Will result in:
{
"B": [ { "Val": 40 }],
"C": [ 50, 100 ],
"Note": "Re-shape json"
}
var target = source.Filter("B.*.val");
// results: {"B":[{"Val":40},{"Val":20}]}
var target = source.Filter("B.[]");
// results: {"B":[{"Val":40},{"Val":20},{"Factor":20}]}
var target = source.Filter("B.[].Factor");
// results: {"B":[{"Factor":20}]}
var target = source.Filter("B.[1].val");
// results: {"B":[{"Val":20}]}
Exclude is kind of opposite of Filter. It instruct which elements to remove.
{
"A": 10,
"B": [
{ "Val": 40 },
{ "Val": 20 },
{ "Factor": 20 }
],
"C": [0, 25, 50, 100 ],
"Note": "Re-shape json"
}
var target = source.Exclude("B.*.val");
// results: {"A":10,"B":[{"Factor":20}],"C":[0,25,50,100],"Note":"Re-shape json"}
var target = source.Exclude("B.[]");
// results: {"A":10,"B":[],"C":[],"Note":"Re-shape json"}
var target = source.Exclude("B.[1]");
// results: {"A":10,"B":[{"Val":40},{"Factor":20}],"C":[0,50,100],"Note":"Re-shape json"}
var target = source.Exclude("B");
// results: {"A":10,"C":[0,25,50,100],"Note":"Re-shape json"}
Try to add property if missing.
{ "A": 0, "B": 0 }
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("C", 1);
Result in:
{ "A": 0, "B": 0, "C": 1 }
{ "A": 0, "B": 0, "C": 0 }
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("C", 1);
Result in:
{ "A": 0, "B": 0, "C": 0 }
{ "A": 0, "B": 0, "C": null }
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("C", 1);
Result in:
{ "A": 0, "B": 0, "C": 1 }
Unless sets the options not to ignore null
var options = new JsonPropertyModificatonOpions
{
IgnoreNull = false
};
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("C", 1);
Which will result in:
{ "A": 0, "B": 0, "C": null }
Changing property within a path
{
"X": {
"Y": {
"A": 0,
"B": 0
}
},
"Q": 2
} "Q": 2
}
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("X.Y", "C", 1);
Result in:
{
"X": {
"Y": {
"A": 0,
"B": 0,
"C": 1
}
},
"Q": 2
}
Merging 2 or more json. The last json will override previous on conflicts
{
"A": 1
}
{
"B": 2
}
var target = source.Merge(joined);
// results: {"A":1,"B":2}
var target = source.Merge(new { B = 2}); // anonymous type
// results: {"A":1,"b":2}
More scenarios:
- {"A":1}.Merge({"B":2,"C":3}) = {"A":1, "B":2, "C"":3}
- {"A":1}.Merge({"B":2},{"C":3}) = {"A":1, "B":2, "C"":3}
- {"A":1}.Merge({"B":2},{"C":3}) = {"A":1, "B":2, "C"":3}
Merging json into specific path of a source json. The last json will override previous on conflicts
{
"A": 1,
"B": {
"B1":[1,2,3]
}
}
var joined = 5;
var target = source.MergeInto("B.B1.[1]", joined);
// results: { "A": 1, "B": { "B1":[1,5,3] }
}
var joined = ... // {"New":"Object"};
var target = source.MergeInto("B.B1.[1]", joined);
// results: { "A": 1, "B": { "B1":[1,{"New":"Object"},3] }
}
var joined = new {"New":"Object"}; // anonymous type
var target = source.MergeInto("B.B1.[1]", joined);
// results: { "A": 1, "B": { "B1":[1,{"new":"Object"},3] }
}
Convert .NET object into JsonElement.
var entity = new Entity(12, new BEntity("Z"));
JsonElement json = entity.ToJson();
var arr = new []{ 1, 2, 3 };
JsonElement json = arr.ToJson();
Convert JsonElement to string
JsonElement json = ...;
string compact = json.AsString();
string indented = json.AsIndentString();
string raw = json.GetRawText(); // same as json.AsIndentString();
Convert JsonElement to stream
JsonElement json = ...;
Stream srm
### ImmutableDictionary converter.
``` csharp
JsonStringEnumConverter EnumConvertor = new JsonStringEnumConverter(JsonNamingPolicy.CamelCase);
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
// Add the converter
Converters = { EnumConvertor, JsonImmutableDictionaryConverter.Default }
};
var source = new Dictionary<ConsoleColor, string>
{
[ConsoleColor.Blue] = nameof(ConsoleColor.Blue),
[ConsoleColor.White] = nameof(ConsoleColor.White)
};
string json = JsonSerializer.Serialize(source, options);
T deserialized = JsonSerializer.Deserialize<T>(json, options);
Looking for other extensions?
Check the following