fpmas 1.5
Static Public Member Functions | List of all members
nlohmann::adl_serializer< fpmas::api::utils::PtrWrapper< AgentType > > Struct Template Reference

#include <json_serializer.h>

Static Public Member Functions

template<typename JsonType >
static fpmas::api::utils::PtrWrapper< AgentType > from_json (const JsonType &j)
 
template<typename JsonType >
static void to_json (JsonType &j, const fpmas::api::utils::PtrWrapper< AgentType > &agent_ptr)
 

Detailed Description

template<typename AgentType>
struct nlohmann::adl_serializer< fpmas::api::utils::PtrWrapper< AgentType > >

Generic Agent pointer serialization.

This partial specialization can be used to easily define serialization rules directly from the Agent implementation, using the following static method definitions :

class UserDefinedAgent : fpmas::model::AgentBase<UserDefinedAgent> {
...
public:
...
static void to_json(
nlohmann::json& j,
const UserDefinedAgent* agent
);
static UserDefinedAgent* from_json(
const nlohmann::json& j
);
...
};
Definition: model.h:702
static void to_json(JsonType &j, const fpmas::api::utils::PtrWrapper< AgentType > &agent_ptr)
Definition: json_serializer.h:332
static fpmas::api::utils::PtrWrapper< AgentType > from_json(const JsonType &j)
Definition: json_serializer.h:321

The from_json method is assumed to return an heap allocated agent (initialized with a new statement).

Example
class Agent1 : public fpmas::model::AgentBase<Agent1> {
private:
int count;
std::string message;
public:
Agent1(int count, std::string message)
: count(count), message(message) {}
static void to_json(nlohmann::json& j, const Agent1* agent) {
j["c"] = agent->count;
j["m"] = agent->message;
}
static Agent1* from_json(const nlohmann::json& j) {
return new Agent1(
j.at("c").get<int>(),
j.at("m").get<std::string>()
);
}
void act() override;
};
Note
Notice that its still possible to define adl_serializer specialization without using the internal to_json / from_json methods.
Example
class Agent1 : public fpmas::model::AgentBase<Agent1> {
private:
int count;
std::string message;
public:
Agent1(int count, std::string message)
: count(count), message(message) {}
int getCount() const {return count;}
std::string getMessage() const {return message;}
void act() override;
};
namespace nlohmann {
template<>
struct adl_serializer<fpmas::api::utils::PtrWrapper<Agent1>> {
static void to_json(
json& j,
) {
j["c"] = agent_ptr->getCount();
j["m"] = agent_ptr->getMessage();
}
json& j
) {
return {new Agent1(
j.at("c").get<int>(),
j.at("m").get<std::string>()
)};
}
};
}
Definition: ptr_wrapper.h:21
Definition: fpmas.cpp:3

Member Function Documentation

◆ from_json()

template<typename AgentType >
template<typename JsonType >
static fpmas::api::utils::PtrWrapper< AgentType > nlohmann::adl_serializer< fpmas::api::utils::PtrWrapper< AgentType > >::from_json ( const JsonType &  j)
inlinestatic

Returns a PtrWrapper initialized with AgentType::from_json(j).

Parameters
jjson
Returns
unserialized agent pointer

◆ to_json()

template<typename AgentType >
template<typename JsonType >
static void nlohmann::adl_serializer< fpmas::api::utils::PtrWrapper< AgentType > >::to_json ( JsonType &  j,
const fpmas::api::utils::PtrWrapper< AgentType > &  agent_ptr 
)
inlinestatic

Calls AgentType::to_json(j, agent_ptr.get()).

Parameters
jjson
agent_ptragent pointer to serialized

The documentation for this struct was generated from the following file: