1/*************************************************************************
2 * libjson-rpc-cpp
3 *************************************************************************
4 * @file specificationwriter.cpp
5 * @date 30.04.2013
6 * @author Peter Spiess-Knafl <dev@spiessknafl.at>
7 * @license See attached LICENSE.txt
8 ************************************************************************/
9
10#include "specificationwriter.h"
11#include "jsonparser.h"
12#include <fstream>
13#include <iostream>
14#include <jsonrpccpp/common/jsonparser.h>
15
16using namespace std;
17using namespace jsonrpc;
18
19Json::Value SpecificationWriter::toJsonValue(const vector<Procedure> &procedures) {
20 Json::Value result;
21 Json::Value row;
22 for (unsigned int i = 0; i < procedures.size(); i++) {
23 procedureToJsonValue(procedures.at(i), row);
24 result[i] = row;
25 row.clear();
26 }
27 return result;
28}
29std::string SpecificationWriter::toString(const vector<Procedure> &procedures) {
30 Json::StreamWriterBuilder wb;
31 wb["indentation"] = " ";
32 return Json::writeString(wb, toJsonValue(procedures));
33}
34bool SpecificationWriter::toFile(const std::string &filename, const vector<Procedure> &procedures) {
35 ofstream file;
36 file.open(filename.c_str(), ios_base::out);
37 if (!file.is_open())
38 return false;
39 file << toString(procedures);
40 file.close();
41 return true;
42}
43Json::Value SpecificationWriter::toJsonLiteral(jsontype_t type) {
44 Json::Value literal;
45 switch (type) {
46 case JSON_BOOLEAN:
47 literal = true;
48 break;
49 case JSON_STRING:
50 literal = "somestring";
51 break;
52 case JSON_REAL:
53 literal = 1.0;
54 break;
55 case JSON_NUMERIC:
56 literal = 1.0;
57 break;
58 case JSON_ARRAY:
59 literal = Json::arrayValue;
60 break;
61 case JSON_OBJECT:
62 literal["objectkey"] = "objectvalue";
63 break;
64 case JSON_INTEGER:
65 literal = 1;
66 break;
67 }
68 return literal;
69}
70void SpecificationWriter::procedureToJsonValue(const Procedure &procedure, Json::Value &target) {
71 target[KEY_SPEC_PROCEDURE_NAME] = procedure.GetProcedureName();
72 if (procedure.GetProcedureType() == RPC_METHOD) {
73 target[KEY_SPEC_RETURN_TYPE] = toJsonLiteral(procedure.GetReturnType());
74 }
75 for (parameterNameList_t::const_iterator it = procedure.GetParameters().begin(); it != procedure.GetParameters().end(); ++it) {
76 if (procedure.GetParameterDeclarationType() == PARAMS_BY_NAME) {
77 target[KEY_SPEC_PROCEDURE_PARAMETERS][it->first] = toJsonLiteral(it->second);
78 } else {
79 target[KEY_SPEC_PROCEDURE_PARAMETERS].append(toJsonLiteral(it->second));
80 }
81 }
82}
83