Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding simdjson example #516

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ CPMAddPackage(
)
```

### [simdjson](https://github.com/simdjson/simdjson)

```
CPMAddPackage("gh:simdjson/[email protected]")
```

lemire marked this conversation as resolved.
Show resolved Hide resolved
### [Boost ](https://github.com/boostorg/boost)

```CMake
Expand Down
14 changes: 14 additions & 0 deletions examples/simdjson/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(CPMSIMDJSONExample)

# ---- Dependencies ----

include(../../cmake/CPM.cmake)
CPMAddPackage("gh:simdjson/[email protected]")

# ---- Executable ----

add_executable(CPMSIMDJSONExample main.cpp)
target_compile_features(CPMSIMDJSONExample PRIVATE cxx_std_17)
target_link_libraries(CPMSIMDJSONExample simdjson::simdjson)
30 changes: 30 additions & 0 deletions examples/simdjson/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "simdjson.h"
#include <iostream>
using namespace simdjson;

int main() {
ondemand::parser parser;
auto cars_json = R"( [
{ "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
{ "make": "Kia", "model": "Soul", "year": 2012, "tire_pressure": [ 30.1, 31.0, 28.6, 28.7 ] },
{ "make": "Toyota", "model": "Tercel", "year": 1999, "tire_pressure": [ 29.8, 30.0, 30.2, 30.5 ] }
] )"_padded;

// Iterating through an array of objects
for (ondemand::object car : parser.iterate(cars_json)) {
// Accessing a field by name
std::cout << "Make/Model: " << std::string_view(car["make"]) << "/" << std::string_view(car["model"]) << std::endl;

// Casting a JSON element to an integer
uint64_t year = car["year"];
std::cout << "- This car is " << 2020 - year << " years old." << std::endl;

// Iterating through an array of floats
double total_tire_pressure = 0;
for (double tire_pressure : car["tire_pressure"]) {
total_tire_pressure += tire_pressure;
}
std::cout << "- Average tire pressure: " << (total_tire_pressure / 4) << std::endl;
}

}