aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/input/json-parsing/json_helper.cpp
blob: 3e6b99c94da6e80a4af85a2b5ad254d01df31f80 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Created by thecharlesblake on 11/13/17.
//

#include <fstream>
#include <sstream>

#include "json_helper.h"
#include "../../../../../lib/rapidjson/stringbuffer.h"
#include "../../output/log_helper.h"

using namespace std;
using namespace rapidjson;

using boost::property_tree::ptree;
using boost::property_tree::write_json;

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;
}

void json_helper::print_game_state_as_json(const game_state& gs) {
    ptree pt;
    if (!gs.tableau_piles.empty()) pt.add_child("tableau piles", piles_to_ptree(gs, gs.tableau_piles));
    if (!gs.cells.empty()) pt.add_child("cells", piles_to_ptree(gs, gs.cells));
    if (!gs.reserve.empty()) pt.add_child("reserve", piles_to_ptree(gs, gs.reserve));
    if (!gs.foundations.empty()) pt.add_child("foundations", piles_to_ptree(gs, gs.foundations));
    if (!gs.sequences.empty()) pt.add_child("sequences", piles_to_ptree(gs, gs.sequences));
    if (!gs.accordion.empty()) pt.add_child("accordion", piles_to_ptree(gs, gs.accordion));
    if (gs.rules.stock_size > 0) {
        pt.add_child("stock", pile_to_ptree(gs.piles[gs.stock]));
        if (gs.rules.stock_deal_t == sol_rules::stock_deal_type::WASTE) {
            pt.add_child("waste", pile_to_ptree(gs.piles[gs.waste]));
        }
    }
    if (gs.rules.hole) pt.add_child("hole", pile_to_ptree(gs.piles[gs.hole]));

    write_json (cout, pt);
}

ptree json_helper::piles_to_ptree(const game_state& gs, const std::list<pile::ref>& piles) {
    ptree pt;
    for (pile::ref pr : piles) {
        pt.push_back(make_pair("", pile_to_ptree(gs.piles[pr])));
    }
    return pt;
}

ptree json_helper::piles_to_ptree(const game_state& gs, const std::vector<pile::ref>& piles) {
    ptree pt;
    for (pile::ref pr : piles) {
        pt.push_back(make_pair("", pile_to_ptree(gs.piles[pr])));
    }
    return pt;
}

ptree json_helper::pile_to_ptree(const pile& pile) {
    ptree pt;
    for (pile::size_type i = pile.size(); i-->0; ) {
        pt.push_back(make_pair("", card_to_ptree(pile[i])));
    }
    return pt;
}

ptree json_helper::card_to_ptree(const card& card) {
    ptree pt;
    pt.put("", card.to_string());
    return pt;
}