Skip to content

Commit

Permalink
json2msgpack: Added support for continuous JSON documents
Browse files Browse the repository at this point in the history
  • Loading branch information
zeil committed Nov 6, 2018
1 parent f5ea038 commit d58495a
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/json2msgpack.cpp
Expand Up @@ -206,23 +206,6 @@ static bool write_value(options_t* options, Value& value, mpack_writer_t* writer
return mpack_writer_error(writer) == mpack_ok;
}

static bool output(options_t* options, Document& document) {
mpack_writer_t writer;
if (options->out_filename != NULL)
mpack_writer_init_file(&writer, options->out_filename);
else
mpack_writer_init_stdfile(&writer, stdout, true);

write_value(options, document, &writer);

mpack_error_t error = mpack_writer_destroy(&writer);
if (error != mpack_ok) {
fprintf(stderr, "%s: error writing MessagePack: %s (%i)\n", options->command,
mpack_error_to_string(error), (int)error);
}
return true;
}

static bool load_file(options_t* options, char** out_data, size_t* out_size) {
FILE* in_file;
if (options->in_filename) {
Expand Down Expand Up @@ -299,22 +282,48 @@ static bool convert(options_t* options) {
return false;

// The data has been null-terminated by load_file()
Document document;
if (options->lax)
document.ParseInsitu<kParseFullPrecisionFlag | kParseCommentsFlag | kParseTrailingCommasFlag>(data);
StringStream stream(data);

mpack_writer_t writer;
if (options->out_filename != NULL)
mpack_writer_init_file(&writer, options->out_filename);
else
document.ParseInsitu<kParseFullPrecisionFlag>(data);
mpack_writer_init_stdfile(&writer, stdout, true);

while (stream.Peek() != '\0') {
// skip space characters
while (isspace(stream.Peek()))
stream.Take();
if (stream.Peek() == '\0')
break;

if (document.HasParseError()) {
fprintf(stderr, "%s: error parsing JSON at offset %i:\n %s\n", options->command,
(int)document.GetErrorOffset(), GetParseError_En(document.GetParseError()));
Document document;
if (options->lax)
document.ParseStream<kParseStopWhenDoneFlag | kParseFullPrecisionFlag | kParseCommentsFlag | kParseTrailingCommasFlag>(stream);
else
document.ParseStream<kParseStopWhenDoneFlag | kParseFullPrecisionFlag>(stream);

if (document.HasParseError()) {
fprintf(stderr, "%s: error parsing JSON at offset %i:\n %s\n", options->command,
(int)document.GetErrorOffset(), GetParseError_En(document.GetParseError()));
mpack_writer_destroy(&writer);
free(data);
return false;
}

write_value(options, document, &writer);
}

mpack_error_t error = mpack_writer_destroy(&writer);
if (error != mpack_ok) {
fprintf(stderr, "%s: error writing MessagePack: %s (%i)\n", options->command,
mpack_error_to_string(error), (int)error);
free(data);
return false;
}

bool error = output(options, document);
free(data);
return error;
return true;
}

static void parse_min_bytes(options_t* options) {
Expand Down

0 comments on commit d58495a

Please sign in to comment.