Weknow.Text.Json.Extensions 5.1.43

.NET 6.0
Install-Package Weknow.Text.Json.Extensions -Version 5.1.43
dotnet add package Weknow.Text.Json.Extensions --version 5.1.43
<PackageReference Include="Weknow.Text.Json.Extensions" Version="5.1.43" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Weknow.Text.Json.Extensions --version 5.1.43
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Weknow.Text.Json.Extensions, 5.1.43"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Weknow.Text.Json.Extensions as a Cake Addin
#addin nuget:?package=Weknow.Text.Json.Extensions&version=5.1.43

// Install Weknow.Text.Json.Extensions as a Cake Tool
#tool nuget:?package=Weknow.Text.Json.Extensions&version=5.1.43
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Weknow Json Extensions

Prepare Build & Deploy NuGet
NuGet

Functionality of this library includes:

YieldWhen

Enumerate over json elements.

With Path

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

With Filter

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

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"
}

Path based Filter

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

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"}

Merge

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}

Merge Into

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] }
}

Serialization

ToJson

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();

AsString

Convert JsonElement to string

JsonElement json = ...;
string compact = json.AsString();
string indented = json.AsIndentString();
string raw = json.GetRawText(); // same as json.AsIndentString();

ToStream

Convert JsonElement to stream

JsonElement json = ...;
Stream srm = json.ToStream();

ImmutableDictionary converter.

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

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Weknow.Text.Json.Extensions:

Package Downloads
Weknow.EventSource.Backbone.Contracts

Package Description

Weknow.EventSource.Backbone.Channels.RedisProvider.Common

Package Description

Weknow.EventSource.Backbone.Channels.S3StoreProvider.Common

Package Description

Weknow.EventSource.Backbone.Channels.RedisConsumerProvider

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.1.43 152 4/12/2022
5.1.42 74 4/12/2022
5.1.41 72 4/12/2022
5.1.40 61 3/16/2022
5.1.39 129 3/16/2022
5.1.38 121 3/10/2022
5.1.37 300 3/10/2022
5.1.36 58 3/10/2022
5.1.35 120 3/10/2022
5.1.34 81 3/10/2022
5.1.21 607 3/2/2022
5.1.20 137 3/2/2022
5.1.18 433 1/17/2022
5.1.17 234 1/13/2022
5.1.16 353 1/6/2022
5.1.15 1,421 12/28/2021
5.1.14 203 12/28/2021
5.1.13 2,106 11/28/2021
5.1.9 1,664 11/28/2021
5.1.8 2,283 11/17/2021
5.1.7 104 11/17/2021
5.1.6 1,617 11/9/2021
5.1.5 431 11/8/2021
5.1.4 291 11/8/2021
5.1.3 132 11/8/2021
5.1.2 124 11/8/2021
5.1.1 249 11/8/2021
5.0.14 284 11/7/2021
5.0.13 166 11/3/2021
5.0.12 424 11/3/2021
5.0.11 514 8/9/2021
5.0.10 134 8/9/2021
5.0.9 157 8/9/2021
5.0.8 11,041 3/24/2021
5.0.7 186 3/9/2021
1.0.7 410 9/3/2020
1.0.6 276 9/3/2020
1.0.5 440 8/2/2020
1.0.4 327 8/2/2020
1.0.3 395 6/17/2020