Example #1
0
bool CLuaArguments::ReadFromJSONString(const char* szJSON)
{
    // Fast isJSON check: Check first non-white space character is '[' or '{'
    for (const char* ptr = szJSON; true;)
    {
        char c = *ptr++;
        if (c == '[' || c == '{')
            break;
        if (isspace((uchar)c))
            continue;
        return false;
    }

    json_object* object = json_tokener_parse(szJSON);
    if (!is_error(object))
    {
        if (json_object_get_type(object) == json_type_array)
        {
            bool bSuccess = true;

            std::vector<CLuaArguments*> knownTables;

            for (int i = 0; i < json_object_array_length(object); i++)
            {
                json_object*  arrayObject = json_object_array_get_idx(object, i);
                CLuaArgument* pArgument = new CLuaArgument();
                bSuccess = pArgument->ReadFromJSONObject(arrayObject, &knownTables);
                m_Arguments.push_back(pArgument);            // then the value
                if (!bSuccess)
                    break;
            }
            json_object_put(object);            // dereference
            return bSuccess;
        }
        else if (json_object_get_type(object) == json_type_object)
        {
            std::vector<CLuaArguments*> knownTables;
            CLuaArgument*               pArgument = new CLuaArgument();
            bool                        bSuccess = pArgument->ReadFromJSONObject(object, &knownTables);
            m_Arguments.push_back(pArgument);            // value
            json_object_put(object);

            return bSuccess;
        }
        json_object_put(object);            // dereference
    }
    //    else
    //        g_pGame->GetScriptDebugging()->LogError ( "Could not parse invalid JSON object.");
    //   else
    //        g_pGame->GetScriptDebugging()->LogError ( "Could not parse HTTP POST request, ensure data uses JSON.");
    return false;
}
bool CLuaArguments::ReadFromJSONObject ( json_object * object, std::vector < CLuaArguments* > * pKnownTables )
{
    if ( !is_error(object) )
    {
        if ( json_object_get_type ( object ) == json_type_object )
        {
            bool bKnownTablesCreated = false;
            if ( !pKnownTables )
            {
                pKnownTables = new std::vector < CLuaArguments* > ();
                bKnownTablesCreated = true;
            }

            pKnownTables->push_back ( this );

            bool bSuccess = true;
            json_object_object_foreach(object, key, val) 
            {
                CLuaArgument* pArgument = new CLuaArgument ( key );
                m_Arguments.push_back ( pArgument ); // push the key first
                pArgument = new CLuaArgument ( );
                bSuccess = pArgument->ReadFromJSONObject ( val, pKnownTables ); // then the value
                m_Arguments.push_back ( pArgument );
                if ( !bSuccess )
                    break;
            }

            if ( bKnownTablesCreated )
                delete pKnownTables;
            return bSuccess;
        }
bool CLuaArguments::ReadFromJSONString ( const char* szJSON )
{
    json_object* object = json_tokener_parse ( const_cast < char* > ( szJSON ) );
    if ( !is_error(object) )
    {
        if ( json_object_get_type ( object ) == json_type_array )
        {
            bool bSuccess = true;

            std::vector < CLuaArguments* > knownTables;
            
            for(int i=0; i < json_object_array_length(object); i++) 
            {
                json_object *arrayObject = json_object_array_get_idx(object, i);
		        CLuaArgument * pArgument = new CLuaArgument();
                bSuccess = pArgument->ReadFromJSONObject ( arrayObject, &knownTables );
                m_Arguments.push_back ( pArgument ); // then the value
                if ( !bSuccess )
                    break;
            }
            return bSuccess;
        }
    }
//    else
//        g_pGame->GetScriptDebugging()->LogError ( "Could not parse invalid JSON object.");
 //   else
//        g_pGame->GetScriptDebugging()->LogError ( "Could not parse HTTP POST request, ensure data uses JSON.");
    return false;
}