本文整理汇总了Java中com.fasterxml.jackson.databind.SerializationFeature类的典型用法代码示例。如果您正苦于以下问题:Java SerializationFeature类的具体用法?Java SerializationFeature怎么用?Java SerializationFeature使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SerializationFeature类属于com.fasterxml.jackson.databind包,在下文中一共展示了SerializationFeature类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: objectToString
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
public static String objectToString (final Object object) {
if (object == null) {
return null;
}
try {
final StringWriter stringWriter = new StringWriter();
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.writeValue(stringWriter, object);
return stringWriter.toString().replaceAll(System.getProperty("line.separator"), "");
} catch (IOException ex) {
STRINGUTIL_LOGGER.info("Sorry. had a error on during Object to String. ("+ex.toString()+")");
return null;
}
}
示例2: createObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
public static ObjectMapper createObjectMapper() {
final YAMLFactory yamlFactory = new YAMLFactory()
.configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false)
.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)
.configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true)
.configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false);
ObjectMapper mapper = new ObjectMapper(yamlFactory)
.registerModule(new Jdk8Module())
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.enable(SerializationFeature.INDENT_OUTPUT)
.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
for (Step step : ServiceLoader.load(Step.class, YamlHelpers.class.getClassLoader())) {
mapper.registerSubtypes(new NamedType(step.getClass(), step.getKind()));
}
return mapper;
}
示例3: createObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
public static ObjectMapper createObjectMapper() {
final YAMLFactory yamlFactory = new YAMLFactory()
.configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false)
.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)
.configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true)
.configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false);
ObjectMapper mapper = new ObjectMapper(yamlFactory)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.enable(SerializationFeature.INDENT_OUTPUT)
.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
for (Step step : ServiceLoader.load(Step.class, YamlHelpers.class.getClassLoader())) {
mapper.registerSubtypes(new NamedType(step.getClass(), step.getKind()));
}
return mapper;
}
示例4: writeTypescriptConfig
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
private void writeTypescriptConfig() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
ObjectNode root = mapper.createObjectNode();
ObjectNode compilerOptions = root.putObject("compilerOptions");
compilerOptions.put("baseUrl", "");
compilerOptions.put("declaration", true);
compilerOptions.put("emitDecoratorMetadata", true);
compilerOptions.put("experimentalDecorators", true);
compilerOptions.put("module", "es6");
compilerOptions.put("moduleResolution", "node");
compilerOptions.put("sourceMap", true);
compilerOptions.put("target", "es5");
ArrayNode typeArrays = compilerOptions.putArray("typeRoots");
typeArrays.add("node_modules/@types");
ArrayNode libs = compilerOptions.putArray("lib");
libs.add("es6");
libs.add("dom");
File outputSourceDir = new File(outputDir, config.getSourceDirectoryName());
File file = new File(outputSourceDir, "tsconfig.json");
file.getParentFile().mkdirs();
mapper.writer().writeValue(file, root);
}
示例5: IiifObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
public IiifObjectMapper() {
this.checkJacksonVersion();
// Don't include null properties
this.setSerializationInclusion(Include.NON_NULL);
// Both are needed to add `@context` to the top-level object
this.disable(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS);
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Some array fields are unwrapped during serialization if they have only one value
this.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
// Register the problem handler
this.addHandler(new ProblemHandler());
// Disable writing dates as timestamps
this.registerModule(new JavaTimeModule());
this.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// Enable automatic detection of parameter names in @JsonCreators
this.registerModule(new ParameterNamesModule());
// Register the module
this.registerModule(new IiifModule());
}
示例6: testBadRegistration
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
@Test
public void testBadRegistration() throws Exception {
RegisterDTO registerDTO = new RegisterDTO();
registerDTO.setUsername("JonkiPro");
registerDTO.setEmail("[email protected]");
registerDTO.setPassword("password1");
registerDTO.setPasswordAgain("password1");
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String requestJson = ow.writeValueAsString(registerDTO);
mockMvc
.perform(post("/api/v1.0/register")
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.content(requestJson))
.andExpect(status().isBadRequest());
}
示例7: objectMapper
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的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);
}
示例8: setUp
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
mapper = Jackson2ObjectMapperBuilder.json()
.featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
mapper.findAndRegisterModules();
}
示例9: afterPropertiesSet
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
@Override
public void afterPropertiesSet() throws Exception
{
//Configure the objectMapper ready for use
objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); //or NON_EMPTY?
// this is deprecated in jackson 2.9 and there is no straight replacement
// https://github.com/FasterXML/jackson-databind/issues/1547
objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
DateFormat DATE_FORMAT_ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DATE_FORMAT_ISO8601.setTimeZone(TimeZone.getTimeZone("UTC"));
objectMapper.setDateFormat(DATE_FORMAT_ISO8601);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
示例10: initialize
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
@Override
public void initialize(final Bootstrap<EndpointConfiguration> bootstrap) {
bootstrap.getObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
bootstrap.addBundle(new SwaggerBundle<EndpointConfiguration>() {
@Override
protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(
EndpointConfiguration configuration) {
return configuration.swagger;
}
});
}
示例11: extendMapper
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
@Override
public void extendMapper(ObjectMapper mapper)
{
SimpleModule restModule = new SimpleModule("RestModule", new Version(1, 0, 0, null));
// TODO this probably should be somewhere else, but it can't be in
// com.tle.core.jackson
// as that would make it dependent on equella i18n
restModule.addSerializer(new I18NSerializer());
mapper.registerModule(restModule);
mapper.registerModule(new JavaTypesModule());
mapper.registerModule(new RestStringsModule());
mapper.setSerializationInclusion(Include.NON_NULL);
// dev mode!
if( DebugSettings.isDebuggingMode() )
{
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
}
mapper.setDateFormat(new ISO8061DateFormatWithTZ());
}
示例12: testCache
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
@Test
public void testCache() throws Exception {
String projectId = "02a70003-e864-464e-b62c-e0ede97deb8c";
DeliveryClient client = new DeliveryClient(projectId);
final boolean[] cacheHit = {false};
client.setCacheManager(new CacheManager() {
@Override
public JsonNode resolveRequest(String requestUri, HttpRequestExecutor executor) throws IOException {
Assert.assertEquals("https://deliver.kenticocloud.com/02a70003-e864-464e-b62c-e0ede97deb8c/items/on_roasts", requestUri);
cacheHit[0] = true;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JSR310Module());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return objectMapper.readValue(this.getClass().getResourceAsStream("SampleContentItem.json"), JsonNode.class);
}
});
ContentItemResponse item = client.getItem("on_roasts");
Assert.assertNotNull(item);
Assert.assertTrue(cacheHit[0]);
}
示例13: JsonMapper
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
private JsonMapper() {
// calls the default constructor
super();
// configures ISO8601 formatter for date without time zone
// the used format is 'yyyy-MM-dd'
setDateFormat(new SimpleDateFormat(FMT_ISO_LOCAL_DATE));
// enforces to skip null and empty values in the serialized JSON output
setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// enforces to skip null references in the serialized output of map
configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
// enables serialization failures, when mapper encounters unknown properties names
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// configures the format to prevent writing of the serialized output for dates
// instances as timestamps; any date should be written in ISO format
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
示例14: deserializeWithStrategy
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的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);
}
示例15: JsonMapper
import com.fasterxml.jackson.databind.SerializationFeature; //导入依赖的package包/类
public JsonMapper() {
// calls the default constructor
super();
// configures ISO8601 formatter for date without time zone
// the used format is 'yyyy-MM-dd'
super.setDateFormat(new SimpleDateFormat(FMT_ISO_LOCAL_DATE));
// enforces to skip null and empty values in the serialized JSON output
super.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// enforces to skip null references in the serialized output of Map
super.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
// enables serialization failures, when mapper encounters unknown properties names
super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
// configures the format to prevent writing of the serialized output for java.util.Date
// instances as timestamps. any date should be written in ISO format
super.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}