/** Method called to configure the generator as necessary and then call write functionality */ protected final void _configAndWriteValue(JsonGenerator jgen, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configureJsonGenerator(jgen); // [JACKSON-282]: consider Closeable if (_config.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) { _writeCloseable(jgen, value, _config); return; } boolean closed = false; try { if (_rootType == null) { _serializerProvider(_config).serializeValue(jgen, value); } else { _serializerProvider(_config).serializeValue(jgen, value, _rootType, _rootSerializer); } closed = true; jgen.close(); } finally { /* won't try to close twice; also, must catch exception (so it * will not mask exception that is pending) */ if (!closed) { try { jgen.close(); } catch (IOException ioe) { } } } }
/** * Method that can be used to serialize any Java value as JSON output, using provided {@link * JsonGenerator}. */ public void writeValue(JsonGenerator jgen, Object value) throws IOException, JsonGenerationException, JsonMappingException { // 10-Aug-2012, tatu: As per [Issue#12], may need to force PrettyPrinter settings, so: _configureJsonGenerator(jgen); if (_config.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) { _writeCloseableValue(jgen, value, _config); } else { if (_rootType == null) { _serializerProvider(_config).serializeValue(jgen, value); } else { _serializerProvider(_config).serializeValue(jgen, value, _rootType, _rootSerializer); } if (_config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) { jgen.flush(); } } }
/** * Method called to locate (root) serializer ahead of time, if permitted by configuration. Method * also is NOT to throw an exception if access fails. */ protected final JsonSerializer<Object> _prefetchRootSerializer( SerializationConfig config, JavaType valueType) { if (valueType == null || !_config.isEnabled(SerializationFeature.EAGER_SERIALIZER_FETCH)) { return null; } try { return _serializerProvider(config).findTypedValueSerializer(valueType, true, null); } catch (JsonProcessingException e) { // need to swallow? return null; } }
// Test to ensure that we can check property ordering defaults... public void testConfigForPropertySorting() throws Exception { ObjectMapper m = new ObjectMapper(); // sort-alphabetically is disabled by default: assertFalse(m.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); SerializationConfig sc = m.getSerializationConfig(); assertFalse(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); assertFalse(sc.shouldSortPropertiesAlphabetically()); DeserializationConfig dc = m.getDeserializationConfig(); assertFalse(dc.shouldSortPropertiesAlphabetically()); // but when enabled, should be visible: m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); sc = m.getSerializationConfig(); assertTrue(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); assertTrue(sc.shouldSortPropertiesAlphabetically()); dc = m.getDeserializationConfig(); // and not just via SerializationConfig, but also via DeserializationConfig assertTrue(dc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); assertTrue(dc.shouldSortPropertiesAlphabetically()); }
/** * Helper method called to set or override settings of passed-in {@link JsonGenerator} * * @since 2.1 */ private final void _configureJsonGenerator(JsonGenerator jgen) { if (_prettyPrinter != null) { PrettyPrinter pp = _prettyPrinter; if (pp == NULL_PRETTY_PRINTER) { jgen.setPrettyPrinter(null); } else { /* [JACKSON-851]: Better take care of stateful PrettyPrinters... * like the DefaultPrettyPrinter. */ if (pp instanceof Instantiatable<?>) { pp = (PrettyPrinter) ((Instantiatable<?>) pp).createInstance(); } jgen.setPrettyPrinter(pp); } } else if (_config.isEnabled(SerializationFeature.INDENT_OUTPUT)) { jgen.useDefaultPrettyPrinter(); } // [JACKSON-520]: add support for pass-through schema: if (_schema != null) { jgen.setSchema(_schema); } }
/** * Helper method used when value to serialize is {@link Closeable} and its <code>close()</code> * method is to be called right after serialization has been called */ private final void _writeCloseableValue(JsonGenerator jgen, Object value, SerializationConfig cfg) throws IOException, JsonGenerationException, JsonMappingException { Closeable toClose = (Closeable) value; try { if (_rootType == null) { _serializerProvider(cfg).serializeValue(jgen, value); } else { _serializerProvider(cfg).serializeValue(jgen, value, _rootType, _rootSerializer); } if (_config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) { jgen.flush(); } Closeable tmpToClose = toClose; toClose = null; tmpToClose.close(); } finally { if (toClose != null) { try { toClose.close(); } catch (IOException ioe) { } } } }
public ObjectWriter with(Locale l) { SerializationConfig newConfig = _config.with(l); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
/** Method for constructing a new instance that is configured with specified features enabled. */ public ObjectWriter withoutFeatures(SerializationFeature... features) { SerializationConfig newConfig = _config.withoutFeatures(features); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
/** * Fluent factory method that will construct a new writer instance that will use specified date * format for serializing dates; or if null passed, one that will serialize dates as numeric * timestamps. * * <p>Note that the method does NOT change state of this reader, but rather construct and returns * a newly configured instance. */ public ObjectWriter with(DateFormat df) { SerializationConfig newConfig = _config.with(df); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
public TypeFactory getTypeFactory() { return _config.getTypeFactory(); }
public boolean isEnabled(MapperFeature f) { return _config.isEnabled(f); }
public boolean isEnabled(SerializationFeature f) { return _config.isEnabled(f); }
/** * Method that will construct a new instance that uses specified default {@link Base64Variant} for * base64 encoding * * @since 2.1 */ public ObjectWriter with(Base64Variant b64variant) { SerializationConfig newConfig = _config.with(b64variant); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
/** Method for constructing a new instance that is configured with specified features enabled. */ public ObjectWriter without(SerializationFeature first, SerializationFeature... other) { SerializationConfig newConfig = _config.without(first, other); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
/** * Method that will construct a new instance that uses specified serialization view for * serialization (with null basically disables view processing) * * <p>Note that the method does NOT change state of this reader, but rather construct and returns * a newly configured instance. */ public ObjectWriter withView(Class<?> view) { SerializationConfig newConfig = _config.withView(view); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
public ObjectWriter withType(TypeReference<?> rootType) { return withType(_config.getTypeFactory().constructType(rootType.getType())); }
/** * Method that will construct a new instance that uses specific type as the root type for * serialization, instead of runtime dynamic type of the root object itself. */ public ObjectWriter withType(Class<?> rootType) { return withType(_config.constructType(rootType)); }
/** * Method for constructing a new instance with configuration that specifies what root name to use * for "root element wrapping". See {@link SerializationConfig#withRootName(String)} for details. * * <p>Note that method does NOT change state of this reader, but rather construct and returns a * newly configured instance. */ public ObjectWriter withRootName(String rootName) { SerializationConfig newConfig = _config.withRootName(rootName); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
/** * Method that will construct a new instance that uses specified provider for resolving filter * instances by id. */ public ObjectWriter with(FilterProvider filterProvider) { if (filterProvider == _config.getFilterProvider()) { // no change? return this; } return new ObjectWriter(this, _config.withFilters(filterProvider)); }
public ObjectWriter with(TimeZone tz) { SerializationConfig newConfig = _config.with(tz); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }
/** Method for constructing a new instance that is configured with specified feature enabled. */ public ObjectWriter with(SerializationFeature feature) { SerializationConfig newConfig = _config.with(feature); return (newConfig == _config) ? this : new ObjectWriter(this, newConfig); }