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;
}
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));
});
}
}
}
}
}
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");
}
}
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);
}
}
}
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);
}
Aggregations