Java tutorial
/****************************************************************************** * * This data and information is proprietary to, and a valuable trade secret * * of, Basis Technology Corp. It is given in confidence by Basis Technology * * and may only be used as permitted under the license agreement under which * * it has been distributed, and in no other way. * * * * Copyright (c) 2015 Basis Technology Corporation All rights reserved. * * * * The technical data and information provided herein are provided with * * `limited rights', and the computer software provided herein is provided * * with `restricted rights' as those terms are defined in DAR and ASPR * * 7-104.9(a). ******************************************************************************/ package com.fasterxml.jackson.tools; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.dataformat.avro.AvroFactory; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; import com.fasterxml.jackson.dataformat.csv.CsvFactory; import com.fasterxml.jackson.dataformat.protobuf.ProtobufFactory; import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.fasterxml.jackson.dataformat.xml.XmlFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.google.common.io.ByteSink; import com.google.common.io.ByteSource; import com.google.common.io.Files; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.Option; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Command-line utility to Jackson */ public final class Jackson { /** * Supported formats. * Perhaps could be introspected. */ enum Format { JSON, YAML, SMILE, CBOR, XML, CSV, AVRO, PROTOBUF; } @Argument(index = 0, required = true, usage = "The input file.", metaVar = "INPUT") File inputFile; @Argument(index = 1, required = true, usage = "The format of the input file.", metaVar = "INPUT_FORMAT") Format inputFormat; @Argument(index = 2, required = true, usage = "The output file.", metaVar = "OUTPUT") File outputFile; @Argument(index = 3, required = true, usage = "The format of the output file.", metaVar = "OUTPUT_FORMAT") Format outputFormat; @Option(name = "-pretty", usage = "Enable pretty-printing.") boolean pretty; public static void main(String[] args) { Jackson that = new Jackson(); CmdLineParser parser = new CmdLineParser(that); try { parser.parseArgument(args); that.run(); } catch (CmdLineException e) { // handling of wrong arguments System.err.println(e.getMessage()); parser.printUsage(System.err); } } private void run() { ByteSource inputSource = Files.asByteSource(inputFile); ByteSink outputSink = Files.asByteSink(outputFile); InputStream input; OutputStream output; try { input = inputSource.openBufferedStream(); output = outputSink.openBufferedStream(); convert(input, output); } catch (IOException e) { System.err.format("Exception in conversion %s%n", e.getMessage()); System.exit(1); } } private void convert(InputStream input, OutputStream output) throws IOException { JsonFactory inputFactory = factory(inputFormat); JsonParser parser = inputFactory.createParser(input); JsonFactory outputFactory = factory(outputFormat); JsonGenerator generator = outputFactory.createGenerator(output); if (pretty) { generator.setPrettyPrinter(new DefaultPrettyPrinter()); } JsonToken token; while ((token = parser.nextToken()) != null) { switch (token) { case START_OBJECT: generator.writeStartObject(); break; case END_OBJECT: generator.writeEndObject(); break; case START_ARRAY: generator.writeStartArray(); break; case END_ARRAY: generator.writeEndArray(); break; case FIELD_NAME: generator.writeFieldName(parser.getCurrentName()); break; case VALUE_STRING: generator.writeString(parser.getValueAsString()); break; case VALUE_NUMBER_INT: generator.writeNumber(parser.getValueAsInt()); break; case VALUE_NUMBER_FLOAT: generator.writeNumber(parser.getValueAsDouble()); break; case VALUE_TRUE: generator.writeBoolean(true); break; case VALUE_FALSE: generator.writeBoolean(false); break; case VALUE_NULL: generator.writeNull(); default: System.err.format("Impossible token %s%n", token); System.exit(1); } } } private JsonFactory factory(Format format) { switch (format) { case JSON: return new JsonFactory(); case YAML: return new YAMLFactory(); case SMILE: return new SmileFactory(); case CBOR: return new CBORFactory(); case XML: return new XmlFactory(); case CSV: return new CsvFactory(); case AVRO: return new AvroFactory(); case PROTOBUF: return new ProtobufFactory(); } throw new RuntimeException("Invalid format"); } }