Giter Club home page Giter Club logo

Comments (10)

giacomodrago avatar giacomodrago commented on June 5, 2024

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.

HighCommander4 avatar HighCommander4 commented on June 5, 2024

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:

https://timetable.iit.artsci.utoronto.ca/api/orgs

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.

giacomodrago avatar giacomodrago commented on June 5, 2024

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.

giacomodrago avatar giacomodrago commented on June 5, 2024

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.

HighCommander4 avatar HighCommander4 commented on June 5, 2024

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.

HighCommander4 avatar HighCommander4 commented on June 5, 2024

I have tried parse_message, and it is working well. Thanks!

from minijson_reader.

giacomodrago avatar giacomodrago commented on June 5, 2024

OK, when I have some time I'll test it properly and merge it.

from minijson_reader.

HighCommander4 avatar HighCommander4 commented on June 5, 2024

Hi there! Any ETA on merging this change into the master branch?

from minijson_reader.

giacomodrago avatar giacomodrago commented on June 5, 2024

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.

HighCommander4 avatar HighCommander4 commented on June 5, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.