Skip to content

Commit

Permalink
added missing files and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Feb 13, 2024
1 parent 475da9b commit 1f28a11
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 3 deletions.
10 changes: 10 additions & 0 deletions include/bedrock/ABTioManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ class ABTioManager {
* @param description ABT-IO instance description.
*
* @return the NamedDependency handle for the newly-created instance.
*
* Example of JSON description:
*
* ```json
* {
* "name": "my_abt_io_instance",
* "pool": "my_pool",
* "config": {}
* }
* ```
*/
std::shared_ptr<NamedDependency>
addABTioInstanceFromJSON(const json& description);
Expand Down
1 change: 1 addition & 0 deletions include/bedrock/ClientManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <bedrock/AbstractServiceFactory.hpp>
#include <bedrock/ClientDescriptor.hpp>
#include <bedrock/NamedDependency.hpp>
#include <nlohmann/json.hpp>
#include <string>
#include <memory>

Expand Down
23 changes: 20 additions & 3 deletions include/bedrock/SSGManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,29 @@ class SSGManager {
/**
* @brief Create a group from a JSON configuration.
*
* @param config JSON-formatted configuration.
* @param description JSON description of the group.
*
* @return The created group id.
* @return The created group as a NamedDependency.
*
* Example of JSON description:
*
* ```json
* {
* "name": "my_ssg_group",
* "pool": "my_pool",
* "credential": 1234,
* "group_file": "/path/to/group/file.ssg",
* "bootstrap": "init|join",
* "swim": {
* "period_length_ms": 500,
* "suspect_timeout_periods": 3,
* "subgroup_member_count": 5
* }
* }
* ```
*/
std::shared_ptr<NamedDependency>
addGroupFromJSON(const json& config);
addGroupFromJSON(const json& description);

/**
* @brief Create a group and add it to the SSG context.
Expand Down
29 changes: 29 additions & 0 deletions python/src/pybind11_json/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2019,
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
224 changes: 224 additions & 0 deletions python/src/pybind11_json/pybind11_json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/***************************************************************************
* Copyright (c) 2019, Martin Renou *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef PYBIND11_JSON_HPP
#define PYBIND11_JSON_HPP

#include <string>
#include <vector>

#include "nlohmann/json.hpp"

#include "pybind11/pybind11.h"

namespace py = pybind11;
namespace nl = nlohmann;

namespace pyjson
{
inline py::object from_json(const nl::json& j)
{
if (j.is_null())
{
return py::none();
}
else if (j.is_boolean())
{
return py::bool_(j.get<bool>());
}
else if (j.is_number_unsigned())
{
return py::int_(j.get<nl::json::number_unsigned_t>());
}
else if (j.is_number_integer())
{
return py::int_(j.get<nl::json::number_integer_t>());
}
else if (j.is_number_float())
{
return py::float_(j.get<double>());
}
else if (j.is_string())
{
return py::str(j.get<std::string>());
}
else if (j.is_array())
{
py::list obj(j.size());
for (std::size_t i = 0; i < j.size(); i++)
{
obj[i] = from_json(j[i]);
}
return std::move(obj);
}
else // Object
{
py::dict obj;
for (nl::json::const_iterator it = j.cbegin(); it != j.cend(); ++it)
{
obj[py::str(it.key())] = from_json(it.value());
}
return std::move(obj);
}
}

inline nl::json to_json(const py::handle& obj)
{
if (obj.ptr() == nullptr || obj.is_none())
{
return nullptr;

Check warning on line 74 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L74

Added line #L74 was not covered by tests
}
if (py::isinstance<py::bool_>(obj))
{
return obj.cast<bool>();

Check warning on line 78 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L78

Added line #L78 was not covered by tests
}
if (py::isinstance<py::int_>(obj))
{
try
{
nl::json::number_integer_t s = obj.cast<nl::json::number_integer_t>();

Check warning on line 84 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L84

Added line #L84 was not covered by tests
if (py::int_(s).equal(obj))
{
return s;

Check warning on line 87 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L87

Added line #L87 was not covered by tests
}
}
catch (...)

Check warning on line 90 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L90

Added line #L90 was not covered by tests
{
}
try
{
nl::json::number_unsigned_t u = obj.cast<nl::json::number_unsigned_t>();

Check warning on line 95 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L95

Added line #L95 was not covered by tests
if (py::int_(u).equal(obj))
{
return u;

Check warning on line 98 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L98

Added line #L98 was not covered by tests
}
}
catch (...)

Check warning on line 101 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L101

Added line #L101 was not covered by tests
{
}
throw std::runtime_error("to_json received an integer out of range for both nl::json::number_integer_t and nl::json::number_unsigned_t type: " + py::repr(obj).cast<std::string>());

Check warning on line 104 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L104

Added line #L104 was not covered by tests
}
if (py::isinstance<py::float_>(obj))
{
return obj.cast<double>();

Check warning on line 108 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L108

Added line #L108 was not covered by tests
}
if (py::isinstance<py::bytes>(obj))
{
py::module base64 = py::module::import("base64");
return base64.attr("b64encode")(obj).attr("decode")("utf-8").cast<std::string>();

Check warning on line 113 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L112-L113

Added lines #L112 - L113 were not covered by tests
}
if (py::isinstance<py::str>(obj))
{
return obj.cast<std::string>();

Check warning on line 117 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L117

Added line #L117 was not covered by tests
}
if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj))
{
auto out = nl::json::array();

Check warning on line 121 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L121

Added line #L121 was not covered by tests
for (const py::handle value : obj)
{
out.push_back(to_json(value));

Check warning on line 124 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L124

Added line #L124 was not covered by tests
}
return out;

Check warning on line 126 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L126

Added line #L126 was not covered by tests
}
if (py::isinstance<py::dict>(obj))
{
auto out = nl::json::object();
for (const py::handle key : obj)
{
out[py::str(key).cast<std::string>()] = to_json(obj[key]);

Check warning on line 133 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L133

Added line #L133 was not covered by tests
}
return out;
}
throw std::runtime_error("to_json not implemented for this type of object: " + py::repr(obj).cast<std::string>());

Check warning on line 137 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L137

Added line #L137 was not covered by tests
}
}

// nlohmann_json serializers
namespace nlohmann
{
#define MAKE_NLJSON_SERIALIZER_DESERIALIZER(T) \
template <> \
struct adl_serializer<T> \
{ \
inline static void to_json(json& j, const T& obj) \
{ \
j = pyjson::to_json(obj); \
} \
\
inline static T from_json(const json& j) \
{ \
return pyjson::from_json(j); \
} \
}

#define MAKE_NLJSON_SERIALIZER_ONLY(T) \
template <> \
struct adl_serializer<T> \
{ \
inline static void to_json(json& j, const T& obj) \
{ \
j = pyjson::to_json(obj); \
} \
}

MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::object);

MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::bool_);
MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::int_);
MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::float_);
MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::str);

MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::list);
MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::tuple);
MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::dict);

MAKE_NLJSON_SERIALIZER_ONLY(py::handle);
MAKE_NLJSON_SERIALIZER_ONLY(py::detail::item_accessor);
MAKE_NLJSON_SERIALIZER_ONLY(py::detail::list_accessor);
MAKE_NLJSON_SERIALIZER_ONLY(py::detail::tuple_accessor);
MAKE_NLJSON_SERIALIZER_ONLY(py::detail::sequence_accessor);
MAKE_NLJSON_SERIALIZER_ONLY(py::detail::str_attr_accessor);
MAKE_NLJSON_SERIALIZER_ONLY(py::detail::obj_attr_accessor);

#undef MAKE_NLJSON_SERIALIZER
#undef MAKE_NLJSON_SERIALIZER_ONLY
}

// pybind11 caster
namespace pybind11
{
namespace detail
{
template <> struct type_caster<nl::json>
{
public:
PYBIND11_TYPE_CASTER(nl::json, _("json"));

bool load(handle src, bool)
{
try
{
value = pyjson::to_json(src);
return true;
}
catch (...)

Check warning on line 209 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L209

Added line #L209 was not covered by tests
{
return false;

Check warning on line 211 in python/src/pybind11_json/pybind11_json.hpp

View check run for this annotation

Codecov / codecov/patch

python/src/pybind11_json/pybind11_json.hpp#L211

Added line #L211 was not covered by tests
}
}

static handle cast(nl::json src, return_value_policy /* policy */, handle /* parent */)
{
object obj = pyjson::from_json(src);
return obj.release();
}
};
}
}

#endif

0 comments on commit 1f28a11

Please sign in to comment.