Example usage for com.fasterxml.jackson.core JsonGenerator writeObject

List of usage examples for com.fasterxml.jackson.core JsonGenerator writeObject

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator writeObject.

Prototype

public abstract void writeObject(Object pojo) throws IOException, JsonProcessingException;

Source Link

Document

Method for writing given Java object (POJO) as Json.

Usage

From source file:com.iflytek.edu.cloud.frame.doc.BuildDocMain.java

public static void main(String[] args) throws Exception {
    ServiceDocBuilder docBuilder = new ServiceDocBuilder();
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    File jsonFile = getJsonFile();
    JsonGenerator jsonGenerator = mapper.getFactory().createGenerator(jsonFile, JsonEncoding.UTF8);
    jsonGenerator.writeObject(docBuilder.buildDoc());

    LOGGER.info("?API?" + jsonFile.getPath());
}

From source file:org.datagator.tools.importer.Main.java

public static void main(String[] args) throws IOException {

    int columnHeaders = 0; // cli input
    int rowHeaders = 0; // cli input

    try {/*w  w  w. j a v a 2  s.  c  o m*/
        CommandLine cmds = parser.parse(opts, args);
        if (cmds.hasOption("filter")) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        if (cmds.hasOption("layout")) {
            String[] layout = cmds.getOptionValues("layout");
            if ((layout == null) || (layout.length != 2)) {
                throw new IllegalArgumentException("Bad layout.");
            }
            try {
                columnHeaders = Integer.valueOf(layout[0]);
                rowHeaders = Integer.valueOf(layout[1]);
                if ((columnHeaders < 0) || (rowHeaders < 0)) {
                    throw new IllegalArgumentException("Bad layout.");
                }
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException(ex);
            }
        }
        if (cmds.hasOption("help")) {
            help.printHelp("importer", opts, true);
            System.exit(EX_OK);
        }
        // positional arguments, i.e., input file name(s)
        args = cmds.getArgs();
    } catch (ParseException ex) {
        throw new IllegalArgumentException(ex);
    } catch (IllegalArgumentException ex) {
        help.printHelp("importer", opts, true);
        throw ex;
    }

    JsonGenerator jg = json.createGenerator(System.out, JsonEncoding.UTF8);
    jg.setPrettyPrinter(new StandardPrinter());

    final Extractor extractor;

    if (args.length == 1) {
        extractor = new FileExtractor(new File(args[0]));
    } else if (args.length > 1) {
        throw new UnsupportedOperationException("Not supported yet.");
    } else {
        throw new IllegalArgumentException("Missing input.");
    }

    int columnsCount = -1;
    int matrixCount = 0;

    ArrayDeque<String> stack = new ArrayDeque<String>();

    AtomType token = extractor.nextAtom();
    while (token != null) {
        switch (token) {
        case FLOAT:
        case INTEGER:
        case STRING:
        case NULL:
            jg.writeObject(extractor.getCurrentAtomData());
            break;
        case START_RECORD:
            jg.writeStartArray();
            break;
        case END_RECORD:
            int _numFields = (Integer) extractor.getCurrentAtomData();
            if (columnsCount < 0) {
                columnsCount = _numFields;
            } else if (columnsCount != _numFields) {
                throw new RuntimeException(String.format("row %s has different length than previous rows",
                        String.valueOf(_numFields - 1)));
            }
            jg.writeEndArray();
            break;
        case START_GROUP:
            stack.push(String.valueOf(extractor.getCurrentAtomData()));
            jg.writeStartObject();
            jg.writeStringField("kind", "datagator#Matrix");
            jg.writeNumberField("columnHeaders", columnHeaders);
            jg.writeNumberField("rowHeaders", rowHeaders);
            jg.writeFieldName("rows");
            jg.writeStartArray();
            break;
        case END_GROUP:
            int rowsCount = (Integer) extractor.getCurrentAtomData();
            if (rowsCount == 0) {
                jg.writeStartArray();
                jg.writeEndArray();
                rowsCount = 1;
                columnsCount = 0;
            }
            jg.writeEndArray();
            jg.writeNumberField("rowsCount", rowsCount);
            jg.writeNumberField("columnsCount", columnsCount);
            jg.writeEndObject();
            matrixCount += 1;
            stack.pop();
            break;
        default:
            break;
        }
        token = extractor.nextAtom();
    }

    jg.close();

    System.exit(EX_OK);
}

From source file:org.springframework.rest.documentation.doclet.RestDoclet.java

public static boolean start(RootDoc rootDoc) throws IOException {

    Javadoc api = new JavadocProcessor().process(rootDoc);

    File outputDirectory = getOutputDirectory(rootDoc.options());

    if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) {
        throw new IllegalStateException("Failed to create output directory " + outputDirectory);
    }//from  w  w w  .ja v a 2s .  co m

    File file = new File(outputDirectory, "javadoc.json");

    FileWriter writer = new FileWriter(file);

    JsonGenerator generator = new JsonFactory(new ObjectMapper()).createGenerator(writer)
            .useDefaultPrettyPrinter();

    generator.writeObject(api);

    return true;
}

From source file:org.n52.restfulwpsproxy.serializer.json.AbstractWPSJsonModule.java

protected static final void writeArrayOfObjects(String fieldName, Object[] objects, JsonGenerator jg)
        throws IOException {
    jg.writeArrayFieldStart(fieldName);/*from w  ww.j  av  a2s.c o m*/
    for (Object o : objects) {
        jg.writeObject(o);
    }
    jg.writeEndArray();
}

From source file:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDbPropertyMarshaller.java

public static AttributeValue getAttributeValue(final Object propertyValue,
        final PropertyDescriptor propertyDescriptor) {
    final Method readMethod = propertyDescriptor.getReadMethod();
    if (propertyValue == null) {
        return null;
    }//from  w w  w.  j a v  a 2 s  .c  o  m
    if (propertyValue instanceof Collection && ((Collection<?>) propertyValue).isEmpty()) {
        return null;
    }
    final DynamoDBReflectorUtil reflector = new DynamoDBReflectorUtil();
    try {
        final ArgumentMarshaller marshaller = reflector.getArgumentMarshaller(readMethod);
        return marshaller.marshall(propertyValue);
    } catch (final DynamoDBMappingException e) {
        try {
            final StringWriter output = new StringWriter();
            final JsonGenerator jsonGenerator = jsonFactory.createGenerator(output);
            jsonGenerator.writeObject(propertyValue);
            return new AttributeValue(output.toString());
        } catch (final IOException ioException) {
            throw new IllegalStateException(ioException);
        }
    }
}

From source file:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDbPropertyMarshaller.java

public static <T extends Item> AttributeValue getValue(final T item,
        final PropertyDescriptor propertyDescriptor) {
    final Method readMethod = propertyDescriptor.getReadMethod();
    Object propertyValue;/*from   w ww . j av a 2 s  .c o  m*/
    try {
        propertyValue = readMethod.invoke(item);
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
    if (propertyValue == null) {
        return null;
    }
    if (propertyValue instanceof Collection && ((Collection<?>) propertyValue).isEmpty()) {
        return null;
    }
    final DynamoDBReflectorUtil reflector = new DynamoDBReflectorUtil();
    try {
        final ArgumentMarshaller marshaller = reflector.getArgumentMarshaller(readMethod);
        return marshaller.marshall(propertyValue);
    } catch (final DynamoDBMappingException e) {
        try {
            final StringWriter output = new StringWriter();
            final JsonGenerator jsonGenerator = jsonFactory.createGenerator(output);
            jsonGenerator.writeObject(propertyValue);
            return new AttributeValue(output.toString());
        } catch (final IOException ioException) {
            throw new IllegalStateException(ioException);
        }
    }
}

From source file:io.wcm.caravan.pipeline.impl.JacksonFunctions.java

/**
 * Serializes the given JSON node using the default factory
 * @param node/*w  ww  .  j  av  a  2 s  .  co m*/
 * @return JSON string
 * @throws JsonPipelineOutputException if the given node can not be serialized
 */
public static String nodeToString(JsonNode node) {
    try {
        StringWriter writer = new StringWriter();
        JsonGenerator generator = jsonFactory.createGenerator(writer);
        generator.writeObject(node);
        return writer.toString();
    } catch (IOException ex) {
        throw new JsonPipelineOutputException("Failed to serialize JsonNode. This was quite unexpected.", ex);
    }
}

From source file:com.google.api.server.spi.config.annotationreader.ApiAnnotationIntrospector.java

@Nullable
private static <TFrom, TTo> JsonSerializer<TFrom> getJsonSerializer(
        @Nullable final Transformer<TFrom, TTo> serializer) {
    if (serializer == null) {
        return null;
    }/*from  ww w .  j a va2  s  . c  o  m*/
    return new JsonSerializer<TFrom>() {
        @Override
        public void serialize(TFrom value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeObject(serializer.transformTo(value));
        }
    };
}

From source file:je.backit.rest.JooqModule.java

static void writeField(String name, Object value, JsonGenerator jg) throws IOException {
    if (value == null)
        return;//from  w w  w .  ja  v a  2 s.  c o  m
    jg.writeFieldName(toCamelCaseLC(name));
    if (name.equals("properties"))
        jg.writeRawValue((String) value);
    else
        jg.writeObject(value);
}

From source file:org.loklak.objects.AbstractIndexEntry.java

public static void writeArray(JsonGenerator json, String fieldName, String[] array) throws IOException {
    json.writeArrayFieldStart(fieldName);
    for (String o : array)
        json.writeObject(o);
    json.writeEndArray();/*from   w w  w .  j  a  v a 2  s.  c o  m*/
}