Skip to content

Does Cereal support streamed JSON deserialization? #112

@dkorolev

Description

@dkorolev

Hello,

Is it possible to have Cereal output each input object as it has been parsed from the stream?

I have a code snippet that looks like this:

class X {
  public:
    X() = default;
    std::string s;

  private:
    friend class cereal::access;
    template<class A> void serialize(A& ar) {
        ar(CEREAL_NVP(s));
    }
};
// ...
{
    cereal::JSONInputArchive ar(std::cin);
    X x;
    while (true) {
        ar(x);
        std::cout << x.s << std::endl;
    }
}

(Full code: https://github.com/dkorolev/sandbox_cereal/blob/233396ef12dcb7feda88aa3e0efe6d8623353da3/standalone.cc)

This code snippet can parse a single piece of input:

echo '{"value0":{"s":"test"}}' | ./build/standalone

It can parse multiple inputs as well:

echo '{"value0":{"s":"this"},"value1":{"s":"test"},"value2":{"s":"passes"}}' | ./build/standalone

However:

1. All the output happens after all the input is processed. I have been unable to have it process streaming data.

In other words, this does not output this test before the whole code runs:

(echo '{"value0":{"s":"this"},"value1":{"s":"test"},' ; sleep 5 ; echo '"value2":{"s":"passes"}}') | ./build/standalone

2. The code crashes with the exception at the end.

terminate called after throwing an instance of 'cereal::RapidJSONException' what(): rapidjson internal assertion failure: IsObject() Aborted (core dumped)

I am using Cereal 1.0.0.

Any suggestions?

Thanks,
Dima

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions