Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex-json-2.cpp
#include "p6loader.h"
#include "p6jsonreader.h"
#include "cconsolestream.h"
#include "ccontenthandler.h"
#include "cerrorhandler.h"
using namespace P6R;
using namespace P6EXAMPLES;
P6DECLARE_CID( p6JSONReader );
P6DECLARE_IID( IContentHandlerInit );
P6DECLARE_IID( IErrorHandlerInit );
namespace {
P6R::P6ERR getJsonReader(P6R::p6IJSONReader **ppIface)
{
P6ERR err;
if(P6SUCCEEDED(err = p6CreateInstance(NULL,CID_p6JSONReader,VALIDATECOMPTR(p6IJSONReader,cpJSON)))) {
if(P6SUCCEEDED(err = cpJSON->initialize(P6JSON_NOFLAGS,0))) {
//
// Using detach() saves a reference counting operation
// and gives ownership to *ppIface.
//
cpJSON.detach(ppIface);
}
}
return err;
}
P6R::P6ERR getContentHandler( P6R::p6IConsole *pConsole, P6R::p6ISafeString *pStr, P6R::p6IJSONContentHandler **ppIface)
{
P6ERR err;
if(P6SUCCEEDED(err = CContentHandler::createInstance(NULL,VALIDATECOMPTR(IContentHandlerInit,cpInit)))) {
if (P6SUCCEEDED( err = cpInit->initialize( pConsole, pStr ))) {
err = cpInit.queryInterface(IID_p6IJSONContentHandler,ppIface);
}
}
return err;
}
P6R::P6ERR getErrorHandler( P6R::p6IConsole *pConsole, P6R::p6IJSONErrorHandler **ppIface)
{
P6ERR err;
if(P6SUCCEEDED(err = CErrorHandler::createInstance(NULL,VALIDATECOMPTR(IErrorHandlerInit,cpInit)))) {
if (P6SUCCEEDED( err = cpInit->initialize( pConsole ))) {
err = cpInit.queryInterface(IID_p6IJSONErrorHandler,ppIface);
}
}
return err;
}
P6R::P6ERR runJSON( p6IConsole *pConsole, p6IDataStream *pStreamDebug )
{
P6UINT32 bufSize = 0;
P6ERR err = eOk;
P6ARG args[1];
// -> notice the error on the first nested value in 'menuitem', it has an array closing instead of an object closing
const P6CHAR* pJSON =
"{\"menu\": {\n" \
" \"id\": \"file\",\n" \
" \"value\": \"File\",\n" \
" \"popup\": {\n" \
" \"menuitem\": [\n" \
" {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"],\n" \
" {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n" \
" {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n" \
" ]\n" \
" },\n" \
" \"readings\":[ -30, 44, 100.34, 3.5E4, 3.5e-4, true ]\n" \
"}}";
// [A] Initialize the components we need
if (P6FAILED( err = p6GetRuntimeIface( VALIDATECOMPTR( p6ISafeString, cpStr )))) return err;
if(P6FAILED(err = getJsonReader(cpReader.addressof()))) return err;
err = cpReader->queryInterface( VALIDATECOMPTR( p6IDataStream, cpStream ));
// [B] Set the content handlers to handle the stream of events and possible errors
if(P6FAILED(err = getContentHandler(pConsole, cpStr, cpContent.addressof()))) return err;
err = cpReader->setContentHandler( cpContent );
if(P6FAILED(err = getErrorHandler(pConsole, cpError.addressof()))) return err;
err = cpReader->setErrorHandler( cpError );
// [C] Stream the data into the parser
if (P6SUCCEEDED( err )) err = cpStream->beginStream();
err = cpStr->strlen( pJSON, 100000, &bufSize );
if (P6SUCCEEDED( err )) err = cpStream->processStream( pJSON, (P6UINT32)bufSize );
err = cpStream->endStream();
if (P6FAILED( err )) {
P6AI_ERR(&args[0],err);
pConsole->writeStdout("ERROR: Example failed with [ %1$ ]\n", &args[0], 1, NULL );
}
return err;
}
} // namespace
int main(int argc,char *argv[])
{
P6ERR err = eOk;
p6ComPtr<p6IDataStream> cpDebugStream;
P6ARG args[1];
if(P6SUCCEEDED(err = CConsoleStream::createInstance(NULL,VALIDATECOMPTR(p6IDataStream,cpDebugStream)))) {
if(P6SUCCEEDED(err = p6InitializeLoader(cpDebugStream,9,P6SCLF_NOFLAGS))) {
err = runJSON( cpConsole, cpDebugStream );
P6AI_ERR(&args[0],err);
cpConsole->writeStdout("runDOM result: [ %1$ ]\n",&args[0],1,NULL);
//
// Make sure to release the console interface
// before calling p6CleanupLoader() !! In this
// instance we don't let the smart pointer
// handle the cleanup.
//
cpConsole = NULL;
}
else printf("ERROR: Failed to retrieve console interface [ %x ]\n", err );
}
else printf("ERROR: Failed to initialize the loader [ %x ]\n", err );
}
else printf( "ERROR: Failed to create CConsoleStream [ %x ]\n", err );
return err;
}