本文整理匯總了Java中com.fasterxml.jackson.databind.MapperFeature類的典型用法代碼示例。如果您正苦於以下問題:Java MapperFeature類的具體用法?Java MapperFeature怎麽用?Java MapperFeature使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MapperFeature類屬於com.fasterxml.jackson.databind包,在下文中一共展示了MapperFeature類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setUp
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
@Before public void setUp() {
SimpleModule module = new SimpleModule();
module.addSerializer(AnInterface.class, new AnInterfaceSerializer());
module.addDeserializer(AnInterface.class, new AnInterfaceDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
mapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
mapper.setVisibilityChecker(mapper.getSerializationConfig()
.getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(JacksonConverterFactory.create(mapper))
.build();
service = retrofit.create(Service.class);
}
示例2: defaultObjectMapperBuilder
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
@Test
public void defaultObjectMapperBuilder() throws Exception {
this.context.register(JacksonAutoConfiguration.class);
this.context.refresh();
Jackson2ObjectMapperBuilder builder = this.context
.getBean(Jackson2ObjectMapperBuilder.class);
ObjectMapper mapper = builder.build();
assertThat(MapperFeature.DEFAULT_VIEW_INCLUSION.enabledByDefault()).isTrue();
assertThat(mapper.getDeserializationConfig()
.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(MapperFeature.DEFAULT_VIEW_INCLUSION.enabledByDefault()).isTrue();
assertThat(mapper.getDeserializationConfig()
.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(mapper.getSerializationConfig()
.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES.enabledByDefault())
.isTrue();
assertThat(mapper.getDeserializationConfig()
.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
}
示例3: additionalPropertiesWorkWithAllVisibility
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
@Test
public void additionalPropertiesWorkWithAllVisibility() throws ClassNotFoundException, SecurityException, NoSuchMethodException, JsonProcessingException, IOException {
mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
mapper.setVisibility(mapper.getVisibilityChecker().with(Visibility.ANY));
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/additionalProperties/defaultAdditionalProperties.json", "com.example");
Class<?> classWithAdditionalProperties = resultsClassLoader.loadClass("com.example.DefaultAdditionalProperties");
String jsonWithAdditionalProperties = "{\"a\":1, \"b\":2};";
Object instanceWithAdditionalProperties = mapper.readValue(jsonWithAdditionalProperties, classWithAdditionalProperties);
JsonNode jsonNode = mapper.readTree(mapper.writeValueAsString(instanceWithAdditionalProperties));
assertThat(jsonNode.path("a").asText(), is("1"));
assertThat(jsonNode.path("b").asInt(), is(2));
assertThat(jsonNode.has("additionalProperties"), is(false));
}
示例4: objectMapper
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
public static ObjectMapper objectMapper() {
return new ObjectMapper()
// Property visibility
.setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS)
.setDefaultVisibility(JsonAutoDetect.Value.construct(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC))
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true)
// Property naming and order
.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE)
// Customised de/serializers
// Formats, locals, encoding, binary data
.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"))
.setDefaultPrettyPrinter(new DefaultPrettyPrinter())
.setLocale(Locale.CANADA);
}
示例5: loadGenesisJson
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
public static GenesisJson loadGenesisJson(InputStream genesisJsonIS) throws RuntimeException {
String json = null;
try {
json = new String(ByteStreams.toByteArray(genesisJsonIS));
ObjectMapper mapper = new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
GenesisJson genesisJson = mapper.readValue(json, GenesisJson.class);
return genesisJson;
} catch (Exception e) {
Utils.showErrorAndExit("Problem parsing genesis: "+ e.getMessage(), json);
throw new RuntimeException(e.getMessage(), e);
}
}
示例6: configureFeature
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
private void configureFeature(Object feature, boolean enabled) {
if (feature instanceof JsonParser.Feature) {
this.objectMapper.configure((JsonParser.Feature) feature, enabled);
}
else if (feature instanceof JsonGenerator.Feature) {
this.objectMapper.configure((JsonGenerator.Feature) feature, enabled);
}
else if (feature instanceof SerializationFeature) {
this.objectMapper.configure((SerializationFeature) feature, enabled);
}
else if (feature instanceof DeserializationFeature) {
this.objectMapper.configure((DeserializationFeature) feature, enabled);
}
else if (feature instanceof MapperFeature) {
this.objectMapper.configure((MapperFeature) feature, enabled);
}
else {
throw new FatalBeanException("Unknown feature class: " + feature.getClass().getName());
}
}
示例7: EsInstanceStore
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
public EsInstanceStore(String host, int port) {
super(host, port);
//This is required to let ES create the mapping of Date instead of long
insertMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
updateMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.addMixIn(getInstanceClass(), IgnoreCreatedTimeMixin.class);
//Specific mapper to read EsDailySnapshotInstance from the index
essnapshotinstanceMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setPropertyNamingStrategy(new EsPropertyNamingStrategy(
EsDailySnapshotInstance.class, EsInstanceStore.class))
.configure(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING, true);
}
示例8: deserializeWithStrategy
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
@Test
public void deserializeWithStrategy() throws Exception {
ObjectMapper
mapper =
new ObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setPropertyNamingStrategy(new EsPropertyNamingStrategy(
EsDailySnapshotInstance.class, EsInstanceStore.class))
.configure(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING, true);
EsDailySnapshotInstance inst = mapper.readValue(doc, EsDailySnapshotInstance.class);
Assert.assertEquals("coreapp-webapp-prod-0a018ef5", inst.getName());
Assert.assertEquals("fixed", inst.getLifecycle());
Assert.assertTrue(inst.getLaunchTime() != null);
}
示例9: configureerMapper
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
private void configureerMapper() {
// Configuratie
this.disable(MapperFeature.AUTO_DETECT_CREATORS);
this.disable(MapperFeature.AUTO_DETECT_FIELDS);
this.disable(MapperFeature.AUTO_DETECT_GETTERS);
this.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
this.disable(MapperFeature.AUTO_DETECT_SETTERS);
// Default velden niet als JSON exposen (expliciet annoteren!)
setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.NONE);
this.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
this.enable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
// serialization
this.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
this.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
// deserialization
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
示例10: initializeMapper
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
private static ObjectMapper initializeMapper() {
return new ObjectMapper()
.registerModule(new JavaTimeModule())
.disable(MapperFeature.AUTO_DETECT_GETTERS)
.disable(MapperFeature.AUTO_DETECT_CREATORS)
.disable(MapperFeature.AUTO_DETECT_SETTERS)
.disable(MapperFeature.AUTO_DETECT_IS_GETTERS)
.disable(MapperFeature.AUTO_DETECT_FIELDS)
.disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.NONE)
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.enable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)
// serialization
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.enable(SerializationFeature.USE_EQUALITY_FOR_OBJECT_ID)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
// deserialization
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
}
示例11: create
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
public static ObjectMapper create(ObjectMapper original, CustomComparators customComparators) {
ObjectMapper mapper = original.copy()
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
/*
* Get the original instance of the SerializerProvider before we add our custom module.
* Our Collection Delegating code does not call itself.
*/
SerializerProvider serializers = mapper.getSerializerProviderInstance();
// This module is reponsible for replacing non-deterministic objects
// with deterministic ones. Example convert Set to a sorted List.
SimpleModule module = new SimpleModule();
module.addSerializer(Collection.class,
new CustomDelegatingSerializerProvider(serializers, new CollectionToSortedListConverter(customComparators))
);
mapper.registerModule(module);
return mapper;
}
示例12: setup
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
@Before
public void setup() throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();
ObjectMapper objectMapper = new ObjectMapper().disable(
MapperFeature.AUTO_DETECT_CREATORS,
MapperFeature.AUTO_DETECT_FIELDS,
MapperFeature.AUTO_DETECT_GETTERS,
MapperFeature.AUTO_DETECT_IS_GETTERS)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
DeltaApi deltaApi = new Retrofit.Builder()
.baseUrl(mockWebServer.url("").toString())
.addConverterFactory(JacksonConverterFactory.create(objectMapper))
.build()
.create(DeltaApi.class);
apiClient = new DeltaApiClient(deltaApi);
}
示例13: IndividualPokemonRepository
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
public IndividualPokemonRepository(String file) throws Exception {
String legacy = null;
if (!file.equals(POKEMONGO_JSON)) {
legacy = file.substring(0, 8);
}
final InputStream is = this.getClass().getResourceAsStream(file);
if (is == null) {
throw new IllegalArgumentException("Can not find " + file);
}
mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
printer = JsonFormat.printer().includingDefaultValueFields();
final RawData rawData = mapper.readValue(is, RawData.class);
all = createPokemons(rawData, legacy);
pokemonMap = all.getPokemonList().stream().collect(Collectors.toMap(p -> p.getPokemonId(), p -> p));
log.info("Loaded {} pokemons", all.getPokemonCount());
}
示例14: configureFeature
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
private void configureFeature(ObjectMapper objectMapper, Object feature, boolean enabled) {
if (feature instanceof JsonParser.Feature) {
objectMapper.configure((JsonParser.Feature) feature, enabled);
}
else if (feature instanceof JsonGenerator.Feature) {
objectMapper.configure((JsonGenerator.Feature) feature, enabled);
}
else if (feature instanceof SerializationFeature) {
objectMapper.configure((SerializationFeature) feature, enabled);
}
else if (feature instanceof DeserializationFeature) {
objectMapper.configure((DeserializationFeature) feature, enabled);
}
else if (feature instanceof MapperFeature) {
objectMapper.configure((MapperFeature) feature, enabled);
}
else {
throw new FatalBeanException("Unknown feature class: " + feature.getClass().getName());
}
}
示例15: booleanSetters
import com.fasterxml.jackson.databind.MapperFeature; //導入依賴的package包/類
@Test
public void booleanSetters() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
.featuresToEnable(MapperFeature.DEFAULT_VIEW_INCLUSION,
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
SerializationFeature.INDENT_OUTPUT)
.featuresToDisable(MapperFeature.AUTO_DETECT_FIELDS,
MapperFeature.AUTO_DETECT_GETTERS,
MapperFeature.AUTO_DETECT_SETTERS,
SerializationFeature.FAIL_ON_EMPTY_BEANS).build();
assertNotNull(objectMapper);
assertTrue(objectMapper.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertTrue(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertFalse(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertFalse(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
assertFalse(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
assertTrue(objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT));
assertFalse(objectMapper.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
}