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" />
<PackageReference Include="Philiprehberger.SafeJson" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=Philiprehberger.SafeJson&version=0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Philiprehberger.SafeJson
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:
License
| Product | Versions 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.