Merge lp:~stolowski/unity-scope-fake/no-jsoncpp into lp:unity-scope-fake
- no-jsoncpp
- Merge into trunk
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 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Marcus Tomlinson (community) | Approve | ||
Review via email: |
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.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'CMakeLists.txt' |
2 | --- CMakeLists.txt 2016-05-19 16:19:45 +0000 |
3 | +++ CMakeLists.txt 2016-06-14 08:56:05 +0000 |
4 | @@ -16,7 +16,6 @@ |
5 | include(GNUInstallDirs) |
6 | find_package(PkgConfig) |
7 | find_package(Boost COMPONENTS system filesystem REQUIRED) |
8 | -pkg_check_modules(JSONCPP jsoncpp REQUIRED) |
9 | |
10 | # Search for our dependencies |
11 | pkg_check_modules( |
12 | @@ -34,7 +33,6 @@ |
13 | "${CMAKE_SOURCE_DIR}/include" |
14 | ${SCOPE_INCLUDE_DIRS} |
15 | ${Boost_INCLUDE_DIRS} |
16 | - ${JSONCPP_INCLUDE_DIRS} |
17 | ) |
18 | |
19 | set(FAKESCOPE_EXE "${CMAKE_INSTALL_FULL_LIBDIR}/fake-scope/fakescope") |
20 | |
21 | === modified file 'debian/control' |
22 | --- debian/control 2016-05-24 08:44:51 +0000 |
23 | +++ debian/control 2016-06-14 08:56:05 +0000 |
24 | @@ -5,7 +5,6 @@ |
25 | Build-Depends: cmake (>= 3.0), |
26 | debhelper (>= 9), |
27 | libunity-scopes-dev (>= 1.0.3), |
28 | - libjsoncpp-dev, |
29 | libboost-dev, |
30 | libboost-filesystem-dev, |
31 | pkg-config, |
32 | |
33 | === modified file 'src/CMakeLists.txt' |
34 | --- src/CMakeLists.txt 2016-05-24 09:39:52 +0000 |
35 | +++ src/CMakeLists.txt 2016-06-14 08:56:05 +0000 |
36 | @@ -23,7 +23,6 @@ |
37 | fakescope |
38 | ${SCOPE_LIBRARIES} |
39 | ${Boost_LIBRARIES} |
40 | - ${JSONCPP_LIBRARIES} |
41 | ) |
42 | |
43 | install( |
44 | |
45 | === modified file 'src/scope.cpp' |
46 | --- src/scope.cpp 2016-05-24 10:35:09 +0000 |
47 | +++ src/scope.cpp 2016-06-14 08:56:05 +0000 |
48 | @@ -19,11 +19,10 @@ |
49 | #include "query.h" |
50 | #include "scope.h" |
51 | #include "preview.h" |
52 | -#include <json/reader.h> |
53 | -#include <json/writer.h> |
54 | |
55 | +#include <iostream> |
56 | #include <fstream> |
57 | -#include <iostream> |
58 | +#include <sstream> |
59 | #include <boost/algorithm/string/replace.hpp> |
60 | |
61 | namespace sc = unity::scopes; |
62 | @@ -40,122 +39,87 @@ |
63 | void Scope::stop() { |
64 | } |
65 | |
66 | -sc::Variant to_variant(Json::Value const& value) |
67 | -{ |
68 | - switch (value.type()) |
69 | - { |
70 | - case Json::ValueType::nullValue: |
71 | - return sc::Variant::null(); |
72 | - case Json::ValueType::arrayValue: |
73 | - { |
74 | - sc::VariantArray arr; |
75 | - for (unsigned int i=0; i<value.size(); ++i) |
76 | - { |
77 | - arr.push_back(to_variant(value[i])); |
78 | - } |
79 | - return sc::Variant(arr); |
80 | - } |
81 | - case Json::ValueType::objectValue: |
82 | - { |
83 | - sc::VariantMap var; |
84 | - for (auto const& m: value.getMemberNames()) |
85 | - { |
86 | - var[m] = to_variant(value[m]); |
87 | - } |
88 | - return sc::Variant(var); |
89 | - } |
90 | - case Json::ValueType::stringValue: |
91 | - return sc::Variant(value.asString()); |
92 | - case Json::ValueType::intValue: |
93 | - case Json::ValueType::uintValue: |
94 | - // this can throw std::runtime_error from jsoncpp if unsigned int to int conversion is not possible |
95 | - if (value.isInt()) |
96 | - { |
97 | - return sc::Variant(value.asInt()); |
98 | - } |
99 | - return sc::Variant(static_cast<int64_t>(value.asInt64())); |
100 | - case Json::ValueType::realValue: |
101 | - return sc::Variant(value.asDouble()); |
102 | - case Json::ValueType::booleanValue: |
103 | - return sc::Variant(value.asBool()); |
104 | - default: |
105 | - { |
106 | - throw std::runtime_error("Unsupported json type " + std::to_string(static_cast<int>(value.type()))); |
107 | - } |
108 | - } |
109 | -} |
110 | - |
111 | void Scope::load_config(const std::string& scope_id) |
112 | { |
113 | - Json::Value root; |
114 | const std::string cfgfile = scope_directory() + "/" + scope_id + ".json"; |
115 | - Json::Reader reader; |
116 | - Json::FastWriter writer; // used to create renderer string in json format |
117 | - std::ifstream file(cfgfile); |
118 | - reader.parse(file, root); |
119 | - if (root.isObject()) { |
120 | - if (root.isMember("categories")) { |
121 | - auto const catsArray = root["categories"]; |
122 | - if (catsArray.isArray()) { |
123 | + |
124 | + ifstream jsonfile(cfgfile); |
125 | + stringstream buffer; |
126 | + buffer << jsonfile.rdbuf(); |
127 | + |
128 | + auto root = sc::Variant::deserialize_json(buffer.str()); |
129 | + |
130 | + if (root.which() == sc::Variant::Dict) { |
131 | + auto const rootDict = root.get_dict(); |
132 | + auto it = rootDict.find("categories"); |
133 | + if (it != rootDict.end()) { |
134 | + if (it->second.which() == sc::Variant::Array) { |
135 | + auto const catsArray = it->second.get_array(); |
136 | for (unsigned i = 0; i<catsArray.size(); i++) { |
137 | - auto const& catsDict = catsArray[i]; |
138 | - if (!catsDict.isObject()) { |
139 | - throw std::runtime_error("'categories' element is not an object, scope " + scope_id); |
140 | + if (catsArray[i].which() != sc::Variant::Dict) { |
141 | + throw std::runtime_error("The element of 'categories' array is not an object, scope " + scope_id); |
142 | } |
143 | + auto const catsDict = catsArray[i].get_dict(); |
144 | |
145 | CategoryDefinition cat_def; |
146 | |
147 | - if (catsDict.isMember("id")) { |
148 | - cat_def.id = catsDict["id"].asString(); |
149 | + it = catsDict.find("id"); |
150 | + if (it != catsDict.end()) { |
151 | + cat_def.id = it->second.get_string(); |
152 | } else { |
153 | throw std::runtime_error("Missing category id, scope " + scope_id); |
154 | } |
155 | - if (catsDict.isMember("name")) { |
156 | - cat_def.name = catsDict["name"].asString(); |
157 | + it = catsDict.find("name"); |
158 | + if (it != catsDict.end()) { |
159 | + cat_def.name = it->second.get_string(); |
160 | } else { |
161 | throw std::runtime_error("Missing category name, scope " + scope_id); |
162 | } |
163 | - if (catsDict.isMember("link")) { |
164 | - cat_def.link = catsDict["link"].asBool(); |
165 | + it = catsDict.find("link"); |
166 | + if (it != catsDict.end()) { |
167 | + cat_def.link = it->second.get_bool(); |
168 | } |
169 | - if (catsDict.isMember("renderer")) { |
170 | - auto rendererObj = catsDict["renderer"]; |
171 | - if (!rendererObj.isObject()) { |
172 | + it = catsDict.find("renderer"); |
173 | + if (it != catsDict.end()) { |
174 | + if (it->second.which() != sc::Variant::Dict) { |
175 | throw std::runtime_error("'renderer' element is not an object, scope " + scope_id); |
176 | } |
177 | - cat_def.renderer = writer.write(rendererObj); |
178 | + cat_def.renderer = it->second.serialize_json(); |
179 | } else { |
180 | throw std::runtime_error("Missing renderer definition, scope " + scope_id); |
181 | } |
182 | - if (catsDict.isMember("results")) { |
183 | - auto resultsObj = catsDict["results"]; |
184 | - if (!resultsObj.isObject()) { |
185 | + it = catsDict.find("results"); |
186 | + if (it != catsDict.end()) { |
187 | + if (it->second.which() != sc::Variant::Dict) { |
188 | throw std::runtime_error("'results' element is not an object, scope " + scope_id); |
189 | } |
190 | - if (resultsObj.isMember("count")) { |
191 | - cat_def.result_count = resultsObj["count"].asInt(); |
192 | + auto const resultsObj = it->second.get_dict(); |
193 | + it = resultsObj.find("count"); |
194 | + if (it != resultsObj.end()) { |
195 | + cat_def.result_count = it->second.get_int(); |
196 | } else { |
197 | throw std::runtime_error("Missing result count, scope " + scope_id + ", category " + cat_def.id); |
198 | } |
199 | - if (resultsObj.isMember("random_text_file")) { |
200 | - cat_def.random_text_file = resultsObj["random_text_file"].asString(); |
201 | + it = resultsObj.find("random_text_file"); |
202 | + if (it != resultsObj.end()) { |
203 | + cat_def.random_text_file = it->second.get_string(); |
204 | boost::replace_all(cat_def.random_text_file, "%SCOPE_DIR%", scope_dir_); |
205 | } |
206 | - if (!resultsObj.isMember("result_template")) { |
207 | + it = resultsObj.find("result_template"); |
208 | + if (it == resultsObj.end()) { |
209 | throw std::runtime_error("Missing icon uri pattern, scope " + scope_id + ", category " + cat_def.id); |
210 | } |
211 | |
212 | // Handle result_template |
213 | - auto resultTemplObj = resultsObj["result_template"]; |
214 | - if (!resultTemplObj.isObject()) { |
215 | + if (it->second.which() != sc::Variant::Dict) { |
216 | throw std::runtime_error("'result_template' element is not an object, scope " + scope_id); |
217 | } |
218 | - for (auto const& mem: resultTemplObj.getMemberNames()) { |
219 | - cat_def.result_values.push_back(std::make_pair<>(mem, to_variant(resultTemplObj[mem]))); |
220 | + auto const resultTemplObj = it->second.get_dict(); |
221 | + for (auto const& mem: resultTemplObj) { |
222 | + cat_def.result_values.push_back(std::make_pair<>(mem.first, mem.second)); |
223 | } |
224 | } // it's ok to have no results |
225 | category_defs_.push_back(cat_def); |
226 | - |
227 | } // end of category array processing loop |
228 | } else { |
229 | throw std::runtime_error("'categories' element is not an array, scope " + scope_id); |
While scanning through the project I noticed some very small issues here and there. When you have time: https:/ /code.launchpad .net/~marcustom linson/ unity-scope- fake/small- fixes/+ merge/297428
Otherwise, this MP looks good! +1