use of javax.json.JsonWriter in project javaee7-samples by javaee-samples.
the class DOMGeneratorTest method testNestedStructure.
@Test
public void testNestedStructure() throws JSONException {
JsonObject jsonObject = Json.createObjectBuilder().add("title", "The Matrix").add("year", 1999).add("cast", Json.createArrayBuilder().add("Keanu Reaves").add("Laurence Fishburne").add("Carrie-Anne Moss")).build();
StringWriter w = new StringWriter();
try (JsonWriter writer = Json.createWriter(w)) {
writer.write(jsonObject);
}
JSONAssert.assertEquals("{\"title\":\"The Matrix\",\"year\":1999,\"cast\":[\"Keanu Reaves\",\"Laurence Fishburne\",\"Carrie-Anne Moss\"]}", w.toString(), JSONCompareMode.STRICT);
}
use of javax.json.JsonWriter in project sling by apache.
the class JsonReaderTest method toJsonArray.
protected String toJsonArray(String[] array) {
JsonArrayBuilder builder = Json.createArrayBuilder();
for (String value : array) {
builder.add(value);
}
StringWriter stringWriter = new StringWriter();
try (JsonWriter writer = Json.createWriter(stringWriter)) {
writer.writeArray(builder.build());
}
return stringWriter.toString();
}
use of javax.json.JsonWriter in project sling by apache.
the class GenerateAdapterMetadataMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
try {
final Map<String, Object> descriptor = new HashMap<>();
final AnnotationDB annotationDb = new AnnotationDB();
annotationDb.scanArchives(buildOutputDirectory.toURI().toURL());
final Set<String> annotatedClassNames = new HashSet<String>();
addAnnotatedClasses(annotationDb, annotatedClassNames, Adaptable.class);
addAnnotatedClasses(annotationDb, annotatedClassNames, Adaptables.class);
for (final String annotatedClassName : annotatedClassNames) {
getLog().info(String.format("found adaptable annotation on %s", annotatedClassName));
final String pathToClassFile = annotatedClassName.replace('.', '/') + ".class";
final File classFile = new File(buildOutputDirectory, pathToClassFile);
final FileInputStream input = new FileInputStream(classFile);
final ClassReader classReader;
try {
classReader = new ClassReader(input);
} finally {
input.close();
}
final ClassNode classNode = new ClassNode();
classReader.accept(classNode, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
@SuppressWarnings("unchecked") final List<AnnotationNode> annotations = classNode.invisibleAnnotations;
for (final AnnotationNode annotation : annotations) {
if (ADAPTABLE_DESC.equals(annotation.desc)) {
parseAdaptableAnnotation(annotation, classNode, descriptor);
} else if (ADAPTABLES_DESC.equals(annotation.desc)) {
parseAdaptablesAnnotation(annotation, classNode, descriptor);
}
}
}
final File outputFile = new File(outputDirectory, fileName);
outputFile.getParentFile().mkdirs();
try (FileWriter writer = new FileWriter(outputFile);
JsonWriter jsonWriter = Json.createWriter(writer)) {
jsonWriter.writeObject(JsonSupport.toJson(descriptor));
}
addResource();
} catch (IOException e) {
throw new MojoExecutionException("Unable to generate metadata", e);
} catch (JsonException e) {
throw new MojoExecutionException("Unable to generate metadata", e);
}
}
use of javax.json.JsonWriter in project javaee7-samples by javaee-samples.
the class DOMGeneratorTest method testSimpleObject.
@Test
public void testSimpleObject() throws JSONException {
JsonObject jsonObject = Json.createObjectBuilder().add("apple", "red").add("banana", "yellow").build();
StringWriter w = new StringWriter();
try (JsonWriter writer = Json.createWriter(w)) {
writer.write(jsonObject);
}
JSONAssert.assertEquals("{\"apple\" : \"red\", \"banana\" : \"yellow\" }", w.toString(), JSONCompareMode.STRICT);
}
use of javax.json.JsonWriter in project javaee7-samples by javaee-samples.
the class DOMGeneratorTest method testEmptyObject.
@Test
public void testEmptyObject() throws JSONException {
JsonObject jsonObject = Json.createObjectBuilder().build();
StringWriter w = new StringWriter();
try (JsonWriter writer = Json.createWriter(w)) {
writer.write(jsonObject);
}
JSONAssert.assertEquals("{}", w.toString(), JSONCompareMode.STRICT);
}
Aggregations