Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 21 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,27 @@ project(
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)

# Add a library using FindPython's tooling (pybind11 also provides a helper like
# this)
python_add_library(_core MODULE cpp_src/jsonparser.cpp WITH_SOABI)
# Define source files for the streaming JSON parser
set(PARSER_SOURCES
cpp_src/jsonparser.cpp
)

# Define header files (for IDE integration)
set(PARSER_HEADERS
cpp_src/jsonparser.h
)

# Add a library using FindPython's tooling
python_add_library(_core MODULE
cpp_src/bindings.cpp
${PARSER_SOURCES}
WITH_SOABI
)

# Make sure the include directory is in the include path
target_include_directories(_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cpp_src)

# Link against pybind11
target_link_libraries(_core PRIVATE pybind11::headers)

# This is passing in the version as a define just as an example
Expand Down
33 changes: 33 additions & 0 deletions cpp_src/bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

/**
* Python bindings for the streaming JSON parser
*/

#include "jsonparser.h"

#include <string>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>


namespace py = pybind11;

// macro defined in pybind11.h (common.h)
PYBIND11_MODULE(_core, m) {
m.doc() = "C++ streaming JSON parser with Python bindings";

// Expose the StreamJsonParser class along with its
// constructor and two functions.
// Note that we use getPython for get to return a py::object
py::class_<StreamingJsonParser>(m, "StreamingJsonParser")
.def(py::init<bool>(), py::arg("strict_mode") = false)
.def("consume", &StreamingJsonParser::consume)
.def("get", &StreamingJsonParser::getPython);

// Expose extra function for parsing without explictly creating obj.
m.def("parse_json", [](const std::string& json_str, bool strict_mode = false) {
StreamingJsonParser parser(strict_mode);
parser.consume(json_str);
return parser.getPython();
}, py::arg("json_str"), py::arg("strict_mode") = false);
}
Loading