Comments (10)
I have trouble picturing a use case where you know so little about what you are parsing you don't even know if you're expecting an object or an array. Usually there are APIs with well-defined message formats, and at the very least you know whether the top-level entity is an object or an array.
After all, a rather unusual JSON library like this one is admittedly a pain in the neck for parsing entities you know little about. If you have such an elaborate use case, and you need flexibility, maybe you could go for a mainstream library that builds a model in memory, that you can query and manipulate as you please.
Maybe I could come up with a better design. Do you have a patch in mind (or in a branch)?
from minijson_reader.
I have trouble picturing a use case where you know so little about what you are parsing you don't even know if you're expecting an object or an array. Usually there are APIs with well-defined message formats, and at the very least you know whether the top-level entity is an object or an array.
I can point you to an affected API in the wild.
It's a Canadian university's API for querying information about its courses. There is a call that gives you a list of "orgs", or departments, in the university:
and then you can make a call to query information about each org's list of courses:
https://timetable.iit.artsci.utoronto.ca/api/courses?org=ANA
Now, most of these calls return an object, but if there are no courses, and empty array is returned:
https://timetable.iit.artsci.utoronto.ca/api/courses?org=COL
I didn't design this API, and I probably wouldn't do it this way if I did, but I have to consume it.
Maybe I could come up with a better design. Do you have a patch in mind (or in a branch)?
I had in mind a simple function toplevel_value_type()
that returns the same thing that value::type()
would return in a nested context.
from minijson_reader.
Oh my, there are some strange APIs out there! 😅
But I didn't quite nail mine either. So, let me make an attempt at the change you requested: thanks for the suggestion.
from minijson_reader.
I invented around your idea... I came up with a parse_message()
function that returns a value
, and then, depending on the type()
of the value, you call parse_object()
or parse_array()
on the same context (which has been changed!).
using namespace minijson;
char buf[] = "\n{\"foo\": \"bar\"}";
buffer_context ctx(buf, sizeof(buf) - 1);
value top = parse_message(ctx);
if (top.type() == Array)
{
throw std::runtime_error("fail!");
}
else if (top.type() == Object)
{
parse_object(ctx, [&](const char* k, value v)
{
dispatch (k)
<<"foo">> [&]{ std::cout << v.as_string() << std::endl; };
});
}
Some code is available in the parse_message
branch, although I didn't test it carefully and it is not fit for production.
What do you think?
from minijson_reader.
Thanks for implementing this! I haven't had a chance to try it yet (traveling at the moment) but I will as soon as I do.
from minijson_reader.
I have tried parse_message
, and it is working well. Thanks!
from minijson_reader.
OK, when I have some time I'll test it properly and merge it.
from minijson_reader.
Hi there! Any ETA on merging this change into the master branch?
from minijson_reader.
While I am not really proud of this library 😅 and I would write it very differently if I were to start from scratch, nevertheless I am trying to tidy up a couple of things (number parsing has top priority). Are you still interested in this parse_message
thing, @HighCommander4 ? If not I will be closing this issue.
from minijson_reader.
@giacomodrago I've stopped maintaining the software that consumes the mentioned API a couple of years ago. The latest version does use your parse_message
branch, and I think it's a sensible capability to have, but please feel free to prioritize accordingly and not spend more effort on this on my account.
from minijson_reader.
Related Issues (15)
- Small compilation errors HOT 4
- minijson_reader truncates strings containing a Unicode null character (\u0000) HOT 2
- Travis CI tests not running any more
- Rework dispatch HOT 3
- Allow user-provided specializations to convert values
- parse_object issue HOT 2
- minijson_reader does not reject unescaped control characters in strings HOT 2
- C++17 features HOT 1
- build errors on OSX
- Dispatcher example in README does not compile
- Rework number parsing HOT 8
- License change request HOT 3
- Unable to read arrays HOT 1
- minijson_reader does not distinguish between an unparsable value and a value parsable as zero HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from minijson_reader.