json icon indicating copy to clipboard operation
json copied to clipboard

No simple way to parse from std::istream

Open grisumbras opened this issue 2 years ago • 4 comments

There should be a quick and convenient way to parse JSON from std::istream. E.g.

std::ifstream f("path/to/file");
auto jv = json::parse(f);

grisumbras avatar Sep 30 '21 16:09 grisumbras

there should be parse_istream and there should even be parse_file that takes the file name directly

Well, parse_istream can just be a parse overload.

vinniefalco avatar Oct 02 '21 16:10 vinniefalco

We have operator<< for serializing but no operator>> for parsing

It's be great to have

std::istringstream iss{"[1,2,3,4,5]"};
value jv;
iss >> jv;

prince-chrismc avatar Oct 09 '21 03:10 prince-chrismc

@prince-chrismc I agree! Would you like to try your hand at it? Shouldn't be too hard...

vinniefalco avatar Oct 09 '21 03:10 vinniefalco

I have a blueprint for the fix. I originally wanted to make the streambuffer to put all input into parser as soon as possible which would work OK with something like istream >> &json_buffer. But then I realized that

std::copy(
  std::istreambuf_iterator<char>(istream),
  std::istreambuf_iterator<char>(),
  std::ostreambuf_iterparsing(&json_buffer))

will push input into parser one byte a time, which is very ineffective. So, my approach has to be changed to have a proper buffer in the streambuf.

grisumbras avatar Oct 10 '21 09:10 grisumbras