Пример #1
0
 /** 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) {
       }
     }
   }
 }
Пример #2
0
 /**
  * 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();
     }
   }
 }
  // 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());
  }
Пример #4
0
 /**
  * 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;
   }
 }
Пример #5
0
 /**
  * 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);
   }
 }
Пример #6
0
 /**
  * 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) {
       }
     }
   }
 }
 /**
  * Convenience method for checking whether specified serialization feature is enabled or not.
  * Shortcut for:
  *
  * <pre>
  *  getConfig().isEnabled(feature);
  * </pre>
  */
 public final boolean isEnabled(SerializationFeature feature) {
   return _config.isEnabled(feature);
 }
 @Override
 public final boolean isEnabled(MapperFeature feature) {
   return _config.isEnabled(feature);
 }
Пример #9
0
 public boolean isEnabled(MapperFeature f) {
   return _config.isEnabled(f);
 }
Пример #10
0
 public boolean isEnabled(SerializationFeature f) {
   return _config.isEnabled(f);
 }