A lightweight, fully-featured JSON parser for C# using Sprache.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE
.vs/config
Dependencies
SpracheJSON
SpracheJSONTest
.gitattributes
.gitignore
.travis.yml
LICENSE.md
README.md
SpracheJSON.sln

README.md

SpracheJSON Travis NuGet Codacy grade

SpracheJSON is a JSON parser for C# written with the awesome Sprache framework. I built this in much the same vain as SpracheDown, more as a side project than anything. However, SpracheJSON is a fully-functioning JSON parser (as far as I'm aware), and as such you can use it for any of your JSON parsing needs.

NuGet

Package Manager:

Install-Package SpracheJSON -Version 1.0.0

.NET CLI:

dotnet add package SpracheJSON --version 1.0.0

You can visit the page on the NuGet Gallery too.

Documentation

I have a full, MSDN-style documentation of the library on the GitHub wiki for this project; all public members of the library are documented there! In future I hope to get some examples and tutorials there.

Quick Tutorial

SpracheJSON can parse JSON from a string or from a file. It parses the JSON into a very simple syntax tree. Imagine you have the following JSON file in "MyFile.json":

{
	"Field1": "Hello, world!",
	"Field2": [
		{
			"Name": "John",
			"Age": 23
		},
		{
			"Name": "Bob",
			"Age": 46
		}
	]
}

To parse that into JSON, you can read it and parse it like so:

var reader = new StreamReader("MyFile.json");
var Parsed = JSON.Parse(reader.ReadToEnd());

From here, you have a JSONObject called Parsed that you can work with. Getting values from the syntax tree is pretty easy:

Console.WriteLine(Parsed["Field1"]);
//Writes 'Hello, World!'

var name = Parsed["Field2"][1]["name"]; //Gets a JSONLiteral object for the name
var age = Parsed["Field2"][1]["age"]; //Gets a JSONLiteral object for the age
Console.WriteLine("The age of {0} is {1}", name, age);
//Writes 'The age of Bob is 46'

Now, let's say that we've got the following class Person:

public class Person
{
	public string Name { get; set; }
	public int Age { get; set; }
}

SpraceJSON can then map our parsed JSON document onto an array of Person objects:

var people = JSON.Map<Person[]>(Parsed["Field2"]);

Any JSONValue object can write itself back into JSON, so you only need the following to write Parsed back to MyFile.json:

var writer = new StreamWriter("MyFile.json");
writer.Write(Parsed.ToJSON());

SpracheJSON also allows you to serialize any object into JSON. Suppose we wanted to write people into its own file:

var writer2 = new StreamWriter("MyOtherFile.json");
writer2.Write(JSON.Write(people));

It should be noted that the object mapping shown above is kinda beta right now (it's in need of error handling). It's being tested and worked on, but if you want to contribute, you definitely should!

Contributing

Comments/Issues/Pull Requests are all very welcome any time!