https://github.com/tyrrrz/quickjson
Simple JSON parser in a source-only package
https://github.com/tyrrrz/quickjson
dotnet dotnet-core dotnet-standard json parser recursive-descent-parser source-only
Last synced: 3 months ago
JSON representation
Simple JSON parser in a source-only package
- Host: GitHub
- URL: https://github.com/tyrrrz/quickjson
- Owner: Tyrrrz
- License: mit
- Created: 2021-06-25T20:44:31.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-20T15:00:34.000Z (11 months ago)
- Last Synced: 2024-05-20T17:28:12.009Z (11 months ago)
- Topics: dotnet, dotnet-core, dotnet-standard, json, parser, recursive-descent-parser, source-only
- Language: C#
- Homepage:
- Size: 104 KB
- Stars: 14
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: License.txt
Awesome Lists containing this project
README
# QuickJson
[](https://github.com/Tyrrrz/.github/blob/master/docs/project-status.md)
[](https://tyrrrz.me/ukraine)
[](https://github.com/Tyrrrz/QuickJson/actions)
[](https://codecov.io/gh/Tyrrrz/QuickJson)
[](https://nuget.org/packages/QuickJson)
[](https://nuget.org/packages/QuickJson)
[](https://discord.gg/2SUWKFnHSm)
[](https://twitter.com/tyrrrz/status/1495972128977571848)
Development of this project is entirely funded by the community. Consider donating to support!
**QuickJson** is a very basic JSON parser, distributed as a source-only package that can be referenced without imposing any run-time dependencies.
## Terms of use[[?]](https://github.com/Tyrrrz/.github/blob/master/docs/why-so-political.md)
By using this project or its source code, for any purpose and in any shape or form, you grant your **implicit agreement** to all the following statements:
- You **condemn Russia and its military aggression against Ukraine**
- You **recognize that Russia is an occupant that unlawfully invaded a sovereign state**
- You **support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas**
- You **reject false narratives perpetuated by Russian state propaganda**To learn more about the war and how you can help, [click here](https://tyrrrz.me/ukraine). Glory to Ukraine! πΊπ¦
## Install
- π¦ [NuGet](https://nuget.org/packages/QuickJson): `dotnet add package QuickJson`
> **Warning**:
> To use this package, your project needs to target C# 10 or later.
> You can ensure this by setting `latest` in the project file.## Usage
To parse a JSON string, call `Json.TryParse(...)` or `Json.Parse(...)`:
```csharp
// This returns null on invalid JSON
var json = Json.TryParse(
"""
{
"foo": [
69,
true,
"bar"
]
}
"""
);// This throws on invalid JSON
var json = Json.Parse("...");
```To retrieve a nested node in the parsed JSON, use `[Try]GetChild(...)`:
```csharp
// May return null if the property doesn't exist
// or if the referenced node is not an object
var foo = json.TryGetChild("foo");// May return null if the child doesn't exist
// or if the referenced node is not an array
var child1 = foo?.TryGetChild(0);
var child2 = foo?.TryGetChild(1);
var child3 = foo?.TryGetChild(2);
```Alternatively, you can also enumerate all object properties using `EnumerateProperties()` or all array children using `EnumerateChildren()`:
```csharp
// Returns an empty enumerator if the referenced node is not an object
foreach (var prop in json.EnumerateProperties())
{
var name = prop.Name; // string
var value = prop.Value; // JsonNode// ...
}// Returns an empty enumerator if the referenced node is not an array
foreach (var child in json.EnumerateChildren())
{
// ...
}
```In order to extract values from nodes, use `[Try]GetNumber()`, `[Try]GetBool()`, or `[Try]GetString()`:
```csharp
// May return null if the node contains a value of a different kind
var value1 = child1?.TryGetNumber(); // double
var value2 = child2?.TryGetBool(); // bool
var value3 = child3?.TryGetString(); // string
```