Json Rocket is a fast JSON parser with the goal to extract pieces of information from a JSON message. It is not a fully fledged deserializer that reads JSON into DTO classes.
The goals of this project are:
- ultra fast parsing of a JSON data
- no heap allocations while parsing
- find/extract multiple values from a JSON message with at most one full tokenization
However, non-goals are:
- proper JSON validation: the code takes a lot of shortcuts for faster parsing that can potentially read invalid JSON. It is guaranteed thought that malformed JSON will not cause any Exceptions.
PM> Install-Package JsonRocket
The following JSON snippet can be tokenized like this
{
"Id": 1,
"Name": "Joh Doe",
"Address": {
"Street": "1st Avenue",
"City": "Springfield"
}
}
var json = Encoding.UTF8.GetBytes(""); // insert above snippet here
var tokenizer = new JsonRocket.Tokenizer();
tokenizer.Reset(json);
while (tokenizer.MoveNext())
{
Console.WriteLine(tokenizer.Current);
}
the output will be
ObjectStart
Key
Integer
Key
String
Key
ObjectStart
Key
String
Key
String
ObjectEnd
ObjectEnd
The following snippet extracts the Name (John Doe
) and the City (Springfield
) from the JSON. This happens with a single iteration through the tokenizer.
var trie = new JsonRocket.Trie();
trie.Add("Name");
trie.Add("Address.City");
var extractor = new JsonRocket.Extractor(trie);
var json = Encoding.UTF8.GetBytes(""); // insert above snippet here
var tokenizer = new JsonRocket.Tokenizer();
tokenizer.Reset(json);
var result = new List<JsonRocket.ExtractedValue>();
extractor.ReadFrom(tokenizer, result);
Console.WriteLine(result[0].Value.ReadString()); // prints 'John Doe'
Console.WriteLine(result[1].Value.ReadString()); // prints 'Springfield'