Bnaya.Extensions.Json 5.1.154

There is a newer version of this package available.
See the version list below for details.
dotnet add package Bnaya.Extensions.Json --version 5.1.154                
NuGet\Install-Package Bnaya.Extensions.Json -Version 5.1.154                
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="Bnaya.Extensions.Json" Version="5.1.154" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bnaya.Extensions.Json --version 5.1.154                
#r "nuget: Bnaya.Extensions.Json, 5.1.154"                
#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 Bnaya.Extensions.Json as a Cake Addin
#addin nuget:?package=Bnaya.Extensions.Json&version=5.1.154

// Install Bnaya.Extensions.Json as a Cake Tool
#tool nuget:?package=Bnaya.Extensions.Json&version=5.1.154                

Bnaya Json Extensions

Build & Deploy NuGet

NuGet

Functionality of this library includes:


ToEnumerable

Enumerate over json elements.

Useful for fetching selected parts of the json

Path based enumeration

Rely on path convention:

json.ToEnumerable(/path convention/);

JSON:

{
 "friends": [
   {
     "name": "Yaron",    
     "id": 1
   },
   {
     "name": "Aviad",   
     "id": 2
   }
 ]
}

CODE:

var items = source.ToEnumerable("friends.[].name");

OR

var items = source.ToEnumerable("friends.*.name");

RESULT

["Yaron", "Aviad"]

CODE:

var items = source.ToEnumerable("");

OR

var items = source.ToEnumerable("");

RESULT

[

CODE:

var items = source.ToEnumerable("friends.[0].name");

RESULT

["Yaron"]

CODE:

var items = source.ToEnumerable("friends.[0].*");

OR

var items = source.ToEnumerable("");

RESULT

["Yaron",1]

See: YieldWhen_Path_Test in the source code

Yield with predicate

Yield element according to predicate and onMatch delegates.

JSON:

{
    "projects": [ {
        "key": "cloud-d",
        "duration": 8
    }, {
    "users": [{
        "rank": {
            "job": 10,
            "relationship": {
                "projects": [
                    { "key": "cloud-d", "team": 5 },
                    { "key": "cloud-x", "team": 32 }
                ]
            }
        },
    }]
}

Sample:

using static System.Text.Json.TraverseFlowInstruction;

var items = source.ToEnumerable((json, deep, breadcrumbs) =>
{
    if(breadcrumbs.Count < 4)
        return Drill;

    if (breadcrumbs[^4] == "relationship" &&
        breadcrumbs[^3] == "projects" &&
        breadcrumbs[^1] == "key")
    {
        //return Drill;
        return Yield;
    }

    return Drill;
});

// "cloud-d", "cloud-x"
var results = items.Select(m => m.GetString()).ToArray();

Filter

Reduce (or modify) the json

JSON:

{
  "A": 10,
  "B": [
    { "Val": 40 },
    { "Val": 20 },
    { "Factor": 20 }
  ],
  "C": [0, 25, 50, 100 ],
  "Note": "Re-shape json"
}

Sample:

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

Keep

Path based Filter

JSON:

{
  "A": 10,
  "B": [
    { "Val": 40 },
    { "Val": 20 },
    { "Factor": 20 }
  ],
  "C": [0, 25, 50, 100 ],
  "Note": "Re-shape json"
}

Sample 1:

var target = source.Keep("B.*.val");

RESULT

{"B":[{"Val":40},{"Val":20}]}

Sample 2:

var target = source.Keep("B.[]");

RESULT

{"B":[{"Val":40},{"Val":20},{"Factor":20}]}

Sample 3:

var target = source.Keep("B.[].Factor");

RESULT

{"B":[{"Factor":20}]}

Sample 4:

var target = source.Keep("B.[1].val");

RESULT

{"B":[{"Val":20}]}

Remove

Remove elements from the json.

{
  "A": 10,
  "B": [
    { "Val": 40 },
    { "Val": 20 },
    { "Factor": 20 }
  ],
  "C": [0, 25, 50, 100 ],
  "Note": "Re-shape json"
}

Sample 1:

var target = source.Remove("");

RESULT


Sample 2:

var target = source.Remove("B.[]");

RESULT

{"A":10,"B":[],"C":[],"Note":"Re-shape json"}

Sample 3:

var target = source.Remove("B.*.val");

RESULT

{"A":10,"B":[{"Factor":20}],"C":[0,25,50,100],"Note":"Re-shape json"}

Sample 4:

var target = source.Remove("B.[1]");

RESULT

{"A":10,"B":[{"Val":40},{"Factor":20}],"C":[0,50,100],"Note":"Re-shape json"}

TryAddProperty

Try to add property if missing.

Sample 1

{ "A": 0, "B": 0 }
source.RootElement.TryAddProperty("C", 1);

Result in:

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

Sample 2

{ "A": 0, "B": 0, "C": 0 }
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

Sample 3

Ignored null

var options = new JsonPropertyModificatonOpions
{
    IgnoreNull = false
};
var source = JsonDocument.Parse(json);
source.RootElement.TryAddProperty("C", 1);

Result in:

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

Sample 4

Changing property within a path

{
  "X": {
    "Y": {
    	"A": 0,
    	"B": 0
    }
  },
  "Y": { "Q": 2 }
}
source.RootElement.TryAddProperty("X.Y", "C", 1);

Result in:

{
  "X": {
      "Y": {
          "A": 0,
          "B": 0,  
          "C": 1
      }
  },
  "Y": { "Q": 2 }
}

TryGetValue

Try to get a value within a path

{
  "B": {
    "B1": ["Very", "Cool"],
    "B2": {
        "B21": {
          "B211": "OK"
        },
        "B22": 22,   
        "B23": true,
        "B24": 1.8,
        "B25": "2007-09-01T10:35:01",
        "B26": "2007-09-01T10:35:01+02"
    }
  }
}

Sample 1

source.TryGetValue(out JsonElement value, "B.B1")

Result in:

["Very","Cool"]

Sample 2

source.TryGetValue(out JsonElement value, "B.B1.*")

Result in:

Very

TryGet...

Try to get a value within a path.

{
  "B": {
    "B1": ["Very", "Cool"],
    "B2": {
        "B21": {
          "B211": "OK"
        },
        "B22": 22,   
        "B23": true,
        "B24": 1.8,
        "B25": "2007-09-01T10:35:01",
        "B26": "2007-09-01T10:35:01+02"
    }
  }
}

String

source.TryGetString(out JsonElement value, "B.B2.*.B211")

Result in: OK

DateTiemOffset

source.TryGetValue(out JsonElement value, "B.*.B26")

Result in: 2007-09-01T10:35:01+02

Double

source.TryGetNumber(out double value, "B.B2.B24")

Result in: 1.8


Merge

Merging 2 or more json. The last json will override previous on conflicts

Sample 1

Source: { "A": 1 } , Merged: { "B": 2 }

var target = source.Merge(merged);

Result in: {"A":1,"b":2}

Sample 2

Source: { "A": 1 } , Merged: {"B":2,"C":3}

var target = source.Merge(merged);

Result in: { "A":1, "B":2, "C":3 }

Sample 3

Source: { "A": 1 } , Merged1: {"B":2} , Merged2: {"C":3}

var target = source.Merge(merged1, merged2);

Result in: { "A":1, "B":2, "C":3 }

Sample 4

Source: { "A": 1 }

var target = source.MergeObject(new { b = 2 });

Result in: {"A":1, "B":2}

Merge Into

Merging json into specific path of a source json.

The last json will override any previous one in case of a conflicts

Source

{ "A": 1, "B": { "B1":[1, 2, 3] } }

Sample 1

Source

{ "C": { "D": { "X":1, "Y":2 } } }

Merged

{ "Z": 3 }
var target = source.MergeInto("C.D", merged);

Result in: { "C": { "D": { "X":1, "Y":2, "Z": 3 } } }

Sample 2

Merged

{ 5 }
var target = source.MergeInto("B.B1.[1]", merged);

Result in: { "A": 1, "B": { "B1":[1, 5, 3] } }

Sample 3

Source

{ "C": { "D": { "X":1, "Y":2 } } }

Merged

{  "Z": 3 }
var target = source.MergeInto("C.D", merged);

Result in: { "C": { "D": { "X":1, "Y":2, "Z": 3 } } }

Sample 4

var target = source.MergeInto("B.B1.[1]", 5);

Result in: { "A": 1, "B": { "B1":[1, 5, 3] } }

Sample 5

var merged = new { New = "Object"}; // anonymous type
var target = source.MergeInto("B.B1.[1]", merged);

Result in: { "A": 1, "B": { "B1":[1, {"New":"Object"}, 3] }

Sample 6

Source

{ "C": { "D": { "X":1, "Y":2 } } }
var target = source.MergeInto("C.D", new { Z = 3 });

Result in: { "C": { "D": { "X":1, "Y":2, "Z": 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

var json = JsonDocument.Parse(JSON);
var srm = json.ToStream() as MemoryStream;
string result = Encoding.UTF8.GetString(srm.ToArray()) ;
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.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Bnaya.Extensions.Json:

Package Downloads
EventSourcing.Backbone.Abstractions

Package Description

EventSourcing.Backbone.Channels.S3StoreProvider.Common

Package Description

EventSourcing.Backbone.Channels.RedisProvider.Common

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.1.162 204 9/19/2023
5.1.161 357 9/14/2023
5.1.160 172 9/14/2023
5.1.159 14,980 5/17/2023
5.1.158 174 5/17/2023
5.1.156 163 5/17/2023
5.1.155 166 5/17/2023
5.1.154 164 5/17/2023
5.1.153 184 5/11/2023
5.1.152 180 5/11/2023
5.1.151 160 5/11/2023