use of javax.json.stream.JsonParser in project wildfly by wildfly.
the class JsonTestCase method testJsonServlet.
@Test
public void testJsonServlet() throws Exception {
final String result = HttpRequest.get(url + "json", 10, TimeUnit.SECONDS);
final JsonParser parser = Json.createParser(new StringReader(result));
String key = null;
String value = null;
while (parser.hasNext()) {
final Event event = parser.next();
switch(event) {
case KEY_NAME:
key = parser.getString();
break;
case VALUE_STRING:
value = parser.getString();
break;
}
}
parser.close();
Assert.assertEquals("Key should be \"name\"", "name", key);
Assert.assertEquals("Value should be \"value\"", "value", value);
}
use of javax.json.stream.JsonParser in project Payara by payara.
the class RemoteRestAdminCommand method parseMetadata.
/**
* Parse the JSon metadata for the command.
*
* @param str the string
* @return the etag to compare the command cache model
*/
private CachedCommandModel parseMetadata(String str, String etag) {
if (logger.isLoggable(Level.FINER)) {
// XXX - assume "debug" == "FINER"
logger.finer("------- RAW METADATA RESPONSE ---------");
logger.log(Level.FINER, "ETag: {0}", etag);
logger.finer(str);
logger.finer("------- RAW METADATA RESPONSE ---------");
}
if (str == null) {
return null;
}
try {
boolean sawFile = false;
JsonParser parser = Json.createParser(new StringReader(str));
parser.next();
JsonObject obj = parser.getObject();
obj = obj.getJsonObject("command");
CachedCommandModel cm = new CachedCommandModel(obj.getString("@name"), etag);
cm.dashOk = parseBoolean(obj, "@unknown-options-are-operands", false);
cm.managedJob = parseBoolean(obj, "@managed-job", false);
cm.setUsage(obj.getString("usage", null));
JsonValue optns = obj.get("option");
if (!JsonValue.NULL.equals(optns) && optns != null) {
JsonArray jsonOptions;
if (optns instanceof JsonArray) {
jsonOptions = (JsonArray) optns;
} else {
JsonArrayBuilder optBuilder = Json.createArrayBuilder();
optBuilder.add(optns);
jsonOptions = optBuilder.build();
}
for (int i = 0; i < jsonOptions.size(); i++) {
JsonObject jsOpt = jsonOptions.getJsonObject(i);
String type = jsOpt.getString("@type");
ParamModelData opt = new ParamModelData(jsOpt.getString("@name"), typeOf(type), parseBoolean(jsOpt, "@optional", false), jsOpt.getString("@default", null), jsOpt.getString("@short", null), parseBoolean(jsOpt, "@obsolete", false), jsOpt.getString("@alias", null));
opt.param._acceptableValues = jsOpt.getString("@acceptable-values", "");
if ("PASSWORD".equals(type)) {
opt.param._password = true;
opt.prompt = jsOpt.getString("@prompt", null);
opt.promptAgain = jsOpt.getString("@prompt-again", null);
} else if ("FILE".equals(type)) {
sawFile = true;
}
if (parseBoolean(jsOpt, "@primary", false)) {
opt.param._primary = true;
}
if (parseBoolean(jsOpt, "@multiple", false)) {
if (opt.type == File.class) {
opt.type = File[].class;
} else {
opt.type = List.class;
}
opt.param._multiple = true;
}
cm.add(opt);
}
}
if (sawFile) {
cm.add(new ParamModelData("upload", Boolean.class, true, null));
addedUploadOption = true;
cm.setAddedUploadOption(true);
}
if (notify) {
cm.add(new ParamModelData("notify", Boolean.class, false, "false"));
}
this.usage = cm.getUsage();
return cm;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Can not parse command metadata", ex);
return null;
}
}
use of javax.json.stream.JsonParser in project Payara by payara.
the class ActionReportJsonProprietaryReader method readFrom.
@Override
public ActionReport readFrom(final InputStream is, final String contentType) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtils.copy(is, baos, 0);
String str = baos.toString("UTF-8");
try {
JsonParser parser = Json.createParser(new StringReader(str));
parser.next();
JsonObject json = parser.getObject();
// JsonObject jsonObject = json.getJsonObject("action-report");
CliActionReport result = new CliActionReport();
fillActionReport(result, json);
return result;
} catch (JsonException ex) {
LoggerRef.logger.log(Level.SEVERE, AdminLoggerInfo.mUnexpectedException, ex);
throw new IOException(ex);
}
}
use of javax.json.stream.JsonParser in project Payara by payara.
the class AdminCommandStateJsonProprietaryReader method readFrom.
@Override
public AdminCommandState readFrom(final InputStream is, final String contentType) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtils.copy(is, baos, 0);
String str = baos.toString("UTF-8");
try {
JsonParser parser = Json.createParser(new StringReader(str));
parser.next();
JsonObject json = parser.getObject();
return readAdminCommandState(json);
} catch (JsonException ex) {
LoggerRef.logger.log(Level.SEVERE, AdminLoggerInfo.mUnexpectedException, ex);
throw new IOException(ex);
}
}
use of javax.json.stream.JsonParser in project Payara by payara.
the class JsonHashMapProvider method readFrom.
@Override
public HashMap<String, String> readFrom(Class<HashMap<String, String>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream in) throws IOException {
HashMap map = new HashMap();
try {
JsonParser parser = Json.createParser(in);
JsonObject obj;
if (parser.hasNext()) {
parser.next();
obj = parser.getObject();
} else {
obj = JsonValue.EMPTY_JSON_OBJECT;
}
for (String k : obj.keySet()) {
JsonValue value = obj.get(k);
if (value.getValueType() == ValueType.STRING) {
map.put(k, ((JsonString) value).getString());
} else {
map.put(k, value.toString());
}
}
return map;
} catch (JsonException ex) {
// map.put("error", "Entity Parsing Error: " + ex.getMessage());
return map;
}
}
Aggregations