/** * Method that we need to override to actually make restoration go * through constructors etc. * Also: must be overridden by sub-classes as well. */ @Override protected Object readResolve() { return new CsvFactory(this, _objectCodec); }
/** * This method assumes use of UTF-8 for encoding. */ @Override public CsvGenerator createGenerator(OutputStream out) throws IOException { return createGenerator(out, JsonEncoding.UTF8); }
@Override public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException { return _createParser(r, _createContext(r, false)); }
@Override public CsvParser createParser(InputStream in) throws IOException { IOContext ctxt = _createContext(in, false); return _createParser(_decorate(in, ctxt), ctxt); }
@Override public CsvParser createParser(URL url) throws IOException { IOContext ctxt = _createContext(url, true); return _createParser(_decorate(_optimizedStreamFromURL(url), ctxt), ctxt); }
@Test public final void testRunSorterSecondColumn() throws Exception { CsvFactory csvFactory = new CsvFactory(); csvFactory.enable(CsvParser.Feature.TRIM_SPACES); // csvFactory.enable(CsvParser.Feature.WRAP_AS_ARRAY); csvFactory.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true); CsvMapper mapper = new CsvMapper(csvFactory); mapper.enable(CsvParser.Feature.TRIM_SPACES); // mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY); mapper.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true); // mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, // true); CsvSchema schema = CsvSchema.builder().setUseHeader(false).build(); verifyCSV(testInput1, 1, 2, 4, mapper, schema); try (Reader inputReader = Files.newBufferedReader(testInput1, StandardCharsets.UTF_8)) { CSVSorter.runSorter(inputReader, testOutput, 1, schema, CSVSorter.getComparator(1), true); } verifyCSV(testOutput, 1, 2, 4, mapper, schema); }
@Override public CsvGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException { // false -> we won't manage the stream unless explicitly directed to IOContext ctxt = _createContext(out, false); ctxt.setEncoding(enc); return _createGenerator(ctxt, _createWriter(_decorate(out, ctxt), JsonEncoding.UTF8, ctxt)); }
@Override public CsvParser createParser(URL url) throws IOException, JsonParseException { return _createParser(_optimizedStreamFromURL(url), _createContext(url, true)); }
/** * This method assumes use of UTF-8 for encoding. */ @Override public CsvGenerator createJsonGenerator(OutputStream out) throws IOException { // false -> we won't manage the stream unless explicitly directed to IOContext ctxt = _createContext(out, false); return _createGenerator(ctxt, _createWriter(out, JsonEncoding.UTF8, ctxt)); }
result = new ObjectMapper(yamlFactory); } else if (MediaType.TEXT_CSV.isCompatible(getMediaType())) { CsvFactory csvFactory = new CsvFactory(); csvFactory.configure(Feature.AUTO_CLOSE_TARGET, false); result = new CsvMapper(csvFactory); } else {
@Override public CsvFactory copy() { _checkInvalidCopy(CsvFactory.class); return new CsvFactory(this, null); }
@Override public CsvGenerator createGenerator(Writer out) throws IOException { IOContext ctxt = _createContext(out, false); return _createGenerator(_decorate(out, ctxt), ctxt); }
public CsvMapper enable(CsvParser.Feature f) { ((CsvFactory)_jsonFactory).enable(f); return this; }
/** * Overridable factory method that actually instantiates desired * parser. */ @Override protected CsvParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException, JsonParseException { Reader r = _createReader(data, offset, len, null, ctxt); return new CsvParser(ctxt, _getBufferRecycler(), _parserFeatures, _csvParserFeatures, _objectCodec, r); }
@Override protected IOContext _createContext(Object srcRef, boolean resourceManaged) { return new CsvIOContext(_getBufferRecycler(), srcRef, resourceManaged); } }
/** * Overridable factory method that actually instantiates desired * generator. */ @Override protected CsvGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException { return _createGenerator(ctxt, out); }
/** * Write a row to the destination. * * @param row The row to write. * @throws java.io.IOException Exception thrown when there's problems * writing to the output. */ @Override protected void writeToOutput(final Map<String, Object> row) throws IOException { if (writer == null) { CsvMapper mapper = new CsvMapper(); mapper.disable(SerializationFeature.CLOSE_CLOSEABLE); mapper.getFactory() .configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); CsvSchema schema = buildCsvSchema(row); writer = mapper.writer(schema); writer.writeValue(getWriter(), row.keySet()); } writer.writeValue(getWriter(), row.values()); }
@Test public final void testRunSorterFirstColumn() throws Exception { CsvFactory csvFactory = new CsvFactory(); csvFactory.enable(CsvParser.Feature.TRIM_SPACES); // csvFactory.enable(CsvParser.Feature.WRAP_AS_ARRAY); csvFactory.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true); CsvMapper mapper = new CsvMapper(csvFactory); mapper.enable(CsvParser.Feature.TRIM_SPACES); // mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY); mapper.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true); // mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, // true); CsvSchema schema = CsvSchema.builder().setUseHeader(false).build(); verifyCSV(testInput1, 1, 2, 4, mapper, schema); try (Reader inputReader = Files.newBufferedReader(testInput1, StandardCharsets.UTF_8)) { CSVSorter.runSorter(inputReader, testOutput, 1, schema, CSVSorter.getComparator(0), true); } verifyCSV(testOutput, 1, 2, 4, mapper, schema); }
@Override public CsvParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException { return _createParser(data, offset, len, _createContext(data, true)); }