pybind/pybind11_json

Support for binary arrays

vlad-nn opened this issue · 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
    {
    ...

Thanks for opening an issue!

Would your proposed fix make NumPy a hard dependency?

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.

Cool :)

Would you be willing to open a PR?