wip continue to replace jsoncpp to json-c

This commit is contained in:
jbnadal
2018-04-04 10:58:28 +02:00
parent 5220d2b0d8
commit 98e209ef09
7 changed files with 113 additions and 75 deletions

View File

@@ -34,6 +34,8 @@
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class Light class Light

View File

@@ -90,26 +90,24 @@ int ShutterDevice::from_json(struct json_object *a_node)
*/ */
struct json_object *ShutterDevice::to_json(void) struct json_object *ShutterDevice::to_json(void)
{ {
#if 0 struct json_object *the_root_node, *the_data_node;
Json::Value theResult; std::vector<Shutter>::iterator the_shutter_it;
Json::Value data_json(Json::arrayValue);
std::vector<Shutter>::iterator theShutter_it;
fprintf(stderr, "ShutterDevice::to_json\n"); fprintf(stderr, "ShutterDevice::to_json\n");
for (theShutter_it = mShutter.begin(); theShutter_it != mShutter.end(); theShutter_it++) { the_root_node = json_object_new_object();
the_data_node = json_object_new_array();
data_json.append ((*theShutter_it).to_json()); // speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// data
json_object_object_add(the_root_node, k_entry_data, the_data_node);
for (the_shutter_it = m_shutters.begin(); the_shutter_it != m_shutters.end(); the_shutter_it++)
{
json_object_array_add(the_data_node, (*the_shutter_it).to_json());
} }
theResult["speach_name"] = mspeach_name; return the_root_node;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
#endif
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------

View File

@@ -34,6 +34,8 @@
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class Shutter class Shutter

View File

@@ -23,14 +23,13 @@
* *
*/ */
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio> #include <cstdio>
#include "devices/sprinkler-device.h" #include <json-c/json.h>
#include "devices/sprinkler-device.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn SprinklerDevice * @fn SprinklerDevice
@@ -41,7 +40,6 @@ SprinklerDevice::SprinklerDevice (void) : Device()
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~SprinklerDevice * @fn ~SprinklerDevice
* *
@@ -51,7 +49,6 @@ SprinklerDevice::~SprinklerDevice (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn from_json * @fn from_json
* *
@@ -59,22 +56,33 @@ SprinklerDevice::~SprinklerDevice (void)
*/ */
int SprinklerDevice::from_json(struct json_object *a_node) int SprinklerDevice::from_json(struct json_object *a_node)
{ {
struct json_object *the_data_node;
fprintf(stderr, "SprinklerDevice::load_from_json\n"); fprintf(stderr, "SprinklerDevice::load_from_json\n");
#if 0
mspeach_name = anElem["speach_name"].asString();
for (Json::Value& theElement : anElem["data"]) { // speach name
Sprinkler theSprinkler; Device::from_json(a_node);
if (theSprinkler.load_from_json (theElement) == 0) {
mSprinkler.push_back (theSprinkler); // Get Sprinkler
if (json_object_object_get_ex(a_node, k_entry_data, &the_data_node) && (json_object_get_type(the_data_node) == json_type_array))
{
int the_len;
struct json_object *the_sprinkler_node;
the_len = json_object_array_length(the_data_node);
for (int i = 0; i < the_len; i++)
{
Sprinkler the_sprinkler;
the_sprinkler_node = json_object_array_get_idx(the_data_node, i);
the_sprinkler.from_json(the_sprinkler_node);
m_sprinklers.push_back(the_sprinkler);
} }
} }
#endif
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn to_json * @fn to_json
* *
@@ -82,29 +90,26 @@ int SprinklerDevice::from_json (struct json_object *a_node)
*/ */
struct json_object *SprinklerDevice::to_json(void) struct json_object *SprinklerDevice::to_json(void)
{ {
#if 0 struct json_object *the_root_node, *the_data_node;
Json::Value theResult; std::vector<Sprinkler>::iterator the_sprintkler_it;
Json::Value data_json(Json::arrayValue);
std::vector<Sprinkler>::iterator theSprinkler_it;
fprintf(stderr, "SprinklerDevice::to_json\n"); fprintf(stderr, "SprinklerDevice::to_json\n");
for (theSprinkler_it = mSprinkler.begin(); theSprinkler_it != mSprinkler.end(); theSprinkler_it++) { the_root_node = json_object_new_object();
the_data_node = json_object_new_array();
data_json.append ((*theSprinkler_it).to_json()); // speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// data
json_object_object_add(the_root_node, k_entry_data, the_data_node);
for (the_sprintkler_it = m_sprinklers.begin(); the_sprintkler_it != m_sprinklers.end(); the_sprintkler_it++)
{
json_object_array_add(the_data_node, (*the_sprintkler_it).to_json());
} }
theResult["speach_name"] = mspeach_name; return the_root_node;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
#endif
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set * @fn set
* *

View File

@@ -51,7 +51,7 @@ class SprinklerDevice : public Device
int set(int an_id, bool a_state); int set(int an_id, bool a_state);
private: private:
std::vector<Sprinkler> m_sprinkler; std::vector<Sprinkler> m_sprinklers;
}; };
#endif /* _SPRINKLER_DEVICE_H */ #endif /* _SPRINKLER_DEVICE_H */

View File

@@ -27,10 +27,19 @@
#include <cstdio> #include <cstdio>
#include <json-c/json.h>
#include <ubus-cpp/ubus-call.h> #include <ubus-cpp/ubus-call.h>
#include "devices/sprinkler.h" #include "devices/sprinkler.h"
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_state "state"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Sprinkler * @fn Sprinkler
* *
@@ -56,12 +65,29 @@ Sprinkler::~Sprinkler(void)
*/ */
int Sprinkler::from_json(struct json_object *a_node) int Sprinkler::from_json(struct json_object *a_node)
{ {
#if 0 struct json_object *the_value_node;
mID = anElem["id"].asInt(); // TODO: Should shared with Light.
mName = anElem["name"].asString(); // id
mSpeach_name = anElem["speach_name"].asString(); if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node))
mState = anElem["state"].asBool(); {
#endif m_id = json_object_get_int(the_value_node);
}
// name
if (json_object_object_get_ex(a_node, k_entry_name, &the_value_node))
{
m_name = json_object_get_string(the_value_node);
}
// speach_name
if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node))
{
m_speach_name = json_object_get_string(the_value_node);
}
// state
if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
{
m_state = json_object_get_boolean(the_value_node);
}
return 0; return 0;
} }
@@ -72,16 +98,20 @@ int Sprinkler::from_json(struct json_object *a_node)
*/ */
struct json_object *Sprinkler::to_json(void) const struct json_object *Sprinkler::to_json(void) const
{ {
#if 0 struct json_object *the_root_node;
Json::Value aResult(Json::objectValue);
fprintf (stderr, "Sprinkler::to_json\n");
aResult["id"] = mID;
aResult["name"] = mName;
aResult["speach_name"] = mSpeach_name;
aResult["state"] = mState;
return aResult; the_root_node = json_object_new_object();
#endif
// id
json_object_object_add(the_root_node, k_entry_id, json_object_new_int(m_id));
// name
json_object_object_add(the_root_node, k_entry_name, json_object_new_string(m_name.c_str()));
// speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// state
json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state));
return the_root_node;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------

View File

@@ -34,11 +34,12 @@
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class Sprinkler class Sprinkler
{ {
public: public:
Sprinkler(void); Sprinkler(void);
~Sprinkler(void); ~Sprinkler(void);
@@ -55,8 +56,8 @@ class Sprinkler
private: private:
uint16_t m_id; uint16_t m_id;
std::string mName; std::string m_name;
std::string mSpeach_name; std::string m_speach_name;
bool m_state; bool m_state;
}; };