Weknow.Text.Json.Extensions 5.1.50

dotnet add package Weknow.Text.Json.Extensions --version 5.1.50                
NuGet\Install-Package Weknow.Text.Json.Extensions -Version 5.1.50                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Weknow.Text.Json.Extensions" Version="5.1.50" />                
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.50                
#r "nuget: Weknow.Text.Json.Extensions, 5.1.50"                
#r directive can be used in F# Interactive and Polyglot Notebooks. 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.50

// Install Weknow.Text.Json.Extensions as a Cake Tool
#tool nuget:?package=Weknow.Text.Json.Extensions&version=5.1.50                

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

TryAddProperty

Try to add property if missing.

Sample 1

{ "A": 0, "B": 0 }
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("C", 1);

Result in:

{ "A": 0, "B": 0, "C": 1 }

Sample 2

{ "A": 0, "B": 0, "C": 0 }
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("C", 1);

Result in:

{ "A": 0, "B": 0, "C": 0 }

Sample 3

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

Sample 4

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
}

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

### 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

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 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

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.1.50 1,526 2/16/2023
5.1.49 1,519 1/23/2023
5.1.48 293 1/22/2023
5.1.47 2,448 12/19/2022
5.1.46 1,034 11/15/2022
5.1.44 19,112 9/24/2022
5.1.43 14,715 4/12/2022
5.1.42 439 4/12/2022
5.1.41 460 4/12/2022
5.1.40 446 3/16/2022
5.1.39 2,106 3/16/2022
5.1.38 2,169 3/10/2022
5.1.37 7,093 3/10/2022
5.1.36 432 3/10/2022
5.1.35 2,147 3/10/2022
5.1.34 469 3/10/2022
5.1.21 13,665 3/2/2022
5.1.20 2,159 3/2/2022
5.1.18 10,339 1/17/2022
5.1.17 2,428 1/13/2022
5.1.16 5,222 1/6/2022
5.1.15 11,177 12/28/2021
5.1.14 1,185 12/28/2021
5.1.13 8,142 11/28/2021
5.1.9 1,866 11/28/2021
5.1.8 14,628 11/17/2021
5.1.7 280 11/17/2021
5.1.6 8,258 11/9/2021
5.1.5 2,101 11/8/2021
5.1.4 1,294 11/8/2021
5.1.3 341 11/8/2021
5.1.2 328 11/8/2021
5.1.1 1,206 11/8/2021
5.0.14 1,255 11/7/2021
5.0.13 366 11/3/2021
5.0.12 2,124 11/3/2021
5.0.11 707 8/9/2021
5.0.10 325 8/9/2021
5.0.9 351 8/9/2021
5.0.8 39,219 3/24/2021
5.0.7 389 3/9/2021
1.0.7 611 9/3/2020
1.0.6 453 9/3/2020
1.0.5 627 8/2/2020
1.0.4 517 8/2/2020
1.0.3 573 6/17/2020