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

poco-1.5.1: Facebook GraphAPI JSON question #64

Closed
ghost opened this issue Jan 13, 2013 · 9 comments
Closed

poco-1.5.1: Facebook GraphAPI JSON question #64

ghost opened this issue Jan 13, 2013 · 9 comments
Labels

Comments

@ghost
Copy link

ghost commented Jan 13, 2013

Hi,

I'm using poco to deal with facebook GraphAPI. I managed to do most of the work but i'm struggling with the latest bit. Here is my code:

try {
    // Target URI
    Poco::URI uri("https://graph.facebook.com/me/friends?access_token=" + AppDelegate::getAccessToken());
    std::string path(uri.getPathEtc());

    // SSL Context
    const Poco::Net::Context::Ptr context(new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_NONE, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"));

    // HTTP Session
    Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context);

    // Request
    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
    session.sendRequest(request);

    // Response
    Poco::Net::HTTPResponse response;
    std::istream& rs = session.receiveResponse(response);
    std::cout << response.getStatus() << " " << response.getReason() << std::endl;
    for (auto response_header: response) {
      std::cout << response_header.first << " = " << response_header.second << std::endl;
    }

    std::ostringstream responseBody;
    if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED) {
      Poco::StreamCopier::copyStream(rs, responseBody);
//      Poco::StreamCopier::copyStream(rs, std::cout);
    } else {
      Poco::NullOutputStream null;
      Poco::StreamCopier::copyStream(rs, null);
      return;
    }

    // Parsing JSON response
    Poco::JSON::DefaultHandler handler;
    Poco::JSON::Parser parser;

    parser.setHandler(&handler);
    parser.parse(responseBody.str());
    Poco::DynamicAny result = handler.result();

    Poco::JSON::Object::Ptr obj;
    //if (result.type() == typeid(Poco::JSON::Object::Ptr)) {
      obj = result.extract<Poco::JSON::Object::Ptr>();
    //}

    // Serialize to string
//      std::ostringstream out;
    obj->stringify(std::cout);
  } catch (Poco::Exception& exc) {
    CCLOG("error =  %s", exc.displayText().c_str());
  }

And here is the output:

200 OK 
Access-Control-Allow-Origin = * 
Cache-Control = private, no-cache, no-store, must-revalidate 
Connection = close 
Content-Length = 3149 
Content-Type = text/javascript; charset=UTF-8 
Date = Sun, 13 Jan 2013 10:46:44 GMT 
ETag = "1bfe8dcc4ef3831df52f40d3457c8744b129908a" 
Expires = Sat, 01 Jan 2000 00:00:00 GMT 
Pragma = no-cache 
X-FB-Debug = PzikR8+tz4SyqNBlz0WiHLKUQhql4FsoiTEitZK/EJ0= 
X-FB-Rev = 711202 
{"data":[Cocos2d: error =  Bad cast exception: Can not convert to std::string

As you can see, everything works like a charm except the:

  obj->stringify(std::cout);

Any ideas of what I'm doing wrong ?

Thanks,
BQ.

@aleks-f
Copy link
Member

aleks-f commented Jan 13, 2013

Any ideas of what I'm doing wrong ?

Impossible to tell without seeing the JSON itself. I'm quite sure, though, that at some point during "stringifying" you end up here:

https://github.com/pocoproject/poco/blob/develop/Foundation/include/Poco/Dynamic/VarHolder.h#L460

inline void VarHolder::convert(std::string& /*val*/) const
{
    throw BadCastException("Can not convert to std::string");
}

Franky Braem (https://github.com/fbraem) wrote that library so he is the best source of information on it.

@ghost
Copy link
Author

ghost commented Jan 14, 2013

Ok thanks for the info.

Franky, is there any documentation/tutorial demonstrating the use of your JSON library ? The benchmark sample is a good start, however, it's a bit bland let's say.

@ghost ghost closed this as completed Jan 14, 2013
@ghost ghost reopened this Jan 14, 2013
@aleks-f
Copy link
Member

aleks-f commented Jan 14, 2013

It'll help if you post a sample of JSON that throws.

@fbraem
Copy link
Member

fbraem commented Jan 14, 2013

Can you send me an example JSON response from the URL you use?

@ghost
Copy link
Author

ghost commented Jan 14, 2013

Sure, my bad. Here is the JSON response:

{"data":[{"name":"Frantisek Zacek","id":"518797070"},{"name":"Clem Ne'me","id":"521787019"},{"name":"Ga\u00eblle Legu\u00e9n\u00e9","id":"523167073"},{"name":"Sylvain Astier","id":"532706918"},{"name":"David Quentin","id":"539806978"},{"name":"Flandre Renaud","id":"559169452"},{"name":"Augustin Rieunier","id":"565503436"},{"name":"Julie Legendre","id":"567007562"},{"name":"Sarah Hosking","id":"572717063"},{"name":"Yann P\u00e9zier","id":"584202620"},{"name":"Denis Rousseau","id":"597972372"},{"name":"Benoit Nesme","id":"604647623"},{"name":"Florent Drillon","id":"609952144"},{"name":"Benjamin Villedieu","id":"633548483"},{"name":"Julien Massenet","id":"635685647"},{"name":"Vincent Remy","id":"658644483"},{"name":"Martin Smurthwaite","id":"670111481"},{"name":"Shanta Raj","id":"680831928"},{"name":"Cyrielle Mehl","id":"682324166"},{"name":"Anne-Fleur Drillon","id":"690978943"},{"name":"FX Singer","id":"699407761"},{"name":"Yann Popo","id":"701439716"},{"name":"Murielle Touret","id":"701911151"},{"name":"Celine Laurencin","id":"710495247"},{"name":"Charles Vionnet","id":"718038682"},{"name":"Geraldine Quenin","id":"729326765"},{"name":"Hadrien Gantzer","id":"781456179"},{"name":"Julie Dupr\u00e9","id":"828462523"},{"name":"Smatti Sofian","id":"842429787"},{"name":"Jef Pommier","id":"1002498019"},{"name":"Jean-Guillaume Desanti","id":"1018251414"},{"name":"Patrick Peinoit","id":"1018307198"},{"name":"Damien Brelivet","id":"1044879509"},{"name":"Arnaud Puy","id":"1080510304"},{"name":"Guillaume Cessieux","id":"1096104649"},{"name":"Perroy Damien","id":"1108765447"},{"name":"Alexandre Demailly","id":"1133658539"},{"name":"Benoit Rivollet","id":"1196713537"},{"name":"Bernard Quenin","id":"1205726565"},{"name":"Marilyn Bass","id":"1218525169"},{"name":"Bastien Perret","id":"1272287731"},{"name":"Karim Tadjene","id":"1277063130"},{"name":"Sophie Ladreyt","id":"1278373214"},{"name":"Paola Jacquemin","id":"1311030773"},{"name":"Agnieszka Jacquemot","id":"1387575444"},{"name":"Annie Suel","id":"1399271494"},{"name":"Laurent Cony","id":"1456958012"},{"name":"Didier Trouvel","id":"1459554573"},{"name":"Delphine Juillot","id":"1484902071"},{"name":"C\u00e9dric Martins","id":"1552565132"},{"name":"Aur\u00e9lie Bert Petrucci","id":"1568276408"},{"name":"Rh\u00e9da Bakir","id":"1616376985"},{"name":"Anthony Poignant","id":"100000969207248"},{"name":"Todd Warwaruk","id":"100001547094911"},{"name":"Miguel Herve","id":"100001565258822"},{"name":"Farah Mahir","id":"100002202791306"},{"name":"Marilyn Ssab","id":"100003069216658"},{"name":"S\u00e9bastien Perret","id":"100003533872156"},{"name":"Glob Ideuh","id":"100003585244509"},{"name":"Isabelle Pourquoipas","id":"100004595822764"},{"name":"Cie K\u00e9roz\u00e8ne","id":"100004688318068"}],"paging":{"next":"https:\/\/graph.facebook.com\/100003057316436\/friends?access_token=BAAGcAvILkyUBABh4NGZCJuj1ZCsHpTROdmbFLs5Dyh4tcZBlR1zaqMmj4MIT348XVwBqwUx2vyBtQxUDO0g62XXXXituSmvrhrYLx0BXZBI9Tgjqrvsez1nnSfqxfQzuuaHBJn2hhb3vDWxIHpGdZCTJ797xgLM82RbjLg9xR06I8V5S1Kg6qDI1werXXqtyl6J2UdAmBdYoZAi1DaxX6lAjwoOfL0PV6cu798A1WNCDgZDZD&limit=5000&offset=5000&__after_id=100004688318068"}}

I was just wondering if there was a more substantial example somewhere else, maybe I should file another question then :)

Thanks,
BQ.

@aleks-f
Copy link
Member

aleks-f commented Jan 15, 2013

I can't reproduce the problem. This code parses and stringifies fine:

std::string fbjson = "{\"data\":[{\"name\":\"Frantisek Zacek\",\"id\":\"518797070\"},{\"name\":\"Clem Ne'me\",\"id\":\"521787019\"},{\"name\":\"Ga\\u00eblle Legu\\u00e9n\\u00e9\",\"id\":\"523167073\"},{\"name\":\"Sylvain Astier\",\"id\":\"532706918\"},{\"name\":\"David Quentin\",\"id\":\"539806978\"},{\"name\":\"Flandre Renaud\",\"id\":\"559169452\"},{\"name\":\"Augustin Rieunier\",\"id\":\"565503436\"},{\"name\":\"Julie Legendre\",\"id\":\"567007562\"},{\"name\":\"Sarah Hosking\",\"id\":\"572717063\"},{\"name\":\"Yann P\\u00e9zier\",\"id\":\"584202620\"},{\"name\":\"Denis Rousseau\",\"id\":\"597972372\"},{\"name\":\"Benoit Nesme\",\"id\":\"604647623\"},{\"name\":\"Florent Drillon\",\"id\":\"609952144\"},{\"name\":\"Benjamin Villedieu\",\"id\":\"633548483\"},{\"name\":\"Julien Massenet\",\"id\":\"635685647\"},{\"name\":\"Vincent Remy\",\"id\":\"658644483\"},{\"name\":\"Martin Smurthwaite\",\"id\":\"670111481\"},{\"name\":\"Shanta Raj\",\"id\":\"680831928\"},{\"name\":\"Cyrielle Mehl\",\"id\":\"682324166\"},{\"name\":\"Anne-Fleur Drillon\",\"id\":\"690978943\"},{\"name\":\"FX Singer\",\"id\":\"699407761\"},{\"name\":\"Yann Popo\",\"id\":\"701439716\"},{\"name\":\"Murielle Touret\",\"id\":\"701911151\"},{\"name\":\"Celine Laurencin\",\"id\":\"710495247\"},{\"name\":\"Charles Vionnet\",\"id\":\"718038682\"},{\"name\":\"Geraldine Quenin\",\"id\":\"729326765\"},{\"name\":\"Hadrien Gantzer\",\"id\":\"781456179\"},{\"name\":\"Julie Dupr\\u00e9\",\"id\":\"828462523\"},{\"name\":\"Smatti Sofian\",\"id\":\"842429787\"},{\"name\":\"Jef Pommier\",\"id\":\"1002498019\"},{\"name\":\"Jean-Guillaume Desanti\",\"id\":\"1018251414\"},{\"name\":\"Patrick Peinoit\",\"id\":\"1018307198\"},{\"name\":\"Damien Brelivet\",\"id\":\"1044879509\"},{\"name\":\"Arnaud Puy\",\"id\":\"1080510304\"},{\"name\":\"Guillaume Cessieux\",\"id\":\"1096104649\"},{\"name\":\"Perroy Damien\",\"id\":\"1108765447\"},{\"name\":\"Alexandre Demailly\",\"id\":\"1133658539\"},{\"name\":\"Benoit Rivollet\",\"id\":\"1196713537\"},{\"name\":\"Bernard Quenin\",\"id\":\"1205726565\"},{\"name\":\"Marilyn Bass\",\"id\":\"1218525169\"},{\"name\":\"Bastien Perret\",\"id\":\"1272287731\"},{\"name\":\"Karim Tadjene\",\"id\":\"1277063130\"},{\"name\":\"Sophie Ladreyt\",\"id\":\"1278373214\"},{\"name\":\"Paola Jacquemin\",\"id\":\"1311030773\"},{\"name\":\"Agnieszka Jacquemot\",\"id\":\"1387575444\"},{\"name\":\"Annie Suel\",\"id\":\"1399271494\"},{\"name\":\"Laurent Cony\",\"id\":\"1456958012\"},{\"name\":\"Didier Trouvel\",\"id\":\"1459554573\"},{\"name\":\"Delphine Juillot\",\"id\":\"1484902071\"},{\"name\":\"C\\u00e9dric Martins\",\"id\":\"1552565132\"},{\"name\":\"Aur\\u00e9lie Bert Petrucci\",\"id\":\"1568276408\"},{\"name\":\"Rh\\u00e9da Bakir\",\"id\":\"1616376985\"},{\"name\":\"Anthony Poignant\",\"id\":\"100000969207248\"},{\"name\":\"Todd Warwaruk\",\"id\":\"100001547094911\"},{\"name\":\"Miguel Herve\",\"id\":\"100001565258822\"},{\"name\":\"Farah Mahir\",\"id\":\"100002202791306\"},{\"name\":\"Marilyn Ssab\",\"id\":\"100003069216658\"},{\"name\":\"S\\u00e9bastien Perret\",\"id\":\"100003533872156\"},{\"name\":\"Glob Ideuh\",\"id\":\"100003585244509\"},{\"name\":\"Isabelle Pourquoipas\",\"id\":\"100004595822764\"},{\"name\":\"Cie K\\u00e9roz\\u00e8ne\",\"id\":\"100004688318068\"}],\"paging\":{\"next\":\"https:\\/\\/graph.facebook.com\\/100003057316436\\/friends?access_token=BAAGcAvILkyUBABh4NGZCJuj1ZCsHpTROdmbFLs5Dyh4tcZBlR1zaqMmj4MIT348XVwBqwUx2vyBtQxUDO0g62XXXXituSmvrhrYLx0BXZBI9Tgjqrvsez1nnSfqxfQzuuaHBJn2hhb3vDWxIHpGdZCTJ797xgLM82RbjLg9xR06I8V5S1Kg6qDI1werXXqtyl6J2UdAmBdYoZAi1DaxX6lAjwoOfL0PV6cu798A1WNCDgZDZD&limit=5000&offset=5000&__after_id=100004688318068\"}}";
Parser parser;
DefaultHandler handler;
parser.setHandler(&handler);
parser.parse(fbjson);
Object::Ptr obj = handler.result().extract<Object::Ptr>();
obj->stringify(std::cout);

@ghost
Copy link
Author

ghost commented Jan 15, 2013

Strange. I ran your code and I get the same error as above (at least its consistent).

I run this code on an iOS device, do you think that could be a reason ? I'm gonna investigate more.

Thanks for your feedback.
BQ.

@aleks-f
Copy link
Member

aleks-f commented Jan 15, 2013

see if you have this fix in your code:

42d963a#JSON/src/Stringifier.cpp

@ghost
Copy link
Author

ghost commented Jan 15, 2013

Yeah, I have the fix.

I found my issue. I messed up my iPhone build configuration file. I forgot to add -std=c++11 to my custom build file.

Here is my build file (if anyone want to use clang c++11/libc++ for iPhone):

#
# $Id: //poco/1.4/build/config/iPhone#3 $
#
# iPhone
#
# Build settings for iPhone OS, using Apple's iPhone SDK
# 

#
# General Settings
#
# iPhone OS does not allow dynamic linking to user libraries
#
LINKMODE ?= STATIC

#
# If the SDK is defined use it
# Otherwise find the latest version installed
#
IPHONE_SDK_VERSION = 6.0

# if IPHONE_SDK_VERSION_MIN is defined use that
# Otherwise use the version found. 

IPHONE_SDK             ?= iPhoneOS
IPHONE_SDK_ROOT        ?= $(shell xcode-select -print-path)/Platforms/$(IPHONE_SDK).platform/Developer/SDKs
IPHONE_SDK_ROOT_DIR     = $(IPHONE_SDK_ROOT)/$(IPHONE_SDK)
IPHONE_SDK_BASE         = $(shell ls -d $(IPHONE_SDK_ROOT_DIR)$(IPHONE_SDK_VERSION)*.sdk | tail -1)
IPHONE_SDK_VERSION_MIN ?= $(patsubst %.sdk,%,$(patsubst $(IPHONE_SDK_ROOT_DIR)%,%,$(IPHONE_SDK_BASE)))

POCO_TARGET_OSNAME     ?= $(IPHONE_SDK)
POCO_TARGET_OSARCH     ?= armv7
TOOL_PREFIX            ?= $(shell xcode-select -print-path)/Platforms/$(IPHONE_SDK).platform/Developer/usr/bin
OSFLAGS                ?= -arch $(POCO_TARGET_OSARCH) -isysroot $(IPHONE_SDK_BASE) -mthumb -miphoneos-version-min=$(IPHONE_SDK_VERSION_MIN)

#
# Tools
#
# If GCC_VER is defined then use it.
# Otherwise select the latest version
#
# CC        = $(shell ls $(TOOL_PREFIX)/llvm-gcc-$(GCC_VER)* | tail -1)
# CXX       = $(shell ls $(TOOL_PREFIX)/llvm-g++-$(GCC_VER)* | tail -1)
CC      = $(shell xcode-select -print-path)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
CXX     = $(shell xcode-select -print-path)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang


LINK    = $(CXX) -bind_at_load
LIB     = libtool -static -o
RANLIB  = ranlib
SHLIB   = $(CXX) $(OSFLAGS) -dynamiclib -Wl,-install_name,$@ -o $@
DYLIB   = $(CXX) $(OSFLAGS) -dynamic -bundle -read_only_relocs suppress -Wl,-bind_at_load -o $@ 
SHLIBLN = $(POCO_BASE)/build/script/shlibln
STRIP   = 
DEP     = $(POCO_BASE)/build/script/makedepend.gcc
SHELL   = sh
RM      = rm -rf
CP      = cp
MKDIR   = mkdir -p

#
# Extension for Shared Libraries
#
SHAREDLIBEXT     = .$(target_version).dylib
SHAREDLIBLINKEXT = .dylib

#
# Compiler and Linker Flags
#
CFLAGS          = $(OSFLAGS)
CFLAGS32        =
CFLAGS64        =
CXXFLAGS        = $(OSFLAGS) -Wall -Wno-sign-compare -std=c++11 -stdlib=libc++
CXXFLAGS32      =
CXXFLAGS64      = 
LINKFLAGS       = $(OSFLAGS)
LINKFLAGS32     =
LINKFLAGS64     =
STATICOPT_CC    =
STATICOPT_CXX   =
STATICOPT_LINK  =
SHAREDOPT_CC    = -fPIC 
SHAREDOPT_CXX   = -fPIC
SHAREDOPT_LINK  = 
DEBUGOPT_CC     = -g -D_DEBUG=$(DEBUGLEVEL)
DEBUGOPT_CXX    = -g -D_DEBUG=$(DEBUGLEVEL) 
DEBUGOPT_LINK   =
RELEASEOPT_CC   = -DNDEBUG -O2
RELEASEOPT_CXX  = -DNDEBUG -O 
RELEASEOPT_LINK =

#
# System Specific Flags
#
SYSFLAGS = -DPOCO_HAVE_IPv6 -DPOCO_NO_FPENVIRONMENT -DPOCO_NO_STAT64 -DPOCO_NO_SHAREDLIBS -DPOCO_NO_NET_IFTYPES

#
# System Specific Libraries
#
SYSLIBS  = -ldl -lc++

Thank you for your help. Sorry I've wasted your time :)
BQ.

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

No branches or pull requests

2 participants