Package blackberry.common.util.json4j

Examples of blackberry.common.util.json4j.JSONObject


    public void invoke( JSExtensionRequest request, JSExtensionResponse response ) throws WidgetException {
        String method = request.getMethodName();
        Object[] args = request.getArgs();
        String msg = "";
        int code = JSExtensionReturnValue.SUCCESS;
        JSONObject data = new JSONObject();
        JSONObject returnValue = null;
       
        try {
            if( method.equals( SETUP ) ) {
                if (args != null && args.length == 3)
                {
                    setupLogging(args[0].toString(), args[1].toString(), Integer.parseInt(args[2].toString()));
                }
                else
                {
                    writeToLog("webworks.system.log", "WebWorks System Log API", "ERROR 4419 Improper API useage");
                }
           
                               
            } else if( method.equals( WRITE ) ) {
                             
                if (args != null && args.length == 1)
                {
                    writeToLog(args[0].toString());
                } else if (args != null && args.length == 3)
                {
                    writeToLog(args[0].toString(), args[1].toString(), args[2].toString());
                }
                else
                {
                    writeToLog("blackberry.system.log", "WebWorks System Log API", "ERROR 4419 Improper API useage");
               
                        }
        } catch( Exception e ) {
            msg = e.getMessage();
            code = JSExtensionReturnValue.FAIL;
        }

        returnValue = new JSExtensionReturnValue( msg, code, data ).getReturnValue();

        response.setPostData( returnValue.toString().getBytes() );
    }
View Full Code Here


     * @throws JSONException Thrown if an IO error occurs during parse, such as a malformed JSON object.
     */
    public JSONObject parseObject(boolean ordered, JSONObject rootObject) throws JSONException {

        try {
            JSONObject result = null;
            if (rootObject != null) {
                result = rootObject;
            } else {
                if (!ordered) {
                    result = new JSONObject();
                } else {
                  //MSN NO ORDERED
                    result = new JSONObject();
                }
            }

            if (lastToken != Token.TokenBraceL) throw new JSONException("Expecting '{' " + tokenizer.onLineCol() + " instead, obtained token: '" + lastToken + "'");
            lastToken = tokenizer.next();

            while (true) {
                if (lastToken == Token.TokenEOF) throw new JSONException("Unterminated object " + tokenizer.onLineCol());

                if (lastToken == Token.TokenBraceR) {
                    lastToken = tokenizer.next();
                    break;
                }

                if (!lastToken.isString()) throw new JSONException("Expecting string key " + tokenizer.onLineCol());
                String key = lastToken.getString();

                lastToken = tokenizer.next();
                if (lastToken != Token.TokenColon) throw new JSONException("Expecting colon " + tokenizer.onLineCol());

                lastToken = tokenizer.next();
                Object val = parseValue(ordered);

                result.put(key, val);

                if (lastToken == Token.TokenComma) {
                    lastToken = tokenizer.next();
                }

View Full Code Here

    public void invoke( JSExtensionRequest request, JSExtensionResponse response ) throws WidgetException {
        String method = request.getMethodName();
        Object[] args = request.getArgs();
        String msg = "";
        int code = JSExtensionReturnValue.SUCCESS;
        JSONObject data = new JSONObject();
        JSONArray dataArray = new JSONArray();
        JSONObject returnValue = null;

        PaymentEngine engine = PaymentSystem.getInstance();

        if( engine == null ) {
            throw new IllegalArgumentException(
                    "Sorry, in-app purchases are unavailable. Make sure BlackBerry App World v2.1 or higher is installed on your device." );
        }

        if( !SUPPORTED_METHODS.contains( method ) ) {
            throw new WidgetException( "Undefined method: " + method );
        }

        try {
            if( method.equals( FUNCTION_PURCHASE ) ) {
                String digitalGoodID = (String) request.getArgumentByName( KEY_DG_ID );
                String digitalGoodSKU = (String) request.getArgumentByName( KEY_DG_SKU );
                String digitalGoodName = (String) request.getArgumentByName( KEY_DG_NAME );
                String metaData = (String) request.getArgumentByName( KEY_METADATA );
                String purchaseAppName = (String) request.getArgumentByName( KEY_APP_NAME );
                String purchaseAppIcon = (String) request.getArgumentByName( KEY_APP_ICON );

                PurchaseArgumentsBuilder arguments = new PurchaseArgumentsBuilder().withDigitalGoodId( digitalGoodID )
                        .withDigitalGoodName( digitalGoodName ).withDigitalGoodSku( digitalGoodSKU ).withMetadata( metaData )
                        .withPurchasingAppName( purchaseAppName )
                        .withPurchasingAppIcon( ( purchaseAppIcon != null ? Bitmap.getBitmapResource( purchaseAppIcon ) : null ) );

                // Blocking call: engine.purchase() invokes AppWorld screen.
                Purchase successfulPurchase = engine.purchase( arguments.build() );
                String purchaseJSON = purchaseToJSONString( successfulPurchase );

                data = new JSONObject( purchaseJSON );
                code = JSExtensionReturnValue.SUCCESS;
                msg = "Purchase Successful";
            } else if( method.equals( FUNCTION_GETEXISTINGPURCHASES ) ) {
                String refresh = (String) request.getArgumentByName( KEY_REFRESH );
                // Blocking call: engine.getExistingPurchases() invokes AppWorld screen.
                Purchase[] purchases = engine.getExistingPurchases( parseBoolean( refresh ) );

                if( purchases.length != 0 ) {
                    for( int i = 0; i < purchases.length; i++ ) {
                        String purchaseJSON = "";
                        purchaseJSON += purchaseToJSONString( purchases[ i ] );
                        JSONObject temp = new JSONObject( purchaseJSON );
                        dataArray.add( temp );
                    }
                }
            } else if( method.equals( new String( FUNCTION_GETMODE ) ) ) {
                data.put( "developmentMode", PaymentSystem.getMode() );
View Full Code Here

     */
    public static SystemEventReturnValue getErrorForOp(String method, String arg) {
        return new SystemEventReturnValue(new JSExtensionReturnValue(
                            "Error calling [" + method + "] with [" + arg + "]",
                            RC_FAIL,
                            new JSONObject()
                ))
    }
View Full Code Here

     */
    public static SystemEventReturnValue getSuccessForOp(String method, String arg) {
        return new SystemEventReturnValue(new JSExtensionReturnValue(
                            "Success calling [" + method + "] with [" + arg + "]",
                            RC_SUCCESS,
                            new JSONObject()
                ))
    }
View Full Code Here

            jsonBuilder.append(",arg:" + eventArg);
        }
       
        jsonBuilder.append('}');
       
        JSONObject retval = null;
       
        try {
            retval = new JSONObject(jsonBuilder.toString());
        } catch (JSONException e) {
            throw new RuntimeException("Error creating JSON return value for event [" + event + "] with arg [" + eventArg + "]");
        }
       
        return retval;
View Full Code Here

     *
     * @return JSONObject
     * @throws JSONException
     */
    public JSONObject getJSONObject() throws JSONException {
        JSONObject jsonObj = new JSONObject();

        if( _fields != null && !_fields.isEmpty() ) {
            Enumeration keys = _fields.keys();
            Vector jsonExcludedFields = getJSONExcludedFields();

            while( keys.hasMoreElements() ) {
                String key = (String) keys.nextElement();

                if( jsonExcludedFields == null || ( jsonExcludedFields != null && !jsonExcludedFields.contains( key ) ) ) {
                    ScriptField field = getItem( key );
                    Object value = getField( key );
                    if( field.getType() == ScriptField.TYPE_DATE ) {
                        if( value != null ) {
                            value = new Long( ( (Date) value ).getTime() );
                        }
                    } else if( field.getType() == ScriptField.TYPE_SCRIPTABLE ) {
                        if( value != null ) {
                            if( value instanceof ObjectBase ) {
                                value = ( (ObjectBase) value ).getJSONObject();
                            } else if( value instanceof ObjectBase[] ) {
                                value = convertObjectArrayToJSONArray( (ObjectBase[]) value );
                            }

                        }
                    }

                    // Added checking for empty string array, json.put doesn't parse string array if it is null
                    if( value instanceof String[] ) {
                        String[] ss = (String[]) value;
                        jsonObj.put( key, ss );
                    } else {
                        jsonObj.put( key, value );
                    }
                }
            }
        }

View Full Code Here

    public void invoke( JSExtensionRequest request, JSExtensionResponse response ) throws WidgetException {
        String method = request.getMethodName();
        Object[] args = request.getArgs();
        String msg = "";
        int code = JSExtensionReturnValue.SUCCESS;
        JSONObject data = new JSONObject();
        JSONObject returnValue = null;

        if( !SUPPORTED_METHODS.contains( method ) ) {
            throw new WidgetException("Undefined method: " + method);
        }

        try {
            if( method.equals( FUNCTION_HAS_PERMISSION ) ) {
                String module = (String) args[ 0 ];
                data.put( ARG_MODULE, module );
                data.put( FUNCTION_HAS_PERMISSION, hasPermission( module ) );
            } else if( method.equals( FUNCTION_HAS_CAPABILITY ) ) {
                String capability = (String) args[ 0 ];
                data.put( ARG_CAPABILITY, capability );
                data.put( FUNCTION_HAS_CAPABILITY, hasCapability( capability ) );
            } else if( method.equals( FUNCTION_HAS_DATA_COVERAGE ) ) {
                data.put( FUNCTION_HAS_DATA_COVERAGE, hasDataCoverage() );
            } else if( method.equals( FUNCTION_IS_MASS_STORAGE_ACTIVE ) ) {
                data.put( FUNCTION_IS_MASS_STORAGE_ACTIVE, isMassStorageActive() );
            } else if( method.equals( FUNCTION_SET_HOME_SCREEN ) ) {
                String picture = (String) args[ 0 ];
                data.put( ARG_PICTURE, picture );
                setHomeScreenBackground( picture );
            } else if( method.equals( SOFTWARE_VERSION ) ) {
                data.put( SOFTWARE_VERSION, getSoftwareVersion() );
            } else if( method.equals( SCRIPT_API_VERSION ) ) {
                data.put( SCRIPT_API_VERSION, getSoftwareVersion() );
            } else if( method.equals( MODEL ) ) {
                data.put( MODEL, getModel() );
            } else if ( method.equals( FUNCTION_GET ) ) {
                data.put( FUNCTION_HAS_CAPABILITY, checkAllCapabilties() );
                data.put( SOFTWARE_VERSION, getSoftwareVersion() );
                data.put( FUNCTION_HAS_PERMISSION, checkAllPermissions() );
                data.put( MODEL, getModel() );
                data.put( FUNCTION_HAS_DATA_COVERAGE, hasDataCoverage() );
                data.put( SCRIPT_API_VERSION, getSoftwareVersion() );
                data.put( FUNCTION_IS_MASS_STORAGE_ACTIVE, isMassStorageActive() );
            }
        } catch( Exception e ) {
            msg = e.getMessage();
            code = JSExtensionReturnValue.FAIL;
        }

        returnValue = new JSExtensionReturnValue( msg, code, data ).getReturnValue();

        response.setPostData( returnValue.toString().getBytes() );
    }
View Full Code Here

TOP

Related Classes of blackberry.common.util.json4j.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.