aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/input/json-parsing/json_helper.cpp
blob: 927787cf18b934ef4b2a30d619b9b0f59347a111 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Created by thecharlesblake on 11/13/17.
//

#include <fstream>
#include <sstream>
#include <rapidjson/stringbuffer.h>

#include "json_helper.h"
#include "../../output/log_helper.h"

using namespace std;
using namespace rapidjson;

Document json_helper::get_file_json(const string& filename) {
    // Reads the file into a string
    std::ifstream ifstr(filename);
    std::stringstream buf;
    buf << ifstr.rdbuf();

    if (ifstr.fail()) {
        throw runtime_error("could not read file " + filename);
    } else {
        Document d;

        d.Parse(buf.str().c_str());
        if (d.HasParseError()) {
            throw runtime_error(filename + " not valid json");
        } else {
            return d;
        }
    }
}

void json_helper::json_parse_err(const string& msg) {
    string err_msg = "Error in JSON doc: " + msg;
    LOG_DEBUG(err_msg);
    throw runtime_error(err_msg);
}

void json_helper::json_parse_warning(const string& msg) {
    LOG_WARNING("Error in JSON doc: " + msg);
}

const string json_helper::schema_err_str(const SchemaValidator& validator) {
    string ret = "Input JSON failed to match the required schema. Schema Validator Error = ";

    StringBuffer sb;
    validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
    ret += validator.GetInvalidSchemaKeyword();
    ret += ": ";

    sb.Clear();
    validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);

    ret += sb.GetString();
    return ret;
}