condense.cpp 1.02 KB
Newer Older
Milo Yip's avatar
Milo Yip committed
1
// JSON condenser example
2 3 4 5 6 7

// 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 17 18
    // Prepare JSON reader and input stream.
    Reader reader;
    char readBuffer[65536];
    FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
19

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

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

31
    return 0;
32
}