Search in sources :

Example 1 with JsonWriter

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);
}
Also used : StringWriter(java.io.StringWriter) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Example 2 with JsonWriter

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();
}
Also used : StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonWriter(javax.json.JsonWriter)

Example 3 with JsonWriter

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);
    }
}
Also used : AnnotationDB(org.scannotation.AnnotationDB) JsonException(javax.json.JsonException) ClassNode(org.objectweb.asm.tree.ClassNode) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) IOException(java.io.IOException) JsonWriter(javax.json.JsonWriter) FileInputStream(java.io.FileInputStream) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with JsonWriter

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);
}
Also used : StringWriter(java.io.StringWriter) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Example 5 with JsonWriter

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);
}
Also used : StringWriter(java.io.StringWriter) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Aggregations

JsonWriter (javax.json.JsonWriter)7 StringWriter (java.io.StringWriter)6 Test (org.junit.Test)4 JsonObject (javax.json.JsonObject)3 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 AttributeReference (com.torodb.core.language.AttributeReference)1 IndexType (com.torodb.core.model.IndexedAttributes.IndexType)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 JsonArray (javax.json.JsonArray)1 JsonException (javax.json.JsonException)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 ClassReader (org.objectweb.asm.ClassReader)1 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)1