Example usage for javax.json JsonObject getBoolean

List of usage examples for javax.json JsonObject getBoolean

Introduction

In this page you can find the example usage for javax.json JsonObject getBoolean.

Prototype

boolean getBoolean(String name);

Source Link

Document

Returns the boolean value of the associated mapping for the specified name.

Usage

From source file:Main.java

public static void main(String[] args) {
    String personJSONData = "  {" + "   \"name\": \"Jack\", " + "   \"age\" : 13, "
            + "   \"isMarried\" : false, " + "   \"address\": { " + "     \"street\": \"#1234, Main Street\", "
            + "     \"zipCode\": \"123456\" " + "   }, "
            + "   \"phoneNumbers\": [\"011-111-1111\", \"11-111-1111\"] " + " }";

    JsonReader reader = Json.createReader(new StringReader(personJSONData));

    JsonObject personObject = reader.readObject();

    reader.close();/*  w w  w  .  j a va2  s.  co m*/

    System.out.println("Name   : " + personObject.getString("name"));
    System.out.println("Age    : " + personObject.getInt("age"));
    System.out.println("Married: " + personObject.getBoolean("isMarried"));

    JsonObject addressObject = personObject.getJsonObject("address");
    System.out.println("Address: ");
    System.out.println(addressObject.getString("street"));
    System.out.println(addressObject.getString("zipCode"));

    System.out.println("Phone  : ");
    JsonArray phoneNumbersArray = personObject.getJsonArray("phoneNumbers");
    for (JsonValue jsonValue : phoneNumbersArray) {
        System.out.println(jsonValue.toString());
    }
}

From source file:io.hakbot.util.JsonUtil.java

/**
 * Returns the value for the specified parameter name included in the specified json object. If json
 * object is null or specified name cannot be found, method returns null.
 *//*ww w.j a va2 s . co m*/
public static boolean getBoolean(JsonObject json, String name) {
    return (json != null && json.containsKey(name)) && json.getBoolean(name);
}

From source file:adalid.jaas.google.GoogleRecaptcha.java

private static boolean success(String jsonString) {
    JsonObject jsonObject;
    try (JsonReader jsonReader = Json.createReader(new StringReader(jsonString))) {
        jsonObject = jsonReader.readObject();
        return jsonObject.getBoolean("success");
    }//from w w w  .j ava  2 s .co m
}

From source file:ke.co.tawi.babblesms.server.servlet.accountmngmt.Login.java

public static boolean validateCaptcha(String gRecaptchaResponse) throws IOException {
    if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
        return false;
    }//from   w w w.  ja  v  a 2  s. c o m

    try {
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        // add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String postParams = "secret=" + secret + "&response=" + gRecaptchaResponse;

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + postParams);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());

        //parse JSON response and return 'success' value
        JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
        JsonObject jsonObject = jsonReader.readObject();
        jsonReader.close();

        return jsonObject.getBoolean("success");
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:fr.ortolang.diffusion.client.cmd.ReindexAllRootCollectionCommand.java

@Override
public void execute(String[] args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;//  w  w  w .ja  v  a 2s .  co  m
    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("h")) {
            help();
        }

        String[] credentials = getCredentials(cmd);
        String username = credentials[0];
        String password = credentials[1];

        boolean fakeMode = cmd.hasOption("F");

        OrtolangClient client = OrtolangClient.getInstance();
        if (username.length() > 0) {
            client.getAccountManager().setCredentials(username, password);
            client.login(username);
        }
        System.out.println("Connected as user: " + client.connectedProfile());
        System.out.println("Looking for root collection ...");

        // Looking for root collection
        List<String> rootCollectionKeys = new ArrayList<>();

        int offset = 0;
        int limit = 100;
        JsonObject listOfObjects = client.listObjects("core", "collection", "PUBLISHED", offset, limit);
        JsonArray keys = listOfObjects.getJsonArray("entries");

        while (!keys.isEmpty()) {
            for (JsonString objectKey : keys.getValuesAs(JsonString.class)) {
                JsonObject objectRepresentation = client.getObject(objectKey.getString());
                JsonObject objectProperty = objectRepresentation.getJsonObject("object");
                boolean isRoot = objectProperty.getBoolean("root");
                if (isRoot) {
                    rootCollectionKeys.add(objectKey.getString());
                }
            }
            offset += limit;
            listOfObjects = client.listObjects("core", "collection", "PUBLISHED", offset, limit);
            keys = listOfObjects.getJsonArray("entries");
        }

        System.out.println("Reindex keys : " + rootCollectionKeys);
        if (!fakeMode) {
            for (String key : rootCollectionKeys) {
                client.reindex(key);
            }
        }

        client.logout();
        client.close();

    } catch (ParseException e) {
        System.out.println("Failed to parse command line properties " + e.getMessage());
        help();
    } catch (OrtolangClientException | OrtolangClientAccountException e) {
        System.out.println("Unexpected error !!");
        e.printStackTrace();
    }
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkBooleanField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("isCriticalSystemObject", true);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getIsCriticalSystemObject());

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (json.getBoolean("isCriticalSystemObject"));
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleExecuteImageNotFound() throws Exception {
    UnitTestUtils.mockJobConsoleLogger();
    PowerMockito.mockStatic(DockerUtils.class);
    PowerMockito.doThrow(new ImageNotFoundException("idont:exist")).when(DockerUtils.class);
    DockerUtils.pullImage(anyString());//  www. ja va2s .com

    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "execute");
    JsonObject requestBody = Json.createObjectBuilder()
            .add("config",
                    Json.createObjectBuilder()
                            .add("IMAGE", Json.createObjectBuilder().add("value", "idont:exist").build())
                            .add("COMMAND", Json.createObjectBuilder().add("value", "ls").build())
                            .add("ARGUMENTS", Json.createObjectBuilder().build()).build())
            .add("context", Json.createObjectBuilder().add("workingDirectory", "pipelines/test")
                    .add("environmentVariables", Json.createObjectBuilder().build()).build())
            .build();
    request.setRequestBody(requestBody.toString());
    final GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected 2xx response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());
    final JsonObject responseBody = Json.createReader(new StringReader(response.responseBody())).readObject();
    assertEquals("Expected failure", Boolean.FALSE, responseBody.getBoolean("success"));
    assertEquals("Message wrong", "Image 'idont:exist' not found", responseBody.getString("message"));
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleExecuteCleanupError() throws Exception {
    UnitTestUtils.mockJobConsoleLogger();
    PowerMockito.mockStatic(DockerUtils.class);
    PowerMockito.doNothing().when(DockerUtils.class);
    DockerUtils.pullImage(anyString());/* ww w  . j a v a 2 s.c o  m*/
    when(DockerUtils.createContainer(anyString(), anyString(), anyMap())).thenReturn("123");
    when(DockerUtils.getContainerUid(anyString())).thenReturn("4:5");
    when(DockerUtils.execCommand(anyString(), any(), anyString(), any())).thenReturn(0);
    PowerMockito.doThrow(new DockerException("FAIL")).when(DockerUtils.class);
    DockerUtils.removeContainer(anyString());
    PowerMockito.mockStatic(SystemHelper.class);
    when(SystemHelper.getSystemUid()).thenReturn("7:8");

    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "execute");
    JsonObject requestBody = Json.createObjectBuilder()
            .add("config",
                    Json.createObjectBuilder()
                            .add("IMAGE", Json.createObjectBuilder().add("value", "ubuntu:latest").build())
                            .add("COMMAND", Json.createObjectBuilder().add("value", "ls").build())
                            .add("ARGUMENTS", Json.createObjectBuilder().build()).build())
            .add("context", Json.createObjectBuilder().add("workingDirectory", "pipelines/test")
                    .add("environmentVariables", Json.createObjectBuilder().build()).build())
            .build();
    request.setRequestBody(requestBody.toString());
    final GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected 2xx response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());
    final JsonObject responseBody = Json.createReader(new StringReader(response.responseBody())).readObject();
    assertEquals("Expected failure", Boolean.FALSE, responseBody.getBoolean("success"));
    assertEquals("Wrong message", "FAIL", responseBody.getString("message"));
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleExecuteError() throws Exception {
    UnitTestUtils.mockJobConsoleLogger();
    PowerMockito.mockStatic(DockerUtils.class);
    PowerMockito.doNothing().when(DockerUtils.class);
    DockerUtils.pullImage(anyString());//from   w w w  .ja  v  a 2  s  . c o  m
    when(DockerUtils.createContainer(anyString(), anyString(), anyMap())).thenReturn("123");
    when(DockerUtils.getContainerUid(anyString())).thenReturn("4:5");
    when(DockerUtils.execCommand(anyString(), any(), anyString(), any()))
            .thenThrow(new DockerException("FAIL"));
    PowerMockito.doNothing().when(DockerUtils.class);
    DockerUtils.removeContainer(anyString());
    PowerMockito.mockStatic(SystemHelper.class);
    when(SystemHelper.getSystemUid()).thenReturn("7:8");

    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "execute");
    JsonObject requestBody = Json.createObjectBuilder()
            .add("config",
                    Json.createObjectBuilder()
                            .add("IMAGE", Json.createObjectBuilder().add("value", "ubuntu:latest").build())
                            .add("COMMAND", Json.createObjectBuilder().add("value", "ls").build())
                            .add("ARGUMENTS", Json.createObjectBuilder().build()).build())
            .add("context", Json.createObjectBuilder().add("workingDirectory", "pipelines/test")
                    .add("environmentVariables", Json.createObjectBuilder().build()).build())
            .build();
    request.setRequestBody(requestBody.toString());
    final GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected 2xx response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());
    final JsonObject responseBody = Json.createReader(new StringReader(response.responseBody())).readObject();
    assertEquals("Expected failure", Boolean.FALSE, responseBody.getBoolean("success"));
    assertEquals("Wrong message", "FAIL", responseBody.getString("message"));
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleExecuteNestedCleanupError() throws Exception {
    UnitTestUtils.mockJobConsoleLogger();
    PowerMockito.mockStatic(DockerUtils.class);
    PowerMockito.doNothing().when(DockerUtils.class);
    DockerUtils.pullImage(anyString());/*www .  jav a  2s  . c  o  m*/
    when(DockerUtils.createContainer(anyString(), anyString(), anyMap())).thenReturn("123");
    when(DockerUtils.getContainerUid(anyString())).thenReturn("4:5");
    when(DockerUtils.execCommand(anyString(), any(), anyString(), any()))
            .thenThrow(new DockerException("FAIL1"));
    PowerMockito.doThrow(new DockerException("FAIL2")).when(DockerUtils.class);
    DockerUtils.removeContainer(anyString());
    PowerMockito.mockStatic(SystemHelper.class);
    when(SystemHelper.getSystemUid()).thenReturn("7:8");

    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "execute");
    JsonObject requestBody = Json.createObjectBuilder()
            .add("config",
                    Json.createObjectBuilder()
                            .add("IMAGE", Json.createObjectBuilder().add("value", "ubuntu:latest").build())
                            .add("COMMAND", Json.createObjectBuilder().add("value", "ls").build())
                            .add("ARGUMENTS", Json.createObjectBuilder().build()).build())
            .add("context", Json.createObjectBuilder().add("workingDirectory", "pipelines/test")
                    .add("environmentVariables", Json.createObjectBuilder().build()).build())
            .build();
    request.setRequestBody(requestBody.toString());
    final GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected 2xx response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());
    final JsonObject responseBody = Json.createReader(new StringReader(response.responseBody())).readObject();
    assertEquals("Expected failure", Boolean.FALSE, responseBody.getBoolean("success"));
    assertEquals("Wrong message", "FAIL1", responseBody.getString("message"));
}