3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "convert_json.h"
25 #include "content/mods.h"
29 #include "httpfetch.h"
32 Json::Value
fetchJsonValue(const std::string
&url
,
33 std::vector
<std::string
> *extra_headers
)
35 HTTPFetchRequest fetch_request
;
36 HTTPFetchResult fetch_result
;
37 fetch_request
.url
= url
;
38 fetch_request
.caller
= HTTPFETCH_SYNC
;
40 if (extra_headers
!= NULL
)
41 fetch_request
.extra_headers
= *extra_headers
;
43 httpfetch_sync(fetch_request
, fetch_result
);
45 if (!fetch_result
.succeeded
) {
49 std::istringstream
stream(fetch_result
.data
);
51 Json::CharReaderBuilder builder
;
52 builder
.settings_
["collectComments"] = false;
55 if (!Json::parseFromStream(builder
, stream
, &root
, &errs
)) {
56 errorstream
<< "URL: " << url
<< std::endl
;
57 errorstream
<< "Failed to parse json data " << errs
<< std::endl
;
58 if (fetch_result
.data
.size() > 100) {
59 errorstream
<< "Data (" << fetch_result
.data
.size()
60 << " bytes) printed to warningstream." << std::endl
;
61 warningstream
<< "data: \"" << fetch_result
.data
<< "\"" << std::endl
;
63 errorstream
<< "data: \"" << fetch_result
.data
<< "\"" << std::endl
;
71 std::string
fastWriteJson(const Json::Value
&value
)
73 std::ostringstream oss
;
74 Json::StreamWriterBuilder builder
;
75 builder
["indentation"] = "";
76 std::unique_ptr
<Json::StreamWriter
> writer(builder
.newStreamWriter());
77 writer
->write(value
, &oss
);