jgaa / restc-cpp

Modern C++ REST Client library
MIT License
615 stars 95 forks source link

Support for Arrays of Arrays of unsigned chars #59

Open kranti-madineni opened 6 years ago

kranti-madineni commented 6 years ago

I am sending Arrays of Arrays of unsigned chars through restc-cpp using the following code.

include < cstdio >

include < sstream >

include < iostream >

include "restc-cpp/logging.h"

include <boost/log/core.hpp>

include <boost/log/trivial.hpp>

include <boost/log/expressions.hpp>

include <boost/fusion/adapted.hpp>

include <boost/filesystem.hpp>

include <boost/scope_exit.hpp>

include "restc-cpp/restc-cpp.h"

include "restc-cpp/SerializeJson.h"

include "rapidjson/writer.h"

include "rapidjson/stringbuffer.h"

include "restc-cpp/test_helper.h"

include "lest/lest.hpp"

include <boost/lexical_cast.hpp>

include <boost/fusion/adapted.hpp>

include "restc-cpp/restc-cpp.h"

include "restc-cpp/SerializeJson.h"

include "restc-cpp/RequestBuilder.h"

include "restc-cpp/IteratorFromJsonSerializer.h"

include <rapidjson/ostreamwrapper.h>

include <rapidjson/prettywriter.h>

include <rapidjson/document.h>

using namespace std; using namespace restc_cpp; using namespace rapidjson;

typedef vector < vector < unsigned char > > LOCAL; typedef vector < vector < unsigned char > > GLOBAL; typedef vector < vector < unsigned char > > ADDRESS; struct MAC { ADDRESS address; }; BOOST_FUSION_ADAPT_STRUCT( MAC, (ADDRESS, address) ) typedef vector MACLIST;

struct DeviceList{ LOCAL local; GLOBAL global; MACLIST maclst; }; BOOST_FUSION_ADAPT_STRUCT( DeviceList, (LOCAL, local) (GLOBAL, global) (MACLIST, maclst) ) typedef vector DeviceLst; struct Service { string ServiceName; int CurrentStatus; int IsMaskable; int Priority; }; BOOST_FUSION_ADAPT_STRUCT ( Service, (string , ServiceName), (int , CurrentStatus), (int , IsMaskable), (int , Priority) ) typedef vector ListService; struct Config2 { int nIdSchedule = {}; int nDCUNo; ListService services; DeviceLst lst; }; BOOST_FUSION_ADAPT_STRUCT( Config2, (int, nIdSchedule) (int, nDCUNo) (ListService,services) (DeviceLst, lst) ) class CDCUSchedule { private: string sDCUNo; string sAddress="http://localhost:3000/Meters";//this is set up using json-server which is a mock json-server which can be used for application development. steps for setting up it are 1) sudo apt install npm 2) sudo npm install -g json-server 3) create a .json file with required data as i have given in the last of this code for example, named dcuconfig.json 4) start json-server using this command, json-server --watch dcuconfig.json 5) now from browser, we type, localhost:3000/meters, we get all data from json-server to our browser. public: CDCUSchedule(string DCU); bool getSchedule(vector &DCUSchedule); //bool updateSchedule(vector &DCUSchedule); }; //definition for DCU Class CDCUSchedule::CDCUSchedule(string DCU) { this->sDCUNo = DCU; } bool CDCUSchedule::getSchedule(vector &DcuSchedule) { bool bRetVal = false; try { // Create and instantiate a Post from data received from the server. DcuSchedule = RestClient::Create()->ProcessWithPromiseT<vector>([&](Context& ctx) { // This is a lambda co-routine, running in a worker-thread // Instantiate a Post structure. vector t; // Serialize it asynchronously. The asynchronously part does not really matter // here, but it may if you receive huge data structures. SerializeFromJson(t, // Construct a request to the server RequestBuilder(ctx) .Get(sAddress) // Add some headers for good taste //.Header("X-Client", "RESTC_CPP") //.Header("X-Client-Purpose", "Testing") .DisableCompression() //.Argument("DcuNo",this->sDCUNo) // Send the request .Execute()); // Return the Post instance trough a C++ future<> return t; }).get(); bRetVal = true; } catch (exception &e) { // LOG(ERROR) << "<<Exception Caught in getSchedule for DCU Serial Number:" << this->sDCUNo << ":" << e.what(); cout<<"exception caught"<<endl; } return bRetVal; } int main( int argc, char * argv[] ) { namespace logging = boost::log; logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::warning ); CDCUSchedule dcu("104400"); std::vector dcusched; dcu.getSchedule(dcusched); cout<<dcusched[0].services[0].CurrentStatus<<endl; cout<<dcusched[1].services[1].ServiceName<<endl; cout<<dcusched[1].lst[0].global[0][0]<<endl; cout<<dcusched[0].lst[0].maclst[0].address[1][1]<<endl; cout<<dcusched[1].nDCUNo<<endl; cout<<endl; }

Example dcuconfig.json file is { "Meters": [ { "nIdSchedule": 5, "nDCUNo": 104400, "services":[ { "ServiceName":"service1","CurrentStatus":1,"IsMaskable":1,"Priority":1 } ], "lst": [{ "local":[ [65,66,67,68,69,69,70,80],[66,66,66,66,66,66,66,66],[67,67,67,67,67,67,67,67]], "global":[ [71,72,73,74,75,76,77,78],[68,68,68,68,68,68,68,68],[69,69,69,69,69,69,69,69]], "maclst": [{ "address":[ [48,49,65,73,74,75,76,78],[70,70,70,70,70,70,70,70],[71,71,71,71,71,71,71,71]] }]

            }]
            },
            {
            "nIdSchedule": 5,
            "nDCUNo": 104400,
              "services":[
                       {
                        "ServiceName":"service2","CurrentStatus":1,"IsMaskable":1,"Priority":2
                       }
                       ],
              "lst": [{
                    "local":[ [65,66,67,68,69,69,70,80],[66,66,66,66,66,66,66,66],[67,67,67,67,67,67,67,67]],
                    "global":[ [71,72,73,74,75,76,77,78],[68,68,68,68,68,68,68,68],[69,69,69,69,69,69,69,69]],
                    "maclst": [{
                           "address":[ [48,49,65,73,74,75,76,78],[70,70,70,70,70,70,70,70],[71,71,71,71,71,71,71,71]]
                    }]

            }]

} ] }

And I am getting the following error during execution. screenshot from 2018-04-06 11-52-40 Am I doing something wrong or the support for arrays of arrays is not there?

kranti-madineni commented 6 years ago

Jarle Sir, I have changed my structure correctly so that this kind of issue is not there in my project...So it is not a show stopper for us...You can work on it if you really think that these kind of JSON structure format is used often in JSON usecases...As I am not an expert in JSON file formats...If its very unusual, you better close this bug as not applicable. Thanking You, Kranti

jgaa commented 6 years ago

I will look closer at this one once I have a few hours spare time at the start of the day (when I am most focused). I don't know how common this is, but if it is valid json and expressible in C++, it should work with restc-cpp.