pybind11_json icon indicating copy to clipboard operation
pybind11_json copied to clipboard

Support for binary arrays

Open vlad-nn opened this issue 4 years ago • 3 comments

NLohmann JSON version 3.10.4 has binary arrays: https://json.nlohmann.me/api/basic_json/binary/ If such array is encountered in JSON object, pyjson::from_json() crashes with stack overflow because it falls to "else // Object" clause and then the for() loop tries to iterate over non-existing values, since binary array is not an object.

Proposed fix:

  1. Add include file: #include "pybind11/numpy.h"

  2. In pyjson::from_json() add: ... else if (j.is_binary()) { const auto &bin = j.get_binary(); return py::array( bin.size(), bin.data() ); } else // Object { ...

vlad-nn avatar Nov 10 '21 03:11 vlad-nn

Thanks for opening an issue!

Would your proposed fix make NumPy a hard dependency?

martinRenou avatar Nov 10 '21 07:11 martinRenou

IMHO, not. See, pybind11/numpy.h header is a part of pybind11 distro, and since pybind11 is header-only library, it does not create any extra dependency. pybind11/numpy.h does not have any pragma linker directives, so I would assume no extra linking.

vlad-nn avatar Nov 10 '21 18:11 vlad-nn

Cool :)

Would you be willing to open a PR?

martinRenou avatar Nov 12 '21 08:11 martinRenou