Cod sursa(job #1357325)

Utilizator MrWhiteDELETEME MrWhite Data 23 februarie 2015 21:23:14
Problema Convertor Scor 0
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 4.16 kb
/**
 * Converts JSON to CSV.
 */

#include <algorithm>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef unordered_map<string, string> json_entry;
typedef pair<string, string> json_field;

/**
 * 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'));
}

/**
 * Basic JSON parser working with an array containing multiple dictionaries.
 */

class JsonParser {
public: 

	/**
	 * Parses the JSON data from the stream.
	 *
	 * @param s Source stream. 
	 */
	bool parse(istream &s);

	/**
	 * Dumps the data in CSV format.
	 *
	 * @param s Destination stream.
	 */
	void dump(ostream &s);

private:

	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, expects a value. */
		STATE_IN_DICT_VALUE_TMP,

		/** In dictionary value (expects string). */
		STATE_IN_DICT_VALUE_STR,

		/** In dictionary value (expects integer). */
		STATE_IN_DICT_VALUE_INT,
	};

	/** Stores entries. */
	vector<json_entry> data;
};

int main()
{
	ifstream fin("convertor.in");
	ofstream fout("convertor.out");
	JsonParser jp;
	jp.parse(fin);
	jp.dump(fout);
	return 0;
}

bool JsonParser::parse(istream &s)
{
	json_entry entry;
	json_field field;
	char ch;
	stack<State> state;
	state.push(STATE_NONE);
	while (s >> noskipws >> ch) {
		if (state.top() == STATE_NONE) {
			if (ch == '[') {
				data.clear();
				state.push(STATE_IN_ARRAY);
			} 
		} else if (state.top() == 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 (state.top() == STATE_IN_DICT) {
			if (ch == '}') {
				data.push_back(entry);
				state.pop();
			} else if (ch == '"') {
				field.first.clear();
				field.second.clear();
				state.push(STATE_IN_DICT_KEY);
			} else if (ch == ':') {
				state.push(STATE_IN_DICT_VALUE_TMP);
			} else if (ch == ',') {
				// TODO: New entry is coming.
			}
		} else if (state.top() == STATE_IN_DICT_KEY) {
			if (ch == '"') {
				// TODO: Escaping.
				state.pop();
			} else {
				field.first += ch;
			}
		} else if (state.top() == STATE_IN_DICT_VALUE_TMP) {
			if (ch == '"') {
				state.pop();
				state.push(STATE_IN_DICT_VALUE_STR);
			} else if (isdigit(ch)) {
				state.pop();
				state.push(STATE_IN_DICT_VALUE_INT);
				field.second += ch;
			}
		} else if (state.top() == STATE_IN_DICT_VALUE_STR) {
			if (ch == '"') {
				entry.insert(field);
				state.pop();
			} else {
				field.second += ch;
			}
		} else if (state.top() == STATE_IN_DICT_VALUE_INT) {
			if (isdigit(ch)) {
				field.second += ch;
			} else if (ch == ',') {
				entry.insert(field);
				state.pop();
			} else if (ch == '}') {
				entry.insert(field);
				data.push_back(entry);
				state.pop();
				state.pop(); // popping STATE_IN_DICT too
			}
		}
	}
	return true; // TODO: Return false on failure.
}

void JsonParser::dump(ostream &s)
{
	// Builds the header.
	vector<string> header;
	for (vector<json_entry>::iterator entry = data.begin(); entry != data.end(); ++entry) {
		for (json_entry::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) {
		s << *it << ",";
	}
	s << "\n";
	// Prints data.
	for (vector<json_entry>::iterator entry = data.begin(); entry != data.end(); ++entry) {
		// TODO: Print fields in order (@see `header`).
		for (json_entry::iterator field = entry->begin(); field != entry->end(); ++field) {
			s << field->second << ",";
		}
		s << "\n";
	}
}