Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zanders3/json
A really simple C# JSON Parser in 350 lines
https://github.com/zanders3/json
json-parser low-memory nuget unity-3d
Last synced: 6 days ago
JSON representation
A really simple C# JSON Parser in 350 lines
- Host: GitHub
- URL: https://github.com/zanders3/json
- Owner: zanders3
- License: mit
- Created: 2015-09-26T22:03:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-29T22:33:47.000Z (almost 2 years ago)
- Last Synced: 2025-02-03T05:31:45.338Z (6 days ago)
- Topics: json-parser, low-memory, nuget, unity-3d
- Language: C#
- Homepage:
- Size: 77.1 KB
- Stars: 328
- Watchers: 19
- Forks: 89
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tiny Json
[![Build Status](https://travis-ci.org/zanders3/json.png?branch=master)](https://travis-ci.org/zanders3/json)
[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/zanders3/json/master/LICENSE)
[![NuGet](https://img.shields.io/nuget/v/TinyJson.svg)](https://www.nuget.org/packages/TinyJson)A really simple C# JSON parser in ~350 lines
- Attempts to parse JSON files with minimal GC allocation
- Nice and simple `"[1,2,3]".FromJson>()` API
- Classes and structs can be parsed too!
```csharp
class Foo
{
public int Value;
}
"{\"Value\":10}".FromJson()
```
- Anonymous JSON is parsed into `Dictionary` and `List`
```csharp
var test = "{\"Value\":10}".FromJson();
int number = ((Dictionary)test)["Value"];
```
- No JIT Emit support to support AOT compilation on iOS
- Attempts are made to NOT throw an exception if the JSON is corrupted or invalid: returns null instead.
- Only public fields and property setters on classes/structs will be written to
- You can *optionally* use `[IgnoreDataMember]` and `[DataMember(Name="Foo")]` to ignore vars and override the default nameLimitations:
- No JIT Emit support to parse structures quickly
- Limited to parsing <2GB JSON files (due to int.MaxValue)
- Parsing of abstract classes or interfaces is NOT supported and will throw an exception.## Changelog
- v1.1 Support added for Enums and fixed Unity compilation
- v1.0 Initial Release## Example Usage
This example will write a list of ints to a File and read it back again:
```csharp
using System;
using System.IO;
using System.Collections.Generic;using TinyJson;
public static class JsonTest
{
public static void Main(string[] args)
{
//Write a file
List values = new List { 1, 2, 3, 4, 5, 6 };
string json = values.ToJson();
File.WriteAllText("test.json", json);
//Read it back
string fileJson = File.ReadAllText("test.json");
List fileValues = fileJson.FromJson>();
}
}
```
Save this as `JsonTest.cs` then compile and run with `mcs JsonTest.cs && mono JsonTest.exe`## Installation
Simply copy and paste the [JSON Parser](https://raw.githubusercontent.com/zanders3/json/master/src/JSONParser.cs) and/or the [JSON Writer](https://raw.githubusercontent.com/zanders3/json/master/src/JSONWriter.cs) into your project. I also provide NuGet but I recommend the copy paste route ;)