use of javax.json.JsonException in project sling by apache.
the class ValidationMojo method validate.
private void validate(final File directory, final String fileName) throws MojoExecutionException {
getLog().debug("Validating " + fileName);
final File file = new File(directory, fileName);
if (file.isFile()) {
if (fileName.endsWith(".json") && !this.skipJson) {
getLog().debug("Validation JSON file " + fileName);
FileInputStream fis = null;
String json = null;
try {
fis = new FileInputStream(file);
json = IOUtils.toString(fis, CharEncoding.UTF_8);
} catch (IOException e) {
throw new MojoExecutionException("An Error occured while validating the file '" + fileName + "'", e);
} finally {
IOUtils.closeQuietly(fis);
}
// validate JSON
try {
JsonSupport.validateJsonStructure(json, jsonQuoteTick);
} catch (JsonException e) {
throw new MojoExecutionException("An Error occured while validating the file '" + fileName + "'", e);
}
}
}
}
use of javax.json.JsonException in project sling by apache.
the class SlingInfoServlet method renderJson.
private void renderJson(final SlingHttpServletResponse response, final Map<String, String> data) throws IOException {
// render data in JSON format
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
final Writer out = response.getWriter();
final JsonGenerator w = Json.createGenerator(out);
try {
w.writeStartObject();
for (final Map.Entry<String, String> e : data.entrySet()) {
w.write(e.getKey(), e.getValue());
}
w.writeEnd();
} catch (JsonException jse) {
out.write(jse.toString());
out.flush();
} finally {
w.flush();
}
}
use of javax.json.JsonException in project sling by apache.
the class JsonRenderer method setup.
public void setup(HttpServletResponse response, String pageTitle) throws IOException, UnsupportedEncodingException {
if (writer != null) {
throw new IllegalStateException("Output Writer already set");
}
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
writer = Json.createGenerator(response.getWriter());
try {
writer.writeStartArray();
} catch (JsonException jex) {
throw (IOException) new IOException().initCause(jex);
}
}
use of javax.json.JsonException in project sling by apache.
the class PostServletOrderTest method verifyOrder.
/**
* Verify node order
*/
private void verifyOrder(String parentUrl, String[] names) throws IOException {
// check that nodes appear in creation order in their parent's list of children
final String content = getContent(parentUrl + ".1.json", CONTENT_TYPE_JSON);
String expected = "";
for (String n : names) {
expected += n + ",";
}
//assertJavascript(expected, content, TEST_SCRIPT);
try {
String actual = "";
JsonObject obj = JsonUtil.parseObject(content);
for (String name : obj.keySet()) {
Object o = obj.get(name);
if (o instanceof JsonObject) {
actual += name + ",";
}
}
assertEquals(expected, actual);
} catch (JsonException e) {
throw new IOException(e.toString());
}
}
use of javax.json.JsonException in project sling by apache.
the class JsonObjectCreator method createProperty.
/**
* Write a single property
*/
private static void createProperty(final JsonObjectBuilder obj, final ValueMap valueMap, final String key, final Object value) {
Object[] values = null;
if (value.getClass().isArray()) {
if (value instanceof long[]) {
values = ArrayUtils.toObject((long[]) value);
} else if (value instanceof int[]) {
values = ArrayUtils.toObject((int[]) value);
} else if (value instanceof double[]) {
values = ArrayUtils.toObject((double[]) value);
} else if (value instanceof byte[]) {
values = ArrayUtils.toObject((byte[]) value);
} else if (value instanceof float[]) {
values = ArrayUtils.toObject((float[]) value);
} else if (value instanceof short[]) {
values = ArrayUtils.toObject((short[]) value);
} else if (value instanceof long[]) {
values = ArrayUtils.toObject((long[]) value);
} else if (value instanceof boolean[]) {
values = ArrayUtils.toObject((boolean[]) value);
} else if (value instanceof char[]) {
values = ArrayUtils.toObject((char[]) value);
} else {
values = (Object[]) value;
}
// write out empty array
if (values.length == 0) {
obj.add(key, Json.createArrayBuilder());
return;
}
}
// special handling for binaries: we dump the length and not the data!
if (value instanceof InputStream || (values != null && values[0] instanceof InputStream)) {
// in the name, and the value should be the size of the binary data
try {
if (values == null) {
obj.add(":" + key, getLength(valueMap, -1, key, (InputStream) value));
} else {
final JsonArrayBuilder result = Json.createArrayBuilder();
for (int i = 0; i < values.length; i++) {
result.add(getLength(valueMap, i, key, (InputStream) values[i]));
}
obj.add(":" + key, result);
}
} catch (final JsonException ignore) {
// we ignore this
LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
}
return;
}
try {
if (!value.getClass().isArray()) {
obj.add(key, getValue(value));
} else {
final JsonArrayBuilder result = Json.createArrayBuilder();
for (Object v : values) {
result.add(getValue(v));
}
obj.add(key, result);
}
} catch (final JsonException ignore) {
// we ignore this
LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
}
}
Aggregations