init
This commit is contained in:
39
unitree_SDK/include/unitree/common/json/json.hpp
Normal file
39
unitree_SDK/include/unitree/common/json/json.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef __UT_JSON_HPP__
|
||||
#define __UT_JSON_HPP__
|
||||
|
||||
#include <unitree/common/any.hpp>
|
||||
|
||||
namespace unitree
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
typedef std::map<std::string,Any> JsonMap;
|
||||
typedef std::vector<Any> JsonArray;
|
||||
|
||||
Any FromJsonString(const std::string& s);
|
||||
|
||||
std::string ToJsonString(const Any& a, bool pretty = false);
|
||||
|
||||
static inline bool IsNull(const Any& a)
|
||||
{
|
||||
return a.Empty();
|
||||
}
|
||||
|
||||
static inline bool IsJsonArray(const Any& a)
|
||||
{
|
||||
return a.GetTypeInfo() == typeid(JsonArray);
|
||||
}
|
||||
|
||||
static inline bool IsJsonMap(const Any& a)
|
||||
{
|
||||
return a.GetTypeInfo() == typeid(JsonMap);
|
||||
}
|
||||
|
||||
static inline bool IsJsonObject(const Any& a)
|
||||
{
|
||||
return IsJsonMap(a);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif//__UT_JSON_HPP__
|
194
unitree_SDK/include/unitree/common/json/json_config.hpp
Normal file
194
unitree_SDK/include/unitree/common/json/json_config.hpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#ifndef __UT_JSON_CONFIG_HPP__
|
||||
#define __UT_JSON_CONFIG_HPP__
|
||||
|
||||
#include <unitree/common/json/json.hpp>
|
||||
|
||||
#define UT_JSON_CONF_KEY_PARAMETER "Parameter"
|
||||
|
||||
namespace unitree
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
class JsonConfig
|
||||
{
|
||||
public:
|
||||
JsonConfig();
|
||||
virtual ~JsonConfig();
|
||||
|
||||
JsonConfig(const std::string& configFileName);
|
||||
|
||||
virtual void Parse(const std::string& configFileName);
|
||||
virtual void ParseContent(const std::string& content);
|
||||
|
||||
//top-level field
|
||||
bool Has(const std::string& name) const;
|
||||
|
||||
//top-level field
|
||||
const Any& Get(const std::string& name) const;
|
||||
|
||||
//top-level field
|
||||
template<typename T>
|
||||
const T& Get(const std::string& name) const
|
||||
{
|
||||
return AnyCast<T>(Get(name));
|
||||
}
|
||||
|
||||
//top-level field
|
||||
template<typename T>
|
||||
T GetNumber(const std::string& name) const
|
||||
{
|
||||
return AnyNumberCast<T>(Get(name));
|
||||
}
|
||||
|
||||
//top-level field
|
||||
template<typename T>
|
||||
T Get(const std::string& name, const T& defValue) const
|
||||
{
|
||||
const Any& a = Get(name);
|
||||
|
||||
if (a.Empty())
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return AnyCast<T>(a);
|
||||
}
|
||||
}
|
||||
|
||||
//top-level field
|
||||
template<typename T>
|
||||
T GetNumber(const std::string& name, const T& defValue) const
|
||||
{
|
||||
const Any& a = Get(name);
|
||||
|
||||
if (a.Empty())
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return AnyNumberCast<T>(a);
|
||||
}
|
||||
}
|
||||
|
||||
//JsonMap field
|
||||
bool Has(const JsonMap& jsonMap, const std::string& name) const;
|
||||
|
||||
//JsonMap field
|
||||
const Any& Get(const JsonMap& jsonMap, const std::string& name) const;
|
||||
|
||||
//JsonMap field
|
||||
template<typename T>
|
||||
const T& Get(const JsonMap& jsonMap, const std::string& name) const
|
||||
{
|
||||
return AnyCast<T>(Get(jsonMap, name));
|
||||
}
|
||||
|
||||
//JsonMap field
|
||||
template<typename T>
|
||||
T GetNumber(const JsonMap& jsonMap, const std::string& name) const
|
||||
{
|
||||
return AnyNumberCast<T>(Get(jsonMap, name));
|
||||
}
|
||||
|
||||
//JsonMap field
|
||||
template<typename T>
|
||||
T Get(const JsonMap& jsonMap, const std::string& name, const T& defValue) const
|
||||
{
|
||||
const Any& a = Get(jsonMap, name);
|
||||
|
||||
if (a.Empty())
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return AnyCast<T>(a);
|
||||
}
|
||||
}
|
||||
|
||||
//JsonMap field
|
||||
template<typename T>
|
||||
T GetNumber(const JsonMap& jsonMap, const std::string& name, const T& defValue) const
|
||||
{
|
||||
const Any& a = Get(jsonMap, name);
|
||||
|
||||
if (a.Empty())
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return AnyNumberCast<T>(a);
|
||||
}
|
||||
}
|
||||
|
||||
//top-level field
|
||||
const Any& GetGlobalParameter(const std::string& name) const;
|
||||
|
||||
//top-level field: Parameter
|
||||
bool HasParameter(const std::string& name) const;
|
||||
|
||||
//top-level field: Parameter
|
||||
const JsonMap& GetParameter() const;
|
||||
|
||||
//get field/value from top-level field: Parameter
|
||||
const Any& GetParameter(const std::string& name) const;
|
||||
|
||||
//get field/value from top-level field: Parameter
|
||||
template<typename T>
|
||||
const T& GetParameter(const std::string& name) const
|
||||
{
|
||||
return AnyCast<T>(GetParameter(name));
|
||||
}
|
||||
|
||||
//get field/value from top-level field: Parameter
|
||||
template<typename T>
|
||||
T GetNumberParameter(const std::string& name) const
|
||||
{
|
||||
return AnyNumberCast<T>(GetParameter(name));
|
||||
}
|
||||
|
||||
//get field/value from top-level field: Parameter
|
||||
template<typename T>
|
||||
T GetParameter(const std::string& name, const T& defValue) const
|
||||
{
|
||||
const Any& a = GetParameter(name);
|
||||
|
||||
if (a.Empty())
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return AnyCast<T>(a);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T GetNumberParameter(const std::string& name, const T& defValue) const
|
||||
{
|
||||
const Any& a = GetParameter(name);
|
||||
|
||||
if (a.Empty())
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return AnyNumberCast<T>(a);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
JsonMap mParameter;
|
||||
Any mContent;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<JsonConfig> JsonConfigPtr;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif//__UT_JSON_CONFIG_HPP__
|
275
unitree_SDK/include/unitree/common/json/jsonize.hpp
Normal file
275
unitree_SDK/include/unitree/common/json/jsonize.hpp
Normal file
@@ -0,0 +1,275 @@
|
||||
#ifndef __UT_JSONIZE_HPP__
|
||||
#define __UT_JSONIZE_HPP__
|
||||
|
||||
#define JN_FROM_WEAK(m, name, value) \
|
||||
if (m.find(name) != m.end()){ JN_FROM(m, name, value); }
|
||||
|
||||
#define JN_FROM(m, name, value) \
|
||||
unitree::common::FromJson(m[name], value)
|
||||
|
||||
#define JN_TO(m, name, value) \
|
||||
unitree::common::ToJson(value, m[name])
|
||||
|
||||
#include <unitree/common/json/json.hpp>
|
||||
|
||||
namespace unitree
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
template<typename T>
|
||||
void FromJson(const Any& a, T& t);
|
||||
|
||||
template<typename T>
|
||||
void ToJson(const T& value, Any& a);
|
||||
|
||||
template<typename T>
|
||||
void FromJsonString(const std::string& s, T& t)
|
||||
{
|
||||
Any a = FromJsonString(s);
|
||||
FromJson<T>(a, t);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::string ToJsonString(const T& t, bool pretty = false)
|
||||
{
|
||||
Any a;
|
||||
ToJson<T>(t, a);
|
||||
return ToJsonString(a, pretty);
|
||||
}
|
||||
|
||||
class Jsonize
|
||||
{
|
||||
public:
|
||||
virtual void toJson(JsonMap& a) const = 0;
|
||||
virtual void fromJson(JsonMap& a) = 0;
|
||||
};
|
||||
|
||||
void FromAny(const Any& a, int8_t& value);
|
||||
|
||||
void FromAny(const Any& a, uint8_t& value);
|
||||
|
||||
void FromAny(const Any& a, int16_t& value);
|
||||
|
||||
void FromAny(const Any& a, uint16_t& value);
|
||||
|
||||
void FromAny(const Any& a, int32_t& value);
|
||||
|
||||
void FromAny(const Any& a, uint32_t& value);
|
||||
|
||||
void FromAny(const Any& a, int64_t& value);
|
||||
|
||||
void FromAny(const Any& a, uint64_t& value);
|
||||
|
||||
void FromAny(const Any& a, float& value);
|
||||
|
||||
void FromAny(const Any& a, double& value);
|
||||
|
||||
void FromAny(const Any& a, bool& value);
|
||||
|
||||
void FromAny(const Any& a, std::string& value);
|
||||
|
||||
void FromAny(const Any& a, JsonMap& value);
|
||||
|
||||
void FromAny(const Any& a, JsonArray& value);
|
||||
|
||||
void FromAny(const Any& a, Jsonize& value);
|
||||
|
||||
template<typename E>
|
||||
void FromAny(const Any& a, std::vector<E>& value)
|
||||
{
|
||||
if (a.Empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const JsonArray& arr = AnyCast<JsonArray>(a);
|
||||
size_t i, count = arr.size();
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
E e;
|
||||
const Any& a_in = arr[i];
|
||||
FromJson<E>(a_in, e);
|
||||
value.push_back(std::move(e));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename E>
|
||||
void FromAny(const Any& a, std::list<E>& value)
|
||||
{
|
||||
if (a.Empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const JsonArray& arr = AnyCast<JsonArray>(a);
|
||||
size_t i, count = arr.size();
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
E e;
|
||||
const Any& a_in = arr[i];
|
||||
FromJson<E>(a_in, e);
|
||||
value.push_back(std::move(e));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename E>
|
||||
void FromAny(const Any& a, std::set<E>& value)
|
||||
{
|
||||
if (a.Empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const JsonArray& arr = AnyCast<JsonArray>(a);
|
||||
size_t i, count = arr.size();
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
E e;
|
||||
const Any& a_in = arr[i];
|
||||
FromJson<E>(a_in, e);
|
||||
value.insert(std::move(e));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename E>
|
||||
void FromAny(const Any& a, std::map<std::string,E>& value)
|
||||
{
|
||||
if (a.Empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const JsonMap& m = AnyCast<JsonMap>(a);
|
||||
JsonMap::const_iterator iter;
|
||||
|
||||
for (iter = m.begin(); iter != m.end(); ++iter)
|
||||
{
|
||||
E e;
|
||||
const std::string& name = iter->first;
|
||||
const Any& a_in = iter->second;
|
||||
FromJson<E>(a_in, e);
|
||||
value[name] = std::move(e);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void FromJson(const Any& a, T& t)
|
||||
{
|
||||
FromAny(a, t);
|
||||
}
|
||||
|
||||
void ToAny(const int8_t& value, Any& a);
|
||||
|
||||
void ToAny(const uint8_t& value, Any& a);
|
||||
|
||||
void ToAny(const int16_t& value, Any& a);
|
||||
|
||||
void ToAny(const uint16_t& value, Any& a);
|
||||
|
||||
void ToAny(const int32_t& value, Any& a);
|
||||
|
||||
void ToAny(const uint32_t& value, Any& a);
|
||||
|
||||
void ToAny(const int64_t& value, Any& a);
|
||||
|
||||
void ToAny(const uint64_t& value, Any& a);
|
||||
|
||||
void ToAny(const float& value, Any& a);
|
||||
|
||||
void ToAny(const double& value, Any& a);
|
||||
|
||||
void ToAny(const bool& value, Any& a);
|
||||
|
||||
void ToAny(const std::string& value, Any& a);
|
||||
|
||||
void ToAny(const JsonMap& value, Any& a);
|
||||
|
||||
void ToAny(const JsonArray& value, Any& a);
|
||||
|
||||
void ToAny(const Jsonize& value, Any& a);
|
||||
|
||||
template<typename E>
|
||||
void ToAny(const std::vector<E>& value, Any& a)
|
||||
{
|
||||
JsonArray arr;
|
||||
|
||||
size_t i, count = value.size();
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
Any a_in;
|
||||
const E& e = value[i];
|
||||
|
||||
ToJson<E>(e, a_in);
|
||||
arr.push_back(std::move(a_in));
|
||||
}
|
||||
|
||||
a = Any(arr);
|
||||
}
|
||||
|
||||
template<typename E>
|
||||
void ToAny(const std::list<E>& value, Any& a)
|
||||
{
|
||||
JsonArray arr;
|
||||
|
||||
typename std::list<E>::const_iterator iter;
|
||||
for (iter = value.begin(); iter != value.end(); ++iter)
|
||||
{
|
||||
Any a_in;
|
||||
const E& e = *iter;
|
||||
|
||||
ToJson<E>(e, a_in);
|
||||
arr.push_back(std::move(a_in));
|
||||
}
|
||||
|
||||
a = Any(arr);
|
||||
}
|
||||
|
||||
template<typename E>
|
||||
void ToAny(const std::set<E>& value, Any& a)
|
||||
{
|
||||
JsonArray arr;
|
||||
|
||||
typename std::set<E>::const_iterator iter;
|
||||
for (iter = value.begin(); iter != value.end(); ++iter)
|
||||
{
|
||||
Any a_in;
|
||||
const E& e = *iter;
|
||||
|
||||
ToJson<E>(e, a_in);
|
||||
arr.push_back(std::move(a_in));
|
||||
}
|
||||
|
||||
a = Any(arr);
|
||||
}
|
||||
|
||||
template<typename E>
|
||||
void ToAny(const std::map<std::string,E>& value, Any& a)
|
||||
{
|
||||
JsonMap m;
|
||||
typename std::map<std::string,E>::const_iterator iter;
|
||||
|
||||
for (iter = value.begin(); iter != value.end(); ++iter)
|
||||
{
|
||||
Any a_in;
|
||||
const std::string& name = iter->first;
|
||||
const E& e = iter->second;
|
||||
|
||||
ToJson<E>(e, a_in);
|
||||
m[name] = a_in;
|
||||
}
|
||||
|
||||
a = Any(m);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void ToJson(const T& value, Any& a)
|
||||
{
|
||||
//std::cout << typeid(value).name() << std::endl;
|
||||
ToAny(value, a);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif//__UT_JSONIZE_HPP__
|
Reference in New Issue
Block a user