Skip to content

A really simple C# JSON Parser in 350 lines

License

Notifications You must be signed in to change notification settings

pdx-niklashedlund/json

This branch is 3 commits ahead of, 2 commits behind zanders3/json:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4d017cc · May 27, 2021

History

77 Commits
May 27, 2021
May 27, 2021
Nov 21, 2015
Dec 3, 2017
Apr 3, 2018
Aug 16, 2018
Apr 22, 2018

Repository files navigation

Tiny Json

Build Status License NuGet

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<List<int>>() API
  • Classes and structs can be parsed too!
class Foo 
{ 
  public int Value;
}
"{\"Value\":10}".FromJson<Foo>()
  • Anonymous JSON is parsed into Dictionary<string,object> and List<object>
var test = "{\"Value\":10}".FromJson<object>();
int number = ((Dictionary<string,object>)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 name

Limitations:

  • 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:

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<int> values = new List<int> { 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<int> fileValues = fileJson.FromJson<List<int>>();
  }
}

Save this as JsonTest.cs then compile and run with mcs JsonTest.cs && mono JsonTest.exe

Installation

Simply copy and paste the JSON Parser and/or the JSON Writer into your project. I also provide NuGet but I recommend the copy paste route ;)

About

A really simple C# JSON Parser in 350 lines

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%