-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_writer.cc
More file actions
64 lines (53 loc) · 1.65 KB
/
json_writer.cc
File metadata and controls
64 lines (53 loc) · 1.65 KB
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
//
// json_writer.cc
//
// Created by Erick Li on 04/11/19.
// Copyright © 2019 Erick Li. All rights reserved.
//
#include "json_writer.hh"
void write_to_json(std::string &jstring, std::string key, std::string value, int level) {
std::string output = "";
// Insert indention for the line.
for (int i = 0; i < level; i++) {
output += " "; // Use 4 spaces instead of a tab
}
// If it has a tag -- `"tag": `
if (0 != key.length()) {
output += ("\"" + key + "\": ");
}
output += value + "\n";
jstring += output;
}
std::string json_comma_remover(std::string &jstring) {
size_t jlen = jstring.length();
std::string ret = "";
int count_spaces = 0;
for (size_t i = 0; i < jlen; i++) {
if (COMMA == jstring[i] && NEWLINE == jstring[i + 1]) {
// std::clog << "Line " << count_line++ << ": " << count_spaces << std::endl;
int temp = i + 2;
int next_space = 0;
while (SPACE == jstring[temp]) {
next_space += 1;
temp += 1;
}
if (next_space == count_spaces) {
ret += COMMA;
}
count_spaces = next_space;
} else if (NEWLINE == jstring[i]) {
// std::clog << "Line " << count_line++ << ": " << count_spaces << std::endl;
int temp = i + 1;
int next_space = 0;
while (SPACE == jstring[temp]) {
next_space += 1;
temp += 1;
}
ret += NEWLINE;
count_spaces = next_space;
} else {
ret += jstring[i];
}
}
return ret;
}