// Test for [JACKSON-554]
 public void testTreeToValue() throws Exception {
   String JSON = "{\"leaf\":{\"value\":13}}";
   ObjectMapper mapper = new ObjectMapper();
   mapper.addMixInAnnotations(Leaf.class, LeafMixIn.class);
   JsonNode root = mapper.readTree(JSON);
   // Ok, try converting to bean using two mechanisms
   Root r1 = mapper.treeToValue(root, Root.class);
   assertNotNull(r1);
   assertEquals(13, r1.leaf.value);
 }
示例#2
0
  static {
    final SimpleModule module =
        new SimpleModule("SwfModule", new Version(1, 0, 0, null, null, null))
            .addSerializer(Date.class, new EpochSecondsDateSerializer())
            .addDeserializer(Date.class, new EpochSecondsDateDeserializer());
    mapper.registerModule(module);
    mapper.setDateFormat(new EpochSecondsDateFormat());
    mapper.addMixInAnnotations(SimpleWorkflowMessage.class, BindingMixIn.class);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
    mapper.setVisibilityChecker(
        new VisibilityChecker.Std(VisibilityChecker.Std.class.getAnnotation(JsonAutoDetect.class)) {
          private static final long serialVersionUID = 1L;

          @Override
          public boolean isSetterVisible(final Method m) {
            return !(m.getParameterCount() == 1 && m.getParameterTypes()[0].isEnum())
                && super.isSetterVisible(m);
          }
        });
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  }
  /**
   * Configure an existing {@link ObjectMapper} instance with this builder's settings. This can be
   * applied to any number of {@code ObjectMappers}.
   *
   * @param objectMapper the ObjectMapper to configure
   */
  @SuppressWarnings("deprecation")
  public void configure(ObjectMapper objectMapper) {
    Assert.notNull(objectMapper, "ObjectMapper must not be null");

    if (this.findModulesViaServiceLoader) {
      // Jackson 2.2+
      objectMapper.registerModules(ObjectMapper.findModules(this.moduleClassLoader));
    } else if (this.findWellKnownModules) {
      registerWellKnownModulesIfAvailable(objectMapper);
    }

    if (this.modules != null) {
      for (Module module : this.modules) {
        // Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
        objectMapper.registerModule(module);
      }
    }
    if (this.moduleClasses != null) {
      for (Class<? extends Module> module : this.moduleClasses) {
        objectMapper.registerModule(BeanUtils.instantiate(module));
      }
    }

    if (this.dateFormat != null) {
      objectMapper.setDateFormat(this.dateFormat);
    }
    if (this.locale != null) {
      objectMapper.setLocale(this.locale);
    }
    if (this.timeZone != null) {
      objectMapper.setTimeZone(this.timeZone);
    }

    if (this.annotationIntrospector != null) {
      objectMapper.setAnnotationIntrospector(this.annotationIntrospector);
    }
    if (this.propertyNamingStrategy != null) {
      objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
    }
    if (this.serializationInclusion != null) {
      objectMapper.setSerializationInclusion(this.serializationInclusion);
    }

    if (this.filters != null) {
      // Deprecated as of Jackson 2.6, but just in favor of a fluent variant.
      objectMapper.setFilters(this.filters);
    }

    for (Class<?> target : this.mixIns.keySet()) {
      // Deprecated as of Jackson 2.5, but just in favor of a fluent variant.
      objectMapper.addMixInAnnotations(target, this.mixIns.get(target));
    }

    if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
      SimpleModule module = new SimpleModule();
      addSerializers(module);
      addDeserializers(module);
      objectMapper.registerModule(module);
    }

    customizeDefaultFeatures(objectMapper);
    for (Object feature : this.features.keySet()) {
      configureFeature(objectMapper, feature, this.features.get(feature));
    }

    if (this.handlerInstantiator != null) {
      objectMapper.setHandlerInstantiator(this.handlerInstantiator);
    } else if (this.applicationContext != null) {
      objectMapper.setHandlerInstantiator(
          new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
    }
  }