Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 2.69 KB

README.md

File metadata and controls

44 lines (31 loc) · 2.69 KB

jdai examples

The same tasks from C++, Python and C: documents, numeric arrays, and records.

C++: neurojson::jdata

Small programs written with jdai's nlohmann/json-style interface. CMake builds them with the library (JDAI_BUILD_EXAMPLES, on by default), and ctest runs them with the tests.

example shows
cpp/hello_jdata.cpp a document built from initializer lists; members, items and conversions; JSON text, BJData bytes and the round trip; JSON-Mmap paths and JSON Pointers; errors
cpp/arrays.cpp typed N-D arrays: matrices from nested braces (matrix), a volume of zeros (zeros) filled through a view (nd<float>()), std::vector in and out; saving with zstd, byte shuffling and N-D tiles; reading back the whole file or only some members
cpp/user_types.cpp your own types: JDAI_DEFINE_TYPE, or to_jdata/from_jdata by hand; a list of structs as records (a structure-of-arrays in BJData); standard containers

Python: the jdai module

The same ground in Python (numpy arrays in and out). ctest runs them when the module is built (-DJDAI_BUILD_PYTHON=ON); by hand: python hello_jdai.py with the module installed (pip install .) or on PYTHONPATH.

example shows
python/hello_jdai.py a document as plain Python objects; JSON text and BJData bytes; a .pmat file whose members are read alone (keys=, JSON-Mmap paths) and updated in place; errors
python/arrays.py numpy arrays of any type and order; zstd, byte shuffling and N-D tiles; a slice, a line and a block read without the rest (load_region), a block written back in place (update_region); zero-copy views of an uncompressed file
python/tables.py a list of dicts as a structure of arrays, read back as records or columns; repeated strings as an enumeration; bytes, complex arrays, Decimal and BJData extensions

C: JData

example shows
c/hello_jdata.c the C interface as one struct: a document built with JData.object(), JData.set, JData.matrix and JData.ndarray; values read by member name, JSON-Mmap path or JSON Pointer; JSON text; a compressed .pmat file and back; errors. Built as C99 and as C11 (where JDAI_VAL(x) makes a value of x's type)

To build a C++ example by hand against an installed jdai:

c++ -std=c++11 -I/path/to/jdai/include hello_jdata.cpp /path/to/libjdai.a -lpthread -o hello_jdata

and a C one (-lstdc++: the library is C++):

cc -std=c99 -I/path/to/jdai/include hello_jdata.c /path/to/libjdai.a -lstdc++ -lpthread -lm -o hello_jdata