Overview
Features
Download
Documentation
Community
Add-Ons & Services

Managing JSON input

Please post support and help requests here.

Managing JSON input

Postby mad93 » 07 Nov 2009, 19:31

Hi, i'm using the 'experimental' JSON parser of Poco, the one in the sandbox. The parser seems to work fine, but I've some doubts about how to process the input string.

In the example code is used JSONPrinter, JSONCondenser and JSONPrettyPrinter, that are classes created using JSONHandler. I understand that JSONHandler is an abstract class and you've to use a derived one with the implementation like the three ones said before. I've tried to follow the code but I can't.

In the JSONParser.h when you call the method parse(std::string) there is a call to the JSONHander.handleBegin() that is a virtual method (with no implementation in JSONHandler, it does nothing). I can't find any overwrite of this method in the classes like JSONPrinter. In this point is where i can't follow the code, so if someone can explain to me where it goes next i'll be thankfull.

Also i interpret from the code that the current handlers are only to output the entities and not to process them but well since i can follow the code i can't be sure.
mad93
 
Posts: 6
Joined: 04 Nov 2009, 22:13

Re: Managing JSON input

Postby alex » 07 Nov 2009, 21:54

mad93 wrote:In this point is where i can't follow the code, so if someone can explain to me where it goes next i'll be thankfull.

The printer classes in the library are provided for convenience (very useful for reading/debugging your JSONs). To do some useful work, you should implement your own class inheriting from JSONHandler and overriding the virtual handler functions of your interest. A chunk of code is worth thousand words, so I added a custom handler to the sample and JSONHandler documentation (in SVN, rev. 1276).

So, given a JSON:
Code: Select all
const std::string str("{"
      "\"firstName\": \"John\","
      "\"lastName\": \"Smith\","
      "\"address\": {"
         "\"streetAddress\": \"21 2nd Street\","
         "\"city\": \"New York\","
         "\"state\": \"NY\","
         "\"postalCode\": 10021"
      "}"
   "}");

and a handler:
Code: Select all
class PersonInfoHandler: public JSONHandler
{
public:
   PersonInfoHandler(std::ostream& out): JSONHandler(out){ }

   void handleKey(const JSONEntity& val) { _key = val; }

   void handleInteger(const JSONEntity& val) {
      if (_key.toString() == "postalCode")
         stream() << ' '  << val.toInteger() << '.' << std::endl;
   }

   void handleString(const JSONEntity& val) {
      if (_key.toString() == "firstName")             stream() << val.toString();
      else if (_key.toString() == "lastName")        stream() << ' ' << val.toString();
      else if (_key.toString() == "streetAddress") stream() << " lives at " << val.toString();
      else if (_key.toString() == "city")           stream() << " in " << val.toString();
      else if (_key.toString() == "state")      stream() << ", " << val.toString();
   }
private:
   JSONEntity _key;
};

this code
Code: Select all
JSONParser jp4(new PersonInfoHandler(std::cout)); jp4.parse(str);

yields this output
Code: Select all
John Smith lives at 21 2nd Street in New York, NY 10021.

Hope this helps :)
alex
 
Posts: 1120
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Managing JSON input

Postby mad93 » 07 Nov 2009, 22:40

Yes, you helped me a lot :)

And thanks for bearing with my noobish questions ;)
mad93
 
Posts: 6
Joined: 04 Nov 2009, 22:13


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests