Add online content repository
[oweals/minetest.git] / src / convert_json.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include <vector>
21 #include <iostream>
22 #include <sstream>
23
24 #include "convert_json.h"
25 #include "content/mods.h"
26 #include "config.h"
27 #include "log.h"
28 #include "settings.h"
29 #include "httpfetch.h"
30 #include "porting.h"
31
32 Json::Value fetchJsonValue(const std::string &url,
33                 std::vector<std::string> *extra_headers)
34 {
35         HTTPFetchRequest fetch_request;
36         HTTPFetchResult fetch_result;
37         fetch_request.url = url;
38         fetch_request.caller = HTTPFETCH_SYNC;
39
40         if (extra_headers != NULL)
41                 fetch_request.extra_headers = *extra_headers;
42
43         httpfetch_sync(fetch_request, fetch_result);
44
45         if (!fetch_result.succeeded) {
46                 return Json::Value();
47         }
48         Json::Value root;
49         std::istringstream stream(fetch_result.data);
50
51         Json::CharReaderBuilder builder;
52         builder.settings_["collectComments"] = false;
53         std::string errs;
54
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;
62                 } else {
63                         errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;
64                 }
65                 return Json::Value();
66         }
67
68         return root;
69 }
70
71 std::string fastWriteJson(const Json::Value &value)
72 {
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);
78         return oss.str();
79 }