/** * Creates a Jackson object mapper based on a media type. It supports JSON, JSON Smile, XML, YAML * and CSV. * * @return The Jackson object mapper. */ protected ObjectMapper createObjectMapper() { ObjectMapper result = null; if (MediaType.APPLICATION_JSON.isCompatible(getMediaType())) { JsonFactory jsonFactory = new JsonFactory(); jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false); result = new ObjectMapper(jsonFactory); } else if (MediaType.APPLICATION_JSON_SMILE.isCompatible(getMediaType())) { SmileFactory smileFactory = new SmileFactory(); smileFactory.configure(Feature.AUTO_CLOSE_TARGET, false); result = new ObjectMapper(smileFactory); } else if (MediaType.APPLICATION_XML.isCompatible(getMediaType()) || MediaType.TEXT_XML.isCompatible(getMediaType())) { XmlFactory xmlFactory = new XmlFactory(); xmlFactory.configure(Feature.AUTO_CLOSE_TARGET, false); result = new XmlMapper(xmlFactory); } else if (MediaType.APPLICATION_YAML.isCompatible(getMediaType())) { YAMLFactory yamlFactory = new YAMLFactory(); yamlFactory.configure(Feature.AUTO_CLOSE_TARGET, false); 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 { JsonFactory jsonFactory = new JsonFactory(); jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false); result = new ObjectMapper(jsonFactory); } return result; }
/** * Creates a Jackson object writer based on a mapper. Has a special handling for CSV media types. * * @return The Jackson object writer. */ protected ObjectWriter createObjectWriter() { ObjectWriter result = null; if (MediaType.TEXT_CSV.isCompatible(getMediaType())) { CsvMapper csvMapper = (CsvMapper) getObjectMapper(); CsvSchema csvSchema = createCsvSchema(csvMapper); result = csvMapper.writer(csvSchema); } else { result = getObjectMapper().writerWithType(getObjectClass()); } return result; }