Example usage for com.google.gson JsonObject addProperty

List of usage examples for com.google.gson JsonObject addProperty

Introduction

In this page you can find the example usage for com.google.gson JsonObject addProperty.

Prototype

public void addProperty(String property, Character value) 

Source Link

Document

Convenience method to add a char member.

Usage

From source file:com.amsterdam.marktbureau.makkelijkemarkt.DagvergunningFragment.java

License:Mozilla Public License

/**
 * Create a json object from the dagvergunning values
 * @return json object/*from   w  ww. j  av  a2  s.c  o  m*/
 */
private JsonObject dagvergunningToJson() {

    JsonObject dagvergunningPayload = new JsonObject();
    dagvergunningPayload.addProperty(
            getString(R.string.makkelijkemarkt_api_dagvergunning_payload_erkenningsnummer), mErkenningsnummer);
    dagvergunningPayload.addProperty(getString(R.string.makkelijkemarkt_api_dagvergunning_payload_markt_id),
            mMarktId);
    dagvergunningPayload.addProperty(getString(R.string.makkelijkemarkt_api_dagvergunning_payload_dag),
            mDagToday);
    dagvergunningPayload.addProperty(getString(R.string.makkelijkemarkt_api_dagvergunning_payload_aanwezig),
            mKoopmanAanwezig);

    // get product values
    String[] productParams = getResources().getStringArray(R.array.array_product_param);
    for (int i = 0; i < productParams.length; i++) {
        if (mProducten.get(productParams[i]) > 0) {
            dagvergunningPayload.addProperty(productParams[i], mProducten.get(productParams[i]));
        }
    }

    if (mErkenningsnummerInvoerMethode != null) {
        dagvergunningPayload.addProperty(
                getString(R.string.makkelijkemarkt_api_dagvergunning_payload_erkenningsnummer_invoer_methode),
                mErkenningsnummerInvoerMethode);
    }

    if (mNotitie != null) {
        dagvergunningPayload.addProperty(getString(R.string.makkelijkemarkt_api_dagvergunning_payload_notitie),
                mNotitie);
    }

    DateFormat datumtijdFormat = new SimpleDateFormat(getString(R.string.date_format_datumtijd));
    dagvergunningPayload.addProperty(
            getString(R.string.makkelijkemarkt_api_dagvergunning_payload_registratie_datumtijd),
            String.valueOf(datumtijdFormat.format(new Date())));

    // add the location from gps
    if (mRegistratieGeolocatieLatitude != -1 && mRegistratieGeolocatieLongitude != -1) {
        JsonArray geolocation = new JsonArray();
        geolocation.add(mRegistratieGeolocatieLatitude);
        geolocation.add(mRegistratieGeolocatieLongitude);
        dagvergunningPayload.add(
                getString(R.string.makkelijkemarkt_api_dagvergunning_payload_registratie_geolocatie),
                geolocation);
    }

    // if set, add vervanger erkenningsnummer
    if (mVervangerErkenningsnummer != null) {
        dagvergunningPayload.addProperty(
                getString(R.string.makkelijkemarkt_api_dagvergunning_payload_vervanger_erkenningsnummer),
                mVervangerErkenningsnummer);
    }

    return dagvergunningPayload;
}

From source file:com.amsterdam.marktbureau.makkelijkemarkt.LoginFragment.java

License:Mozilla Public License

/**
 * OnClick on the login button authenticate the user with the api
 *///from  ww  w  .j  a v  a  2  s .c  o m
@OnClick(R.id.login_button)
public void authenticateAccount() {

    // if user entered a password try to authenticate with the api
    if (mPassword.getText().toString().trim().equals("")) {
        mToast = Utility.showToast(getContext(), mToast, getString(R.string.notice_login_enter_password));
    } else {

        // prepare the json payload for the post request from the entered login details
        JsonObject auth = new JsonObject();
        auth.addProperty(getString(R.string.makkelijkemarkt_api_login_payload_accound_id_name),
                String.valueOf(mSelectedAccountId));
        auth.addProperty(getString(R.string.makkelijkemarkt_api_login_payload_password_name),
                mPassword.getText().toString());

        // show progress dialog
        mLoginProcessDialog.show();

        // create a login post request and add the account details as json
        ApiPostLoginBasicId postLogin = new ApiPostLoginBasicId(getContext());
        postLogin.setPayload(auth);
        if (!postLogin.enqueue(this)) {
            mLoginProcessDialog.dismiss();
        }
    }
}

From source file:com.amsterdam.marktbureau.makkelijkemarkt.NotitieFragment.java

License:Mozilla Public License

/**
 * Onclick save the notitie//from  w w w .ja v a  2 s.  c o  m
 */
@OnClick(R.id.notitie_save)
public void saveNotitie() {
    if (!mBerichtEditText.getText().toString().trim().isEmpty()) {
        JsonObject notitiePayload = new JsonObject();

        // show progress dialog
        mProgressDialog.show();

        // bericht
        String bericht = mBerichtEditText.getText().toString().trim();
        notitiePayload.addProperty(getString(R.string.makkelijkemarkt_api_notitie_payload_bericht), bericht);

        // get the markt id from the shared preferences
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getContext());
        int marktId = settings.getInt(getString(R.string.sharedpreferences_key_markt_id), 0);
        notitiePayload.addProperty(getString(R.string.makkelijkemarkt_api_notitie_payload_markt_id), marktId);

        // get the date of today for the dag param
        SimpleDateFormat sdf = new SimpleDateFormat(getString(R.string.date_format_dag));
        String dag = sdf.format(new Date());
        notitiePayload.addProperty(getString(R.string.makkelijkemarkt_api_notitie_payload_dag), dag);

        // send post call to the api
        ApiPostNotitie postNotitie = new ApiPostNotitie(getContext());
        postNotitie.setPayload(notitiePayload);
        postNotitie.enqueue(this);

    } else {
        Utility.showToast(getContext(), mToast, getString(R.string.notice_notitie_enter_text));
    }
}

From source file:com.android.cloudfiles.cloudfilesforandroid.CloudFiles.java

License:Apache License

private String generateToken(String userName, String apiKey) throws IOException {

    JsonObject cred = new JsonObject();
    cred.addProperty("username", userName);
    cred.addProperty("apiKey", apiKey);
    JsonObject rax = new JsonObject();
    rax.add("RAX-KSKEY:apiKeyCredentials", cred);

    JsonObject obj = new JsonObject();
    obj.add("auth", rax);
    String json = obj.toString();

    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder().url(tokenURL).post(body).build();
    Response response = client.newCall(request).execute();

    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    return response.body().string();
}

From source file:com.android.tools.idea.editors.hierarchyview.HierarchyViewCaptureOptions.java

License:Apache License

public String serialize() {
    JsonObject obj = new JsonObject();
    obj.addProperty(VERSION, myVersion);
    obj.addProperty(TITLE, myTitle);//ww  w  .j ava  2s . co m
    return obj.toString();
}

From source file:com.apifest.example.AddSenderIdInBodyAction.java

License:Apache License

@Override
public HttpRequest execute(HttpRequest req, String internalURI, HttpResponse tokenValidationResponse)
        throws MappingException {
    JsonParser parser = new JsonParser();
    JsonObject json = parser.parse(new String(req.getContent().toString(CharsetUtil.UTF_8))).getAsJsonObject();
    log.info("request body: " + json);
    json.addProperty("senderId", "1232");
    byte[] newContent = json.toString().getBytes(CharsetUtil.UTF_8);
    ChannelBuffer buf = ChannelBuffers.copiedBuffer(newContent);
    req.setContent(buf);/*from   w w  w .  j av  a2  s .co m*/
    HttpHeaders.setContentLength(req, newContent.length);
    return req;
}

From source file:com.apifest.oauth20.HttpRequestHandler.java

License:Apache License

protected HttpResponse handleAuthorize(HttpRequest req) {
    HttpResponse response = null;// w  w  w .  j a  v a  2 s  . c  om
    try {
        String redirectURI = auth.issueAuthorizationCode(req);
        // TODO: validation http protocol?
        log.debug("redirectURI: {}", redirectURI);

        // return auth_code
        JsonObject obj = new JsonObject();
        obj.addProperty("redirect_uri", redirectURI);
        response = Response.createOkResponse(obj.toString());
        accessTokensLog.info("authCode {}", obj.toString());
    } catch (OAuthException ex) {
        response = Response.createOAuthExceptionResponse(ex);
        invokeExceptionHandler(ex, req);
    }
    return response;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public boolean storeScope(Scope scope) {
    boolean stored = false;
    String id = scope.getScope();
    Gson gson = new Gson();
    String json = gson.toJson(scope);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    jsonObj.remove("scope");
    // use scope name as _id
    jsonObj.addProperty(ID_NAME, id);

    try {/*from  ww w.  j av  a2 s.  c o m*/
        // use ObjectMapper in order to represent expiresIn as integer not as double - 100 instead of 100.00
        Map<String, Object> result = new ObjectMapper().readValue(jsonObj.toString(), Map.class);

        // if scope already exits, updates it, otherwise creates the scope
        BasicDBObject query = new BasicDBObject(ID_NAME, id);
        BasicDBObject newObject = new BasicDBObject(result);
        DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
        coll.update(query, newObject, true, false);
        stored = true;
    } catch (JsonParseException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (JsonMappingException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (IOException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    }

    return stored;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

protected String constructDbId(Object object) {
    Gson gson = new Gson();
    String json = gson.toJson(object);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    if (jsonObj.has("id")) {
        String id = jsonObj.get("id").getAsString();
        jsonObj.remove("id");
        jsonObj.addProperty(ID_NAME, id);
    }//from   w  w w.  j a va 2 s .  c om
    return jsonObj.toString();
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public boolean storeScope(Scope scope) {
    boolean stored = false;
    String id = scope.getScope();
    Gson gson = new Gson();
    String json = gson.toJson(scope);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    jsonObj.remove("scope");
    // use scope name as _id
    jsonObj.addProperty(CLIENTS_ID, id);

    try {/*  www .  j a  v  a2  s  .  c o  m*/
        // use ObjectMapper in order to represent expiresIn as integer not as double - 100 instead of 100.00
        Map<String, Object> result = new ObjectMapper().readValue(jsonObj.toString(), Map.class);

        // if scope already exits, updates it, otherwise creates the scope
        BasicDBObject query = new BasicDBObject(CLIENTS_ID, id);
        BasicDBObject newObject = new BasicDBObject(result);
        DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
        coll.update(query, newObject, true, false);
        stored = true;
    } catch (JsonParseException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (JsonMappingException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (IOException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    }

    return stored;
}