Merge lp:~stolowski/unity-scope-fake/no-jsoncpp into lp:unity-scope-fake

Proposed by Paweł Stołowski
Status: Merged
Approved by: Marcus Tomlinson
Approved revision: 61
Merged at revision: 60
Proposed branch: lp:~stolowski/unity-scope-fake/no-jsoncpp
Merge into: lp:unity-scope-fake
Diff against target: 229 lines (+47/-87)
4 files modified
CMakeLists.txt (+0/-2)
debian/control (+0/-1)
src/CMakeLists.txt (+0/-1)
src/scope.cpp (+47/-83)
To merge this branch: bzr merge lp:~stolowski/unity-scope-fake/no-jsoncpp
Reviewer Review Type Date Requested Status
Marcus Tomlinson (community) Approve
Review via email: mp+297297@code.launchpad.net

Commit message

Drop jsoncpp, use Variant - JSON (de)serialization from scopes API.

Description of the change

Drop jsoncpp, use Variant - JSON (de)serialization from scopes API.

To post a comment you must log in.
Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

While scanning through the project I noticed some very small issues here and there. When you have time: https://code.launchpad.net/~marcustomlinson/unity-scope-fake/small-fixes/+merge/297428

Otherwise, this MP looks good! +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt 2016-05-19 16:19:45 +0000
+++ CMakeLists.txt 2016-06-14 08:56:05 +0000
@@ -16,7 +16,6 @@
16include(GNUInstallDirs)16include(GNUInstallDirs)
17find_package(PkgConfig)17find_package(PkgConfig)
18find_package(Boost COMPONENTS system filesystem REQUIRED)18find_package(Boost COMPONENTS system filesystem REQUIRED)
19pkg_check_modules(JSONCPP jsoncpp REQUIRED)
2019
21# Search for our dependencies20# Search for our dependencies
22pkg_check_modules(21pkg_check_modules(
@@ -34,7 +33,6 @@
34 "${CMAKE_SOURCE_DIR}/include"33 "${CMAKE_SOURCE_DIR}/include"
35 ${SCOPE_INCLUDE_DIRS}34 ${SCOPE_INCLUDE_DIRS}
36 ${Boost_INCLUDE_DIRS}35 ${Boost_INCLUDE_DIRS}
37 ${JSONCPP_INCLUDE_DIRS}
38)36)
3937
40set(FAKESCOPE_EXE "${CMAKE_INSTALL_FULL_LIBDIR}/fake-scope/fakescope")38set(FAKESCOPE_EXE "${CMAKE_INSTALL_FULL_LIBDIR}/fake-scope/fakescope")
4139
=== modified file 'debian/control'
--- debian/control 2016-05-24 08:44:51 +0000
+++ debian/control 2016-06-14 08:56:05 +0000
@@ -5,7 +5,6 @@
5Build-Depends: cmake (>= 3.0),5Build-Depends: cmake (>= 3.0),
6 debhelper (>= 9),6 debhelper (>= 9),
7 libunity-scopes-dev (>= 1.0.3),7 libunity-scopes-dev (>= 1.0.3),
8 libjsoncpp-dev,
9 libboost-dev,8 libboost-dev,
10 libboost-filesystem-dev,9 libboost-filesystem-dev,
11 pkg-config,10 pkg-config,
1211
=== modified file 'src/CMakeLists.txt'
--- src/CMakeLists.txt 2016-05-24 09:39:52 +0000
+++ src/CMakeLists.txt 2016-06-14 08:56:05 +0000
@@ -23,7 +23,6 @@
23 fakescope23 fakescope
24 ${SCOPE_LIBRARIES}24 ${SCOPE_LIBRARIES}
25 ${Boost_LIBRARIES}25 ${Boost_LIBRARIES}
26 ${JSONCPP_LIBRARIES}
27)26)
2827
29install(28install(
3029
=== modified file 'src/scope.cpp'
--- src/scope.cpp 2016-05-24 10:35:09 +0000
+++ src/scope.cpp 2016-06-14 08:56:05 +0000
@@ -19,11 +19,10 @@
19#include "query.h"19#include "query.h"
20#include "scope.h"20#include "scope.h"
21#include "preview.h"21#include "preview.h"
22#include <json/reader.h>
23#include <json/writer.h>
2422
23#include <iostream>
25#include <fstream>24#include <fstream>
26#include <iostream>25#include <sstream>
27#include <boost/algorithm/string/replace.hpp>26#include <boost/algorithm/string/replace.hpp>
2827
29namespace sc = unity::scopes;28namespace sc = unity::scopes;
@@ -40,122 +39,87 @@
40void Scope::stop() {39void Scope::stop() {
41}40}
4241
43sc::Variant to_variant(Json::Value const& value)
44{
45 switch (value.type())
46 {
47 case Json::ValueType::nullValue:
48 return sc::Variant::null();
49 case Json::ValueType::arrayValue:
50 {
51 sc::VariantArray arr;
52 for (unsigned int i=0; i<value.size(); ++i)
53 {
54 arr.push_back(to_variant(value[i]));
55 }
56 return sc::Variant(arr);
57 }
58 case Json::ValueType::objectValue:
59 {
60 sc::VariantMap var;
61 for (auto const& m: value.getMemberNames())
62 {
63 var[m] = to_variant(value[m]);
64 }
65 return sc::Variant(var);
66 }
67 case Json::ValueType::stringValue:
68 return sc::Variant(value.asString());
69 case Json::ValueType::intValue:
70 case Json::ValueType::uintValue:
71 // this can throw std::runtime_error from jsoncpp if unsigned int to int conversion is not possible
72 if (value.isInt())
73 {
74 return sc::Variant(value.asInt());
75 }
76 return sc::Variant(static_cast<int64_t>(value.asInt64()));
77 case Json::ValueType::realValue:
78 return sc::Variant(value.asDouble());
79 case Json::ValueType::booleanValue:
80 return sc::Variant(value.asBool());
81 default:
82 {
83 throw std::runtime_error("Unsupported json type " + std::to_string(static_cast<int>(value.type())));
84 }
85 }
86}
87
88void Scope::load_config(const std::string& scope_id)42void Scope::load_config(const std::string& scope_id)
89{43{
90 Json::Value root;
91 const std::string cfgfile = scope_directory() + "/" + scope_id + ".json";44 const std::string cfgfile = scope_directory() + "/" + scope_id + ".json";
92 Json::Reader reader;45
93 Json::FastWriter writer; // used to create renderer string in json format46 ifstream jsonfile(cfgfile);
94 std::ifstream file(cfgfile);47 stringstream buffer;
95 reader.parse(file, root);48 buffer << jsonfile.rdbuf();
96 if (root.isObject()) {49
97 if (root.isMember("categories")) {50 auto root = sc::Variant::deserialize_json(buffer.str());
98 auto const catsArray = root["categories"];51
99 if (catsArray.isArray()) {52 if (root.which() == sc::Variant::Dict) {
53 auto const rootDict = root.get_dict();
54 auto it = rootDict.find("categories");
55 if (it != rootDict.end()) {
56 if (it->second.which() == sc::Variant::Array) {
57 auto const catsArray = it->second.get_array();
100 for (unsigned i = 0; i<catsArray.size(); i++) {58 for (unsigned i = 0; i<catsArray.size(); i++) {
101 auto const& catsDict = catsArray[i];59 if (catsArray[i].which() != sc::Variant::Dict) {
102 if (!catsDict.isObject()) {60 throw std::runtime_error("The element of 'categories' array is not an object, scope " + scope_id);
103 throw std::runtime_error("'categories' element is not an object, scope " + scope_id);
104 }61 }
62 auto const catsDict = catsArray[i].get_dict();
10563
106 CategoryDefinition cat_def;64 CategoryDefinition cat_def;
10765
108 if (catsDict.isMember("id")) {66 it = catsDict.find("id");
109 cat_def.id = catsDict["id"].asString();67 if (it != catsDict.end()) {
68 cat_def.id = it->second.get_string();
110 } else {69 } else {
111 throw std::runtime_error("Missing category id, scope " + scope_id);70 throw std::runtime_error("Missing category id, scope " + scope_id);
112 }71 }
113 if (catsDict.isMember("name")) {72 it = catsDict.find("name");
114 cat_def.name = catsDict["name"].asString();73 if (it != catsDict.end()) {
74 cat_def.name = it->second.get_string();
115 } else {75 } else {
116 throw std::runtime_error("Missing category name, scope " + scope_id);76 throw std::runtime_error("Missing category name, scope " + scope_id);
117 }77 }
118 if (catsDict.isMember("link")) {78 it = catsDict.find("link");
119 cat_def.link = catsDict["link"].asBool();79 if (it != catsDict.end()) {
80 cat_def.link = it->second.get_bool();
120 }81 }
121 if (catsDict.isMember("renderer")) {82 it = catsDict.find("renderer");
122 auto rendererObj = catsDict["renderer"];83 if (it != catsDict.end()) {
123 if (!rendererObj.isObject()) {84 if (it->second.which() != sc::Variant::Dict) {
124 throw std::runtime_error("'renderer' element is not an object, scope " + scope_id);85 throw std::runtime_error("'renderer' element is not an object, scope " + scope_id);
125 }86 }
126 cat_def.renderer = writer.write(rendererObj);87 cat_def.renderer = it->second.serialize_json();
127 } else {88 } else {
128 throw std::runtime_error("Missing renderer definition, scope " + scope_id);89 throw std::runtime_error("Missing renderer definition, scope " + scope_id);
129 }90 }
130 if (catsDict.isMember("results")) {91 it = catsDict.find("results");
131 auto resultsObj = catsDict["results"];92 if (it != catsDict.end()) {
132 if (!resultsObj.isObject()) {93 if (it->second.which() != sc::Variant::Dict) {
133 throw std::runtime_error("'results' element is not an object, scope " + scope_id);94 throw std::runtime_error("'results' element is not an object, scope " + scope_id);
134 }95 }
135 if (resultsObj.isMember("count")) {96 auto const resultsObj = it->second.get_dict();
136 cat_def.result_count = resultsObj["count"].asInt();97 it = resultsObj.find("count");
98 if (it != resultsObj.end()) {
99 cat_def.result_count = it->second.get_int();
137 } else {100 } else {
138 throw std::runtime_error("Missing result count, scope " + scope_id + ", category " + cat_def.id);101 throw std::runtime_error("Missing result count, scope " + scope_id + ", category " + cat_def.id);
139 }102 }
140 if (resultsObj.isMember("random_text_file")) {103 it = resultsObj.find("random_text_file");
141 cat_def.random_text_file = resultsObj["random_text_file"].asString();104 if (it != resultsObj.end()) {
105 cat_def.random_text_file = it->second.get_string();
142 boost::replace_all(cat_def.random_text_file, "%SCOPE_DIR%", scope_dir_);106 boost::replace_all(cat_def.random_text_file, "%SCOPE_DIR%", scope_dir_);
143 }107 }
144 if (!resultsObj.isMember("result_template")) {108 it = resultsObj.find("result_template");
109 if (it == resultsObj.end()) {
145 throw std::runtime_error("Missing icon uri pattern, scope " + scope_id + ", category " + cat_def.id);110 throw std::runtime_error("Missing icon uri pattern, scope " + scope_id + ", category " + cat_def.id);
146 }111 }
147112
148 // Handle result_template113 // Handle result_template
149 auto resultTemplObj = resultsObj["result_template"];114 if (it->second.which() != sc::Variant::Dict) {
150 if (!resultTemplObj.isObject()) {
151 throw std::runtime_error("'result_template' element is not an object, scope " + scope_id);115 throw std::runtime_error("'result_template' element is not an object, scope " + scope_id);
152 }116 }
153 for (auto const& mem: resultTemplObj.getMemberNames()) {117 auto const resultTemplObj = it->second.get_dict();
154 cat_def.result_values.push_back(std::make_pair<>(mem, to_variant(resultTemplObj[mem])));118 for (auto const& mem: resultTemplObj) {
119 cat_def.result_values.push_back(std::make_pair<>(mem.first, mem.second));
155 }120 }
156 } // it's ok to have no results121 } // it's ok to have no results
157 category_defs_.push_back(cat_def);122 category_defs_.push_back(cat_def);
158
159 } // end of category array processing loop123 } // end of category array processing loop
160 } else {124 } else {
161 throw std::runtime_error("'categories' element is not an array, scope " + scope_id);125 throw std::runtime_error("'categories' element is not an array, scope " + scope_id);

Subscribers

People subscribed via source and target branches

to all changes: