Philiprehberger.SafeJson 0.2.0

dotnet add package Philiprehberger.SafeJson --version 0.2.0
                    
NuGet\Install-Package Philiprehberger.SafeJson -Version 0.2.0
                    
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="Philiprehberger.SafeJson" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.SafeJson" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.SafeJson" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.SafeJson --version 0.2.0
                    
#r "nuget: Philiprehberger.SafeJson, 0.2.0"
                    
#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.
#:package Philiprehberger.SafeJson@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.SafeJson&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.SafeJson&version=0.2.0
                    
Install as a Cake Tool

Philiprehberger.SafeJson

CI NuGet Last updated

Resilient JSON parsing with fallback defaults and path-based value extraction that never throws.

Installation

dotnet add package Philiprehberger.SafeJson

Usage

Parsing and Extracting Values

using Philiprehberger.SafeJson;

var node = SafeJson.Parse("""{"user": {"name": "Alice", "age": 30}}""");

string name = node.GetString("user.name", "unknown"); // "Alice"
int age = node.GetInt("user.age", 0);                 // 30
bool active = node.GetBool("user.active", false);     // false (missing → default)
double score = node.GetDouble("user.score", 0.0);     // 0.0 (missing → default)

Safe Handling of Invalid JSON

using Philiprehberger.SafeJson;

// Invalid JSON returns an empty node — never throws
var empty = SafeJson.Parse("not valid json");
string safe = empty.GetString("anything", "default"); // "default"
bool has = empty.Has("anything");                     // false

DateTime and GUID Parsing

using Philiprehberger.SafeJson;

var json = SafeJson.Parse("""{"event": {"date": "2026-04-05T10:30:00Z", "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"}}""");

DateTime date = json.GetDateTime("event.date", DateTime.MinValue); // 2026-04-05T10:30:00Z
Guid id = json.GetGuid("event.id", Guid.Empty);                   // a1b2c3d4-e5f6-7890-abcd-ef1234567890

// Missing or invalid values return the default — never throws
DateTime missing = json.GetDateTime("event.nope", DateTime.MinValue); // DateTime.MinValue
Guid invalid = json.GetGuid("event.date", Guid.Empty);               // Guid.Empty

Array Access and Path Navigation

using Philiprehberger.SafeJson;

var json = SafeJson.Parse("""{"items": [{"id": 1}, {"id": 2}]}""");

int id = json.GetInt("items[0].id", 0);       // 1
SafeJsonNode[] items = json.GetArray("items"); // 2 elements
bool exists = json.Has("items[1].id");         // true

API

SafeJson

Method Description
Parse(string json) Parse JSON string into a SafeJsonNode. Never throws.

SafeJsonNode

Method Description
GetString(path, defaultValue) Extract a string value at the given path.
GetInt(path, defaultValue) Extract an int value at the given path.
GetLong(path, defaultValue) Extract a long value at the given path.
GetDouble(path, defaultValue) Extract a double value at the given path.
GetBool(path, defaultValue) Extract a bool value at the given path.
GetDecimal(path, defaultValue) Extract a decimal value at the given path.
GetDateTime(path, defaultValue) Parse an ISO 8601 date string at the given path.
GetGuid(path, defaultValue) Parse a GUID string at the given path.
GetArray(path) Extract an array of SafeJsonNode at the given path.
Has(path) Check whether a value exists at the given path.
this[string key] Access a child node by key.
this[int index] Access a child node by array index.

Paths support dot notation (user.address.city) and array indexing (items[0].name).

Development

dotnet build src/Philiprehberger.SafeJson.csproj --configuration Release

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 126 4/5/2026
0.1.2 117 4/1/2026
0.1.1 105 3/25/2026
0.1.0 112 3/23/2026