source: sources/src/rpcJsonResponse.cc @ 1464:3f6711617be1

Revision 1464:3f6711617be1, 4.5 KB checked in by niam, 22 months ago (diff)

{issue #58[resolved]} support for non-c++ exceptions

Line 
1/***************************************************************************
2 *            rpcJsonResponse.cc
3 *
4 *  Mon Jul 07 2008
5 *  Copyright  2008  Dmytro Milinevskyy
6 *  milinevskyy@gmail.com
7 ****************************************************************************/
8
9/*
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License version 2.1 as published by
12 *  the Free Software Foundation;
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU Library General Public License for more details.
18 *
19 *  You should have received a copy of the GNU Lesser General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/**
25 * vim indentation settings
26 * set tabstop=4
27 * set shiftwidth=4
28 */
29
30#include <libdodo/directives.h>
31
32#include <libdodo/rpcJsonResponse.h>
33#include <libdodo/types.h>
34#include <libdodo/dataFormatJsonNode.h>
35#include <libdodo/rpcResponse.h>
36#include <libdodo/rpcJsonResponseEx.h>
37#include <libdodo/rpcJsonValue.h>
38
39using namespace dodo::rpc::json;
40
41dodo::rpc::response
42response::jsonToResponse(dodo::data::format::json::node &node,
43                         dodo::string                     &version,
44                         long                           &id)
45{
46    if (node.valueDataType != dodo::data::format::json::node::DATA_OBJECT)
47        dodo_throw exception::basic(exception::MODULE_RPCJSONRESPONSE, RESPONSEEX_JSONTORESPONSE, exception::ERRNO_LIBDODO, RESPONSEEX_ROOTNOTANOBJECT, RPCJSONRESPONSEEX_ROOTNOTANOBJECT_STR, __LINE__, __FILE__);
48
49    rpc::response resp;
50
51    version = (*node.objectValue)["version"].string();
52    id = (*node.objectValue)["id"].numeric();
53
54    const dodo::data::format::json::node &error = (*node.objectValue)["error"];
55
56    if (!error.isNull())
57        resp.rValues.push_back(value::jsonToValue(error));
58    else {
59        resp.succ = true;
60
61        const dodo::data::format::json::node &result = (*node.objectValue)["result"];
62
63        if (result.valueDataType == dodo::data::format::json::node::DATA_ARRAY) {
64            dodoArray<dodo::data::format::json::node>::const_iterator
65            i = result.arrayValue->begin(),
66            j = result.arrayValue->end();
67            for (; i != j; ++i)
68                resp.rValues.push_back(value::jsonToValue(*i));
69        } else
70            resp.rValues.push_back(value::jsonToValue(result));
71    }
72
73    return resp;
74}
75
76//-------------------------------------------------------------------
77
78dodo::data::format::json::node
79response::responseToJson(const rpc::response &data,
80                         const dodo::string    &version,
81                         long                id)
82{
83    dodo::data::format::json::node resp;
84    dodo::data::format::json::node node;
85
86    resp.valueDataType = dodo::data::format::json::node::DATA_OBJECT;
87    resp.objectValue = new dodoMap<dodo::string, dodo::data::format::json::node, dodoMapStringCompare>;
88
89    node.valueDataType = dodo::data::format::json::node::DATA_STRING;
90
91    node.stringValue = new dodo::string(version);
92    resp.objectValue->insert(std::make_pair(dodo::string("version"), node));
93    delete node.stringValue;
94
95    node.valueDataType = dodo::data::format::json::node::DATA_NUMERIC;
96    node.numericValue = id;
97    resp.objectValue->insert(std::make_pair(dodo::string("id"), node));
98
99    dodoArray<rpc::value>::const_iterator i = data.rValues.begin(), j = data.rValues.end();
100    if (i != j) {
101        if (!data.succ)
102            resp.objectValue->insert(std::make_pair(dodo::string("error"), value::valueToJson(*i)));
103        else {
104            if (data.rValues.size() == 1)
105                resp.objectValue->insert(std::make_pair(dodo::string("result"), value::valueToJson(*i)));
106            else {
107                node.valueDataType = dodo::data::format::json::node::DATA_ARRAY;
108                node.arrayValue = new dodoArray<dodo::data::format::json::node>;
109
110                for (; i != j; ++i)
111                    node.arrayValue->push_back(value::valueToJson(*i));
112
113                resp.objectValue->insert(std::make_pair(dodo::string("result"), node));
114
115                delete node.arrayValue;
116                node.valueDataType = dodo::data::format::json::node::DATA_NULL;
117            }
118        }
119    }
120
121    return resp;
122}
123
124//-------------------------------------------------------------------
125
Note: See TracBrowser for help on using the repository browser.