Search in sources :

Example 26 with JsonReader

use of javax.json.JsonReader in project opentheso by miledrousset.

the class HandleClient method getIdHandle.

private String getIdHandle(String jsonText) {
    if (jsonText == null)
        return null;
    // {"responseCode":1,"handle":"20.500.11942/opentheso443"}
    JsonReader reader = Json.createReader(new StringReader(jsonText));
    JsonObject jsonObject = reader.readObject();
    reader.close();
    JsonString values = jsonObject.getJsonString("handle");
    if (values != null)
        return values.getString();
    return null;
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 27 with JsonReader

use of javax.json.JsonReader in project Payara by payara.

the class PayaraMicroDeployableContainer method processDeploymentAsJson.

private void processDeploymentAsJson(String deploymentInformation, HTTPContext httpContext) {
    Matcher jsonMatcher = jsonPattern.matcher(deploymentInformation);
    if (jsonMatcher.find()) {
        // Convert the deployment information into a JsonArray
        String jsonString = jsonMatcher.group("jsonArray");
        try (JsonReader reader = Json.createReader(new StringReader(jsonString))) {
            JsonArray array = reader.readArray();
            // For each deployed application
            for (JsonObject app : array.getValuesAs(JsonObject.class)) {
                // Print the application details
                printApplicationFound(app.getString("Name"));
                // Store all application servlet mappings
                Map<String, JsonObject> allMappings = new HashMap<>();
                // If there's only one module, the mappings and context root are directly under the application
                JsonObject appMappings = app.getJsonObject("Mappings");
                if (appMappings != null) {
                    // Get the context root and store the relevant servlets
                    String contextRoot = app.getString("Context Root");
                    allMappings.put(contextRoot, app.getJsonObject("Mappings"));
                } else {
                    JsonArray modules = app.getJsonArray("Modules");
                    modules.forEach(moduleValue -> {
                        // Get the context root and store the relevant servlets
                        JsonObject module = (JsonObject) moduleValue;
                        String contextRoot = module.getString("Context Root");
                        printModuleFound(module.getString("Name"), contextRoot);
                        allMappings.put(contextRoot, module);
                    });
                }
                // Handle each set of servlet mappings
                for (String contextRoot : allMappings.keySet()) {
                    JsonObject mappings = allMappings.get(contextRoot);
                    mappings.values().forEach(name -> {
                        String servletName = name.toString().replaceAll("\"", "");
                        printServletFound(servletName);
                        httpContext.add(new Servlet(servletName, contextRoot));
                    });
                }
            }
        }
    }
}
Also used : JsonArray(javax.json.JsonArray) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Servlet(org.jboss.arquillian.container.spi.client.protocol.metadata.Servlet)

Example 28 with JsonReader

use of javax.json.JsonReader in project quickstart by wildfly.

the class JSONRaceStage method run.

@Override
public void run(Race.Registration registration) throws Exception {
    // 1. build an object with nested structure
    JsonObject jsonObject = Json.createObjectBuilder().add("firstName", "John").add("lastName", "Smith").add("age", 25).add("address", Json.createObjectBuilder().add("streetAddress", "21 2nd Street").add("city", "New York").add("state", "NY").add("postalCode", "10021")).add("phoneNumber", Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "home").add("number", "212 555-1234")).add(Json.createObjectBuilder().add("type", "fax").add("number", "646 555-4567"))).build();
    // 2. write the object to a string
    StringWriter stringWriter = new StringWriter();
    try (JsonWriter writer = Json.createWriter(stringWriter)) {
        writer.write(jsonObject);
    }
    String toString = stringWriter.toString();
    // 3. read object from the string
    JsonObject fromString = null;
    try (JsonReader jsonReader = Json.createReader(new StringReader(toString))) {
        fromString = jsonReader.readObject();
    }
    // 4. sanity check :)
    if (!jsonObject.equals(fromString)) {
        throw new IllegalStateException("json object read from string does not equal the one built");
    }
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) JsonWriter(javax.json.JsonWriter)

Example 29 with JsonReader

use of javax.json.JsonReader in project javaee7-firstcup by ecabrerar.

the class DeviceWebSocketServer method handleMessage.

@OnMessage
public void handleMessage(String message, Session session) {
    try (JsonReader reader = Json.createReader(new StringReader(message))) {
        JsonObject jsonMessage = reader.readObject();
        if ("add".equals(jsonMessage.getString("action"))) {
            Device device = new Device();
            device.setName(jsonMessage.getString("name"));
            device.setDescription(jsonMessage.getString("description"));
            device.setType(jsonMessage.getString("type"));
            device.setStatus("Off");
            sessionHandler.addDevice(device);
        }
        if ("remove".equals(jsonMessage.getString("action"))) {
            int id = (int) jsonMessage.getInt("id");
            sessionHandler.removeDevice(id);
        }
        if ("toggle".equals(jsonMessage.getString("action"))) {
            int id = (int) jsonMessage.getInt("id");
            sessionHandler.toggleDevice(id);
        }
    }
}
Also used : Device(org.ecabrerar.examples.websockethome.model.Device) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) ServerEndpoint(javax.websocket.server.ServerEndpoint) OnMessage(javax.websocket.OnMessage)

Example 30 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromReaderTest method testNestedStructure.

@Test
public void testNestedStructure() throws JSONException {
    JsonReader jsonReader = Json.createReader(new StringReader("{" + "  \"title\":\"The Matrix\"," + "  \"year\":1999," + "  \"cast\":[" + "    \"Keanu Reaves\"," + "    \"Laurence Fishburne\"," + "    \"Carrie-Anne Moss\"" + "  ]" + "}"));
    JsonObject json = jsonReader.readObject();
    assertNotNull(json);
    assertFalse(json.isEmpty());
    assertTrue(json.containsKey("title"));
    assertEquals("The Matrix", json.getString("title"));
    assertTrue(json.containsKey("year"));
    assertEquals(1999, json.getInt("year"));
    assertTrue(json.containsKey("cast"));
    JsonArray jsonArr = json.getJsonArray("cast");
    assertNotNull(jsonArr);
    assertEquals(3, jsonArr.size());
    JSONAssert.assertEquals("[" + "    \"Keanu Reaves\"," + "    \"Laurence Fishburne\"," + "    \"Carrie-Anne Moss\"" + "  ]", jsonArr.toString(), JSONCompareMode.STRICT);
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonReader (javax.json.JsonReader)61 JsonObject (javax.json.JsonObject)49 StringReader (java.io.StringReader)31 JsonArray (javax.json.JsonArray)31 JsonString (javax.json.JsonString)25 Test (org.junit.Test)20 HashMap (java.util.HashMap)12 LinkedHashMap (java.util.LinkedHashMap)10 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)3 Response (com.ning.http.client.Response)3 File (java.io.File)3 JsonException (javax.json.JsonException)3 JsonValue (javax.json.JsonValue)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2