public void testJsonFactoryLinkage() { // first, implicit factory, giving implicit linkage assertSame(MAPPER, MAPPER.getFactory().getCodec()); // and then explicit factory, which should also be implicitly linked JsonFactory f = new JsonFactory(); ObjectMapper m = new ObjectMapper(f); assertSame(f, m.getFactory()); assertSame(m, f.getCodec()); }
/** * Method that can be used to serialize any Java value as a String. Functionally equivalent to * calling {@link #writeValue(Writer,Object)} with {@link java.io.StringWriter} and constructing * String, but more efficient. * * <p>Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it. */ public String writeValueAsString(Object value) throws JsonProcessingException { // alas, we have to pull the recycler directly here... SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler()); try { _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value); } catch (JsonProcessingException e) { // to support [JACKSON-758] throw e; } catch (IOException e) { // shouldn't really happen, but is declared as possibility so: throw JsonMappingException.fromUnexpectedIOE(e); } return sw.getAndClear(); }
/** * Method that can be used to serialize any Java value as a byte array. Functionally equivalent to * calling {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream} and * getting bytes, but more efficient. Encoding used will be UTF-8. * * <p>Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it. */ public byte[] writeValueAsBytes(Object value) throws JsonProcessingException { ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler()); try { _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value); } catch (JsonProcessingException e) { // to support [JACKSON-758] throw e; } catch (IOException e) { // shouldn't really happen, but is declared as possibility so: throw JsonMappingException.fromUnexpectedIOE(e); } byte[] result = bb.toByteArray(); bb.release(); return result; }
private void process(File input) throws IOException { JsonFactory jsonF = new JsonFactory(); JsonParser jp = jsonF.createJsonParser(input); TwitterEntry entry = read(jp); // let's write to a file, using UTF-8 encoding (only sensible one) StringWriter strw = new StringWriter(); JsonGenerator jg = jsonF.createJsonGenerator(strw); jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier // Here we would modify it... for now, will just (re)indent it write(jg, entry); System.out.println("Result = [" + strw.toString() + "]"); }
/** * Method for reading sequence of Objects from parser stream. * * @since 1.8 */ public <T> MappingIterator<T> readValues(URL src) throws IOException, JsonProcessingException { JsonParser jp = _jsonFactory.createJsonParser(src); if (_schema != null) { jp.setSchema(_schema); } DeserializationContext ctxt = _createDeserializationContext(jp, _config); return new MappingIterator<T>(_valueType, jp, ctxt, _findRootDeserializer(_config, _valueType)); }
protected final double _testRawDeser(int reps, byte[] json, ObjectReader reader) throws IOException { long start = System.nanoTime(); final JsonFactory f = reader.getFactory(); while (--reps >= 0) { JsonParser p = f.createParser(new ByteArrayInputStream(json)); JsonToken t; while ((t = p.nextToken()) != null) { if (t == JsonToken.VALUE_STRING) { p.getText(); } else if (t.isNumeric()) { p.getNumberValue(); } ; } p.close(); } hash = f.hashCode(); return _msecsFromNanos(System.nanoTime() - start); }
public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: java [input]"); System.exit(1); } byte[] data = readAll(args[0]); JsonFactory f = new JsonFactory(); boolean doIntern = true; f.configure(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES, doIntern); f.configure(JsonFactory.Feature.INTERN_FIELD_NAMES, doIntern); ObjectMapper m = new ObjectMapper(); Object input1 = m.readValue(data, Object.class); JsonNode input2 = m.readTree(data); new ManualReadPerfUntypedStream() .testFromBytes( // .testFromString( m, "JSON-as-Object", input1, Object.class, m, "JSON-as-Object2", input2, Object.class // ,m, "JSON-as-Node", input2, JsonNode.class ); }
protected JsonParser constructParser(byte[] data) throws IOException { return _factory.createParser(data, 0, data.length); }
protected JsonGenerator constructGenerator(OutputStream baos) throws IOException { return _factory.createGenerator(baos, JsonEncoding.UTF8); }
protected JsonParser constructParser(InputStream in) throws IOException { return _factory.createParser(in); }
public boolean isEnabled(JsonParser.Feature f) { return _jsonFactory.isEnabled(f); }
public JsonNode readTree(String content) throws IOException, JsonProcessingException { return _bindAndCloseAsTree(_jsonFactory.createJsonParser(content)); }
public JsonNode readTree(Reader r) throws IOException, JsonProcessingException { return _bindAndCloseAsTree(_jsonFactory.createJsonParser(r)); }
public JsonNode readTree(InputStream in) throws IOException, JsonProcessingException { return _bindAndCloseAsTree(_jsonFactory.createJsonParser(in)); }
@SuppressWarnings("unchecked") public <T> T readValue(URL src) throws IOException, JsonProcessingException { return (T) _bindAndClose(_jsonFactory.createJsonParser(src)); }
@SuppressWarnings("unchecked") public <T> T readValue(byte[] src, int offset, int length) throws IOException, JsonProcessingException { return (T) _bindAndClose(_jsonFactory.createJsonParser(src, offset, length)); }
/** * Method that can be used to serialize any Java value as JSON output, written to File provided. */ public void writeValue(File resultFile, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configAndWriteValue(_jsonFactory.createJsonGenerator(resultFile, JsonEncoding.UTF8), value); }
/** * Method that can be used to serialize any Java value as JSON output, using output stream * provided (using encoding {@link JsonEncoding#UTF8}). * * <p>Note: method does not close the underlying stream explicitly here; however, {@link * JsonFactory} this mapper uses may choose to close the stream depending on its settings (by * default, it will try to close it when {@link JsonGenerator} we construct is closed). */ public void writeValue(OutputStream out, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configAndWriteValue(_jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8), value); }
/** * Method that can be used to serialize any Java value as JSON output, using Writer provided. * * <p>Note: method does not close the underlying stream explicitly here; however, {@link * JsonFactory} this mapper uses may choose to close the stream depending on its settings (by * default, it will try to close it when {@link JsonGenerator} we construct is closed). */ public void writeValue(Writer w, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configAndWriteValue(_jsonFactory.createJsonGenerator(w), value); }