Repository navigation
JsonHelper
JsonHelper adds small, type-safe helpers around System.Text.Json.JsonElement.
It is especially useful when reading optional properties where a missing or mismatched value should fall back to a default.
using System.Text.Json;
using var document =
JsonDocument.Parse(json);
JsonElement root =
document.RootElement;
string name =
root.GetPropertyOrDefault(
"name",
"Unknown");
int health =
root.GetPropertyOrDefault(
"health",
1);
bool boss =
root.GetPropertyOrDefault(
"boss",
false);Missing values return the supplied default:
int mana =
root.GetPropertyOrDefault(
"mana",
0);Use GetElementOrDefault when you already have the element itself:
JsonElement value =
root.GetProperty("health");
int health =
value.GetElementOrDefault(0);The helper currently supports common primitives including:
stringbool- signed/unsigned integer types
floatdoubledecimalDateTimeDateTimeOffsetGuid- enums
Unsupported generic types throw an ArgumentException rather than silently guessing how to deserialize them.
For larger object graphs, use normal System.Text.Json serialization/deserialization.
For JSON like:
{
"difficulty": "Hard"
}you can use:
Difficulty difficulty =
root.GetPropertyOrDefault(
"difficulty",
Difficulty.Normal);Numeric enum values are also accepted when the value is defined by the enum.
VOID also uses JsonHelper internally while converting LDtk field instances into its typed LDtk setting objects.
The LDtk-specific conversion methods are internal; normal game code usually accesses the resulting settings through the public LDtk APIs instead.
See LDtk Settings.
Home · Getting Started · Rendering · Custom Renderers · GitHub · Report an Issue
Built with VOID Engine · MIT License
- Coroutines
- State Machine
- Save System
- Pathfinding
- Logging
- Beacon Manager
- Beacon Event System
- Discoverable System
- Instance Helper
- BeaconManager Extensions
- BeaconHandle Extensions
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions