mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
Quickstart and intro (#575)
* Add quickstart * Fix amalgamation Co-authored-by: John Keiser <john@johnkeiser.com>
This commit is contained in:
@@ -1,16 +1,45 @@
|
||||
# simdjson : Parsing gigabytes of JSON per second
|
||||
# simdjson : Parsing gigabytes of JSON per second
|
||||
|
||||
<img src="images/logo.png" width="10%" style="float: right">
|
||||
JSON is everywhere on the Internet. Servers spend a *lot* of time parsing it. We need a fresh approach. simdjson uses commonly available SIMD instructions and microparallel algorithms to parse JSON 2.5x faster than anything else out there.
|
||||
|
||||
* **Ludicrous Speed:** Over 2.5x faster than other production-grade JSON parsers.
|
||||
* **Delightfully Easy:** First-class, easy to use API.
|
||||
* **Complete Validation:** Full JSON and UTF-8 validation, with no compromises.
|
||||
* **Rock-Solid Reliability:** From memory allocation to error handling, simdjson's design avoids surprises.
|
||||
|
||||
This library is part of the [Awesome Modern C++](https://awesomecpp.com) list.
|
||||
|
||||
[](https://cloud.drone.io/simdjson/simdjson)
|
||||
[](https://circleci.com/gh/simdjson/simdjson)
|
||||
[](https://ci.appveyor.com/project/lemire/simdjson-jmmti/branch/master)
|
||||
[![][license img]][license]
|
||||
[](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:simdjson)
|
||||
|
||||
## Quick Start
|
||||
|
||||
## A C++ library to see how fast we can parse JSON with complete validation.
|
||||
simdjson is easily consumable with a single .h and .cpp file.
|
||||
|
||||
JSON documents are everywhere on the Internet. Servers spend a lot of time parsing these documents. We want to accelerate the parsing of JSON per se using commonly available SIMD instructions as much as possible while doing full validation (including character encoding). This library is part of the [Awesome Modern C++](https://awesomecpp.com) list.
|
||||
0. Prerequisites: `g++` or `clang++`.
|
||||
1. Pull [simdjson.h](singleheader/simdjson.h) and [simdjson.cpp](singleheader/simdjson.h) into a directory, along with the sample file [twitter.json](jsonexamples/twitter.json).
|
||||
```
|
||||
wget https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.h https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.cpp https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json
|
||||
```
|
||||
2. Create `parser.cpp`:
|
||||
|
||||
<img src="images/logo.png" width="10%">
|
||||
```c++
|
||||
#include "simdjson.h"
|
||||
int main(void) {
|
||||
simdjson::document::parser parser;
|
||||
simdjson::document& tweets = parser.load("twitter.json");
|
||||
std::cout << tweets["search_metadata"]["count"] << " results." << std::endl;
|
||||
}
|
||||
```
|
||||
3. `g++ -o parser parser.cpp` (or clang++)
|
||||
4. `./parser`
|
||||
```
|
||||
100 results.
|
||||
```
|
||||
|
||||
## Real-world usage
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* auto-generated on Thu Mar 5 10:30:07 PST 2020. Do not edit! */
|
||||
/* auto-generated on Fri Mar 20 11:47:31 PDT 2020. Do not edit! */
|
||||
|
||||
#include <iostream>
|
||||
#include "simdjson.h"
|
||||
@@ -8,37 +8,31 @@ int main(int argc, char *argv[]) {
|
||||
std::cerr << "Please specify at least one file name. " << std::endl;
|
||||
}
|
||||
const char * filename = argv[1];
|
||||
simdjson::padded_string p = simdjson::get_corpus(filename);
|
||||
auto [doc, error] = simdjson::document::parse(p); // do the parsing
|
||||
simdjson::document::parser parser;
|
||||
auto [doc, error] = parser.load(filename); // do the parsing
|
||||
if (error) {
|
||||
std::cout << "document::parse failed" << std::endl;
|
||||
std::cout << "parse failed" << std::endl;
|
||||
std::cout << "error code: " << error << std::endl;
|
||||
std::cout << error << std::endl;
|
||||
} else {
|
||||
std::cout << "document::parse valid" << std::endl;
|
||||
std::cout << "parse valid" << std::endl;
|
||||
}
|
||||
if(argc == 2) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
//JsonStream
|
||||
// parse_many
|
||||
const char * filename2 = argv[2];
|
||||
simdjson::padded_string p2 = simdjson::get_corpus(filename2);
|
||||
simdjson::document::parser parser;
|
||||
simdjson::JsonStream js{p2};
|
||||
int parse_res = simdjson::SUCCESS_AND_HAS_MORE;
|
||||
|
||||
while (parse_res == simdjson::SUCCESS_AND_HAS_MORE) {
|
||||
parse_res = js.json_parse(parser);
|
||||
for (auto result : parser.load_many(filename2)) {
|
||||
error = result.error();
|
||||
}
|
||||
|
||||
if( ! parser.is_valid()) {
|
||||
std::cout << "JsonStream not valid" << std::endl;
|
||||
if (error) {
|
||||
std::cout << "parse_many failed" << std::endl;
|
||||
std::cout << "error code: " << error << std::endl;
|
||||
std::cout << error << std::endl;
|
||||
} else {
|
||||
std::cout << "JsonStream valid" << std::endl;
|
||||
std::cout << "parse_many valid" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
+5220
-2384
File diff suppressed because it is too large
Load Diff
+2321
-1021
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user