rapidjson-writable
API to rapidjson that supports writing data asynchronously.
Last updated 3 years ago by thlorenz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install rapidjson-writable 
SYNC missed versions from official npm registry.

rapidjson-writable build status

API to rapidjson that supports writing data asynchronously. Uses libuv to block the parser thread while waiting for data.

#include <iostream>
#include <fstream>
#include <unistd.h>

#define CHUNK_SIZE 64

#include "test_assert.h"  // see test/test_assert.h
#include "util.h"         // see test/util.h
#include "rapidjson_writable.h"

using namespace rapidjson_writable;

class DumpWritable : public RapidjsonWritable {
  void onparserFailure(rapidjson::Reader& reader) {
    printParserFailure(reader);
  }

  void onparsedToken(SaxHandler& handler) {
    printParsedToken(handler);
  }

  void onparseComplete() {
    fprintf(stdout, "OK\n");
  }
};

void write_chunks(std::istream& stream, RapidjsonWritable& writable) {
  do {
    std::vector<char> buffer (CHUNK_SIZE, 0);
    stream.read(buffer.data(), buffer.size());
    writable.write(*buffer.data(), buffer.size());
    usleep(1E5);
  } while(!stream.eof());

  // wait for parser thread to finish before exiting
  writable.wait();
}

int main(int argc, const char* argv[]) {
  DumpWritable writable;

  assert(!writable.initialized());
  const TestWritableResult* r = writable.init(TestWritableResult::OK());
  if (r->hasError) {
    fprintf(stderr, "Encountered writable init error: %s\n", r->errorMsg);
    delete r;
    return 1;
  }
  delete r;

  assert(writable.initialized());

  const char* file = argv[1];
  std::ifstream ifs(file);
  write_chunks(ifs, writable);
}

Testing Manually

Dump Tokens

make clean && make test_dump

./bin/test_dump file.json
# or
cat file.json | ./bin/test_dump

Current Tags

  • 1.2.1                                ...           latest (3 years ago)

4 Versions

  • 1.2.1                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |