condense.cpp 986 Bytes
Newer Older
1 2 3 4 5 6 7
// JSON condenser exmaple

// This example parses JSON text from stdin with validation, 
// and re-output the JSON content to stdout without whitespace.

#include "rapidjson/reader.h"
#include "rapidjson/writer.h"
8 9
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
10
#include "rapidjson/error/en.h"
11 12 13

using namespace rapidjson;

14
int main(int, char*[]) {
15 16
	// Prepare JSON reader and input stream.
	Reader reader;
17 18
	char readBuffer[65536];
	FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
19 20

	// Prepare JSON writer and output stream.
21 22 23
	char writeBuffer[65536];
	FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
	Writer<FileWriteStream> writer(os);
24 25

	// JSON reader parse from the input stream and let writer generate the output.
26
	if (!reader.Parse(is, writer)) {
27
		fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode()));
28 29 30 31 32
		return 1;
	}

	return 0;
}