Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for Parsing JSON to Custom Data Structure #1787

Open
lano1106 opened this issue Oct 9, 2020 · 0 comments
Open

Suggestion for Parsing JSON to Custom Data Structure #1787

lano1106 opened this issue Oct 9, 2020 · 0 comments

Comments

@lano1106
Copy link

lano1106 commented Oct 9, 2020

I'm currently using the suggested pattern in the lib documentation.

What I am trying to achieve is to construct C++ objects from JSON strings. The strategy that I'm deploying is very similar to what is suggested in the lib doc.

I pass a reader and an input stream object to a class constructor and I'm parsing the JSON by passing the constructed object reference as the handler.

One difficulty that I have is that my handler class is now littered with a bunch of data members that their only purpose is for providing a parsing context. Past the object construction, they aren't needed anymore...

I was thinking of 2 solutions:

  1. Have rapidjson pass a parsing context to the various handler methods... (similar to what expat offers)
  2. Have a friend builder class.

I think #2 has the most potential... Maybe add this idea to the doc to help others... Here is the concept:

class A { public: A(Reader r, InputStream is) { BuilderHandler handler(*this); r.parse(is, handler); } private: class BuilderHandler { BuilderHandler(A &a) : a_(a) {} private: // bunch of context parsing variables A &a_; }; friend class BuilderHandler; };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant