com.fasterxml.jackson.databind.ObjectMapper.enableDefaultTypingAsProperty()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(262)

本文整理了Java中com.fasterxml.jackson.databind.ObjectMapper.enableDefaultTypingAsProperty()方法的一些代码示例,展示了ObjectMapper.enableDefaultTypingAsProperty()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ObjectMapper.enableDefaultTypingAsProperty()方法的具体详情如下:
包路径:com.fasterxml.jackson.databind.ObjectMapper
类名称:ObjectMapper
方法名:enableDefaultTypingAsProperty

ObjectMapper.enableDefaultTypingAsProperty介绍

[英]Method for enabling automatic inclusion of type information -- needed for proper deserialization of polymorphic types (unless types have been annotated with com.fasterxml.jackson.annotation.JsonTypeInfo) -- using "As.PROPERTY" inclusion mechanism and specified property name to use for inclusion (default being "@class" since default type information always uses class name as type identifier)

NOTE: use of Default Typing can be a potential security risk if incoming content comes from untrusted sources, and it is recommended that this is either not done, or, if enabled, use #setDefaultTypingpassing a custom TypeResolverBuilder implementation that white-lists legal types to use.
[中]用于启用自动包含类型信息的方法—正确反序列化多态类型所需的(除非已使用com.fasterxml.jackson.annotation.JsonTypeInfo对类型进行了注释)—使用“As.PROPERTY”包含机制和指定的用于包含的属性名(默认值为“@class”)由于默认类型信息始终使用类名作为类型标识符)
注意:如果传入内容来自不受信任的来源,则使用默认类型可能会带来潜在的安全风险,建议不要这样做,或者如果启用,使用#SetDefaultTyping传递自定义TypeResolverBuilder实现,该实现将白名单中列出要使用的合法类型。

代码示例

代码示例来源:origin: apache/usergrid

public EntitySerializer() {
  try {
    mapper = new ObjectMapper( f );
    //                mapper.enable(SerializationFeature.INDENT_OUTPUT); don't indent output,
    // causes slowness
    mapper.enableDefaultTypingAsProperty( ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, "@class" );
  }
  catch ( Exception e ) {
    throw new RuntimeException( "Error setting up mapper", e );
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

/**
 * Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the
 * given {@literal name}. In case of an {@literal empty} or {@literal null} String the default
 * {@link JsonTypeInfo.Id#CLASS} will be used.
 *
 * @param classPropertyTypeName Name of the JSON property holding type information. Can be {@literal null}.
 */
public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName) {
  this(new ObjectMapper());
  // simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need
  // the type hint embedded for deserialization using the default typing feature.
  registerNullValueSerializer(mapper, classPropertyTypeName);
  if (StringUtils.hasText(classPropertyTypeName)) {
    mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, classPropertyTypeName);
  } else {
    mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
  }
}

代码示例来源:origin: apache/usergrid

public EntitySerializer( final SerializationFig serializationFig, final MetricsFactory metricsFactory) {
      this.serializationFig = serializationFig;
//            SimpleModule listModule = new SimpleModule("ListFieldModule", new Version(1, 0, 0, null,null,null))
//                .addAbstractTypeMapping(ListField.class, ArrayField.class);
//            MAPPER.registerModule(listModule);
      // causes slowness
      MAPPER.enableDefaultTypingAsProperty( ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, "@class" );
      this.bytesOutHistorgram = metricsFactory.getHistogram(MvccEntitySerializationStrategyV2Impl.class, "bytes.out");
      this.bytesInHistorgram = metricsFactory.getHistogram(MvccEntitySerializationStrategyV2Impl.class, "bytes.in");
      this.bytesOutTimer = metricsFactory.getTimer(MvccEntitySerializationStrategyV2Impl.class, "bytes.out");

    }

代码示例来源:origin: apache/usergrid

public EntitySerializer( final SerializationFig serializationFig, final MetricsFactory metricsFactory) {
  this.serializationFig = serializationFig;
  this.bytesOutHistorgram = metricsFactory.getHistogram(MvccEntitySerializationStrategyV3Impl.class, "bytes.out");
  this.bytesOutTimer = metricsFactory.getTimer(MvccEntitySerializationStrategyV3Impl.class, "bytes.out");
  this.bytesInHistorgram = metricsFactory.getHistogram(MvccEntitySerializationStrategyV3Impl.class, "bytes.in");
  //                mapper.enable(SerializationFeature.INDENT_OUTPUT); don't indent output,
  // causes slowness
  MAPPER.enableDefaultTypingAsProperty( ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, "@class" );
}

代码示例来源:origin: apache/usergrid

mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, "@class");

代码示例来源:origin: apache/usergrid

mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, "@class");

代码示例来源:origin: net.sf.javaprinciples.resource/resource-handling-core-impl

public JacksonResourceFormatter() {
  mapper = new ObjectMapper();
  mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@type");
}

代码示例来源:origin: stackoverflow.com

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enableDefaultTypingAsProperty(DefaultTyping.JAVA_LANG_OBJECT, "class");
// Note Object.class as second parameter.  Jackson will find the "class"
// property in the JSON and use it to determine which class to make.
MyClass foo = (MyClass) objectMapper.readValue(jsonString, Object.class);

代码示例来源:origin: citerus/bookstore-cqrs-example

public SimpleFileBasedEventStore(String eventStoreFile) throws IOException {
 objectMapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "type");
 this.eventStoreFile = init(eventStoreFile);
}

代码示例来源:origin: stackoverflow.com

ObjectMapper om = new ObjectMapper();
om.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "__class");

IPerson value = new MyPerson();
String s = om.writeValueAsString(value);
IPerson d = om.readValue(s, IPerson.class);

代码示例来源:origin: apache/servicemix-bundles

/**
 * Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the
 * given {@literal name}. In case of an {@literal empty} or {@literal null} String the default
 * {@link JsonTypeInfo.Id#CLASS} will be used.
 *
 * @param classPropertyTypeName Name of the JSON property holding type information. Can be {@literal null}.
 */
public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName) {
  this(new ObjectMapper());
  // simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need
  // the type hint embedded for deserialization using the default typing feature.
  mapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName)));
  if (StringUtils.hasText(classPropertyTypeName)) {
    mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, classPropertyTypeName);
  } else {
    mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
  }
}

代码示例来源:origin: org.springframework.data/spring-data-redis

/**
 * Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the
 * given {@literal name}. In case of an {@literal empty} or {@literal null} String the default
 * {@link JsonTypeInfo.Id#CLASS} will be used.
 *
 * @param classPropertyTypeName Name of the JSON property holding type information. Can be {@literal null}.
 */
public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName) {
  this(new ObjectMapper());
  // simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need
  // the type hint embedded for deserialization using the default typing feature.
  mapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName)));
  if (StringUtils.hasText(classPropertyTypeName)) {
    mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, classPropertyTypeName);
  } else {
    mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
  }
}

代码示例来源:origin: io.joynr.java/jeeintegration

private void provisionAccessControl(Properties properties, String domain, String[] interfaceNames) {
  ObjectMapper objectMapper = new ObjectMapper();
  objectMapper.enableDefaultTypingAsProperty(DefaultTyping.JAVA_LANG_OBJECT, "_typeName");
  List<MasterAccessControlEntry> allEntries = new ArrayList<>();
  for (String interfaceName : interfaceNames) {
    MasterAccessControlEntry newMasterAccessControlEntry = new MasterAccessControlEntry("*",
                                              domain,
                                              interfaceName,
                                              TrustLevel.LOW,
                                              new TrustLevel[]{
                                                  TrustLevel.LOW },
                                              TrustLevel.LOW,
                                              new TrustLevel[]{
                                                  TrustLevel.LOW },
                                              "*",
                                              Permission.YES,
                                              new Permission[]{
                                                  joynr.infrastructure.DacTypes.Permission.YES });
    allEntries.add(newMasterAccessControlEntry);
  }
  MasterAccessControlEntry[] provisionedAccessControlEntries = allEntries.toArray(new MasterAccessControlEntry[allEntries.size()]);
  String provisionedAccessControlEntriesAsJson;
  try {
    provisionedAccessControlEntriesAsJson = objectMapper.writeValueAsString(provisionedAccessControlEntries);
    properties.setProperty(StaticDomainAccessControlProvisioning.PROPERTY_PROVISIONED_MASTER_ACCESSCONTROLENTRIES,
                provisionedAccessControlEntriesAsJson);
  } catch (JsonProcessingException e) {
    LOG.error("Error parsing JSON.", e);
  }
}

代码示例来源:origin: com.tinkerpop/gremlin-core

@Override
public ObjectMapper createMapper() {
  final ObjectMapper om = new ObjectMapper();
  om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  if (embedTypes)
    om.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, GraphSONTokens.CLASS);
  if (normalize)
    om.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
  // this provider toStrings all unknown classes and converts keys in Map objects that are Object to String.
  final DefaultSerializerProvider provider = new GraphSONSerializerProvider();
  provider.setDefaultKeySerializer(new GraphSONModule.GraphSONKeySerializer());
  om.setSerializerProvider(provider);
  om.registerModule(new GraphSONModule(normalize));
  customModules.forEach(om::registerModule);
  // plugin external serialization modules
  if (loadCustomSerializers)
    om.findAndRegisterModules();
  // keep streams open to accept multiple values (e.g. multiple vertices)
  om.getFactory().disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
  return om;
}

代码示例来源:origin: io.joynr.java.core/libjoynr

public JsonMessageSerializerModule() {
  objectMapper = new ObjectMapper();
  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
  objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
  // objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
  objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);
  objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
  objectMapper.enableDefaultTypingAsProperty(DefaultTyping.JAVA_LANG_OBJECT, "_typeName");
  TypeResolverBuilder<?> joynrTypeResolverBuilder = objectMapper.getSerializationConfig()
                                 .getDefaultTyper(SimpleType.construct(Object.class));
  SimpleModule module = new SimpleModule("NonTypedModule", new Version(1, 0, 0, "", "", ""));
  module.addSerializer(new JoynrEnumSerializer());
  module.addSerializer(new JoynrListSerializer());
  module.addSerializer(new JoynrArraySerializer());
  TypeDeserializer typeDeserializer = joynrTypeResolverBuilder.buildTypeDeserializer(objectMapper.getDeserializationConfig(),
                                            SimpleType.construct(Object.class),
                                            null);
  module.addDeserializer(Request.class, new RequestDeserializer(objectMapper));
  module.addDeserializer(OneWayRequest.class, new OneWayRequestDeserializer(objectMapper));
  module.addDeserializer(Object.class, new JoynrUntypedObjectDeserializer(typeDeserializer));
  module.setMixInAnnotation(Throwable.class, ThrowableMixIn.class);
  objectMapper.registerModule(module);
}

代码示例来源:origin: org.apache.fulcrum/fulcrum-json-jackson2

DefaultTyping defaultTyping = DefaultTyping
    .valueOf(defaultTypeDefs[0]);
mapper.enableDefaultTypingAsProperty(defaultTyping,
    defaultTypeDefs[1]);
getLogger().info(

相关文章

微信公众号

最新文章

更多

ObjectMapper类方法