int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
{
    // Got a string argument?
    CScriptArgReader argStream ( luaVM );

    if ( !argStream.NextIsNil () )
    {
        bool bCompact = false;
        // Read the argument
        CLuaArguments JSON;
        JSON.ReadArgument ( luaVM, 1 );
        argStream.Skip ( 1 );
        argStream.ReadBool ( bCompact, false );

        if ( !argStream.HasErrors () )
        {
            // Convert it to a JSON string
            std::string strJSON;
            if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
            {
                // Return the JSON string
                lua_pushstring ( luaVM, strJSON.c_str () );
                return 1;
            }
        }
        else
            m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM );

    // Failed
    lua_pushnil ( luaVM );
    return 1;
}
int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
{
    // Got a string argument?
    CScriptArgReader argStream ( luaVM );

    if ( !argStream.NextIsNil () )
    {
        int jsonFlags = 0;
        // Read the argument
        CLuaArguments JSON;
        JSON.ReadArgument ( luaVM, 1 );
        argStream.Skip ( 1 );

        bool bCompact;
        argStream.ReadBool ( bCompact, false );
        jsonFlags |= bCompact ? JSON_C_TO_STRING_PLAIN : JSON_C_TO_STRING_SPACED;
        
        eJSONPrettyType jsonPrettyType;
        argStream.ReadEnumString ( jsonPrettyType, JSONPRETTY_NONE );
        if ( jsonPrettyType != JSONPRETTY_NONE )
            jsonFlags |= jsonPrettyType;

        if ( !argStream.HasErrors () )
        {
            // Convert it to a JSON string
            std::string strJSON;
            if ( JSON.WriteToJSONString ( strJSON, false, jsonFlags ) )
            {
                // Return the JSON string
                lua_pushstring ( luaVM, strJSON.c_str () );
                return 1;
            }
        }
        else
            m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM );

    // Failed
    lua_pushnil ( luaVM );
    return 1;
}
int CLuaFunctionDefs::OutputChatBox ( lua_State* luaVM )
{
    // bool outputChatBox ( string text [, element visibleTo=getRootElement(), int r=231, int g=217, int b=176, bool colorCoded=false ] )
    SString ssChat;
    CElement* pElement;
    bool bColorCoded;
    // Default
    unsigned char ucRed = 231;
    unsigned char ucGreen = 217;
    unsigned char ucBlue = 176;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( ssChat );
    argStream.ReadUserData ( pElement, m_pRootElement );

    if ( argStream.NextIsNumber () && argStream.NextIsNumber ( 1 ) && argStream.NextIsNumber ( 2 ) )
    {
        argStream.ReadNumber ( ucRed );
        argStream.ReadNumber ( ucGreen );
        argStream.ReadNumber ( ucBlue );
    }
    else
        argStream.Skip ( 3 );

    argStream.ReadBool ( bColorCoded, false );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CStaticFunctionDefinitions::OutputChatBox ( (const char*) ssChat, pElement, ucRed, ucGreen, ucBlue, bColorCoded, pLuaMain );
            lua_pushboolean ( luaVM, true );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::OOP_OutputChatBox ( lua_State * luaVM )
{
    // bool Player:outputChat ( string text [, int r=231, int g=217, int b=176, bool colorCoded=false ] )
    CElement* pElement; SString strText; uchar ucRed = 231; uchar ucGreen = 217; uchar ucBlue = 176; bool bColorCoded;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pElement );
    argStream.ReadString ( strText );

    if ( argStream.NextIsNumber ( 0 ) && argStream.NextIsNumber ( 1 ) && argStream.NextIsNumber ( 2 ) )
    {
        argStream.ReadNumber ( ucRed );
        argStream.ReadNumber ( ucGreen );
        argStream.ReadNumber ( ucBlue );
    }
    else
        argStream.Skip ( 3 );

    argStream.ReadBool ( bColorCoded, false );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CStaticFunctionDefinitions::OutputChatBox ( strText, pElement, ucRed, ucGreen, ucBlue, bColorCoded, pLuaMain );
            lua_pushboolean ( luaVM, true );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}