Skip to content

Instantly share code, notes, and snippets.

@killgxlin
Created October 9, 2014 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killgxlin/7a112e2e593e8f168453 to your computer and use it in GitHub Desktop.
Save killgxlin/7a112e2e593e8f168453 to your computer and use it in GitHub Desktop.
int
write_json(int wfd, Document &doc) {
auto &al = doc.GetAllocator();
GenericStringBuffer<UTF8<>, Document::AllocatorType> sb(&al);
PrettyWriter<GenericStringBuffer<UTF8<>, Document::AllocatorType>, UTF8<>, UTF8<>, Document::AllocatorType > sbwr(sb, &al);
doc.Accept(sbwr);
return strndup(sb.GetString(), sb.GetSize());
uint32_t size = sb.GetSize();
assert(write(wfd, &size, sizeof(size)) == sizeof(size));
assert(write(wfd, sb.GetString(), size) == sb.GetSize());
return 0;
}
int
read_json(int rfd, Document &doc) {
uint32_t size;
assert(read(rfd, &size, sizeof(size)) == sizeof(size));
char* buffer = malloc(size);
assert(read(rfd, buffer, size) == size);
assert(!doc.Parse(buffer).HasParseError());
return 0;
}
static void
my_test2() {
int fds[2];
assert(pipe(fds) == 0);
Document doc;
assert(write_json(fds[1], doc) == 0);
assert(read_json(fds[0], doc) == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment