Pagini recente » Cod sursa (job #1756428) | Cod sursa (job #2834657) | Cod sursa (job #1655830) | Cod sursa (job #1049342) | Cod sursa (job #1357547)
/**
* Converts JSON to CSV.
*/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<string, string> json_field;
typedef vector<json_field> json_dictionary;
/**
* Checks if a character is digit.
*
* @param ch
* @return `true` if the given character is digit, `false` otherwise.
*/
bool isdigit(char ch)
{
return ((ch >= '0') && (ch <= '9'));
}
class Json {
public:
void addDictionary(json_dictionary &entry, ostream &out);
void dump(ostream &out);
private:
vector<json_dictionary> data;
};
/**
* Basic JSON parser working with an array containing multiple dictionaries.
*/
class JsonParser {
public:
/**
* Parses the JSON data from the stream.
*
* @param s Source stream.
* @return
*/
Json* parse(istream &in, ostream &out);
/**
* Dumps the data in CSV format.
*
* @param s Destination stream.
*/
void dump(ostream &out);
private:
/**
* List of possible states of the parser.
* They are usually stored in a stack.
*/
enum State {
/** Initial state. */
STATE_NONE,
/** In array (expects entry). */
STATE_IN_ARRAY,
/** In dictionary (expects field). */
STATE_IN_DICT,
/** In dictionary key (expects string). */
STATE_IN_DICT_KEY,
/** In a dictionary value. */
STATE_IN_DICT_VALUE,
/** In dictionary value (expects string). */
STATE_IN_DICT_VALUE_STR,
/** In dictionary value (expects integer). */
STATE_IN_DICT_VALUE_INT,
};
/** Stores entries. */
vector<json_dictionary> data;
};
int main()
{
ifstream fin("convertor.in");
ofstream fout("convertor.out");
JsonParser jp;
jp.parse(fin, fout);
//jp.dump(fout);
return 0;
}
Json* JsonParser::parse(istream &in, ostream &out)
{
Json *json = new Json;
json_dictionary entry;
char key[128], value[1024];
int keyLen = 0, valueLen = 0;
char ch;
stack<State> state;
state.push(STATE_NONE);
while (in >> noskipws >> ch) {
State current = state.top();
if (current == STATE_NONE) {
if (ch == '[') {
//data.clear();
state.push(STATE_IN_ARRAY);
}
} else if (current == STATE_IN_ARRAY) {
if (ch == ']') {
state.pop();
} else if (ch == '{') {
entry.clear();
state.push(STATE_IN_DICT);
} else if (ch == ',') {
// TODO: New entry is coming.
}
} else if (current == STATE_IN_DICT) {
if (ch == '}') {
//data.push_back(entry);
json->addDictionary(entry, out);
state.pop();
} else if (ch == '"') {
keyLen = 0;
valueLen = 0;
state.push(STATE_IN_DICT_KEY);
} else if (ch == ':') {
state.push(STATE_IN_DICT_VALUE);
} else if (ch == ',') {
// TODO: New entry is coming.
}
} else if (current == STATE_IN_DICT_KEY) {
if (ch == '"') {
// TODO: Escaping.
state.pop();
} else {
key[keyLen++] = ch;
}
} else if (current == STATE_IN_DICT_VALUE) {
if (ch == '"') {
state.top() = STATE_IN_DICT_VALUE_STR;
} else if (isdigit(ch)) {
state.top() = STATE_IN_DICT_VALUE_INT;
value[valueLen++] = ch;
}
} else if (current == STATE_IN_DICT_VALUE_STR) {
if (ch == '"') {
key[keyLen] = value[valueLen] = 0;
entry.push_back(json_field(key, value));
state.pop();
} else {
value[valueLen++] = ch;
}
} else if (current == STATE_IN_DICT_VALUE_INT) {
if (isdigit(ch)) {
value[valueLen++] = ch;
} else if (ch == '}') {
key[keyLen] = value[valueLen] = 0;
entry.push_back(json_field(key, value));
json->addDictionary(entry, out);
state.pop();
state.pop(); // popping STATE_IN_DICT too
} else if (ch == ',') {
key[keyLen] = value[valueLen] = 0;
entry.push_back(json_field(key, value));
state.pop();
}
}
}
return json; // TODO: Return `NULL` on failure.
}
void Json::addDictionary(json_dictionary &entry, ostream &out)
{
static bool isFirst = true;
if (isFirst) {
// Builds the header.
vector<string> header;
for (json_dictionary::iterator field = entry.begin(); field != entry.end(); ++field) {
if (find(header.begin(), header.end(), field->first) == header.end()) {
header.push_back(field->first);
}
}
// Prints the header.
for (vector<string>::iterator it = header.begin(); it != header.end(); ++it) {
out << *it << ',';
}
out << '\n';
isFirst = false;
}
// Prints data.
for (json_dictionary::iterator field = entry.begin(); field != entry.end(); ++field) {
out << field->second << ',';
}
out << '\n';
}
void Json::dump(ostream &out)
{
// Builds the header (using only the first entry).
vector<string> header;
for (json_dictionary::iterator field = data.begin()->begin(); field != data.begin()->end(); ++field) {
if (find(header.begin(), header.end(), field->first) == header.end()) {
header.push_back(field->first);
}
}
// Prints the header.
for (vector<string>::iterator it = header.begin(); it != header.end(); ++it) {
out << *it << ',';
}
out << '\n';
// Prints data.
for (vector<json_dictionary>::iterator entry = data.begin(); entry != data.end(); ++entry) {
// TODO: Print fields in order (@see `header`).
for (json_dictionary::iterator field = entry->begin(); field != entry->end(); ++field) {
out << field->second << ',';
}
out << '\n';
}
}