本文整理匯總了Java中com.fasterxml.jackson.databind.ser.std.StdSerializer類的典型用法代碼示例。如果您正苦於以下問題:Java StdSerializer類的具體用法?Java StdSerializer怎麽用?Java StdSerializer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
StdSerializer類屬於com.fasterxml.jackson.databind.ser.std包,在下文中一共展示了StdSerializer類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: customJackson2HttpMessageConverter
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
/**
* Configure Jackson (enable Path to Json/String conversion)
*/
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter converter) {
final ObjectMapper objectMapper = new ObjectMapper();
final SimpleModule sm = new SimpleModule();
sm.addSerializer(Path.class, new StdSerializer<Path>(Path.class) {
private static final long serialVersionUID = 8963132152002562810L;
@Override
public void serialize(Path value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeString(value.toString());
}
});
objectMapper.registerModule(sm);
converter.setObjectMapper(objectMapper);
return converter;
}
示例2: ObjectMapperResolver
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
public ObjectMapperResolver() {
mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
@Override
public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config,
final JavaType type,
BeanDescription beanDesc,
final JsonDeserializer<?> deserializer) {
return new JsonDeserializer<Enum>() {
@Override
public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase());
}
};
}
});
module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) {
@Override
public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(value.name().toLowerCase());
}
});
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(module);
}
示例3: CommonModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public CommonModule() {
super("common", new Version(0, 1, 0, null, null, null));
setDeserializerModifier(new BeanDeserializerModifier() {
@Override
public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config,
final JavaType type,
BeanDescription beanDesc,
final JsonDeserializer<?> deserializer) {
return new JsonDeserializer<Enum>() {
@Override
public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase());
}
};
}
});
addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) {
@Override
public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(value.name().toLowerCase());
}
});
addDeserializer(String.class, new RawValueDeserializer());
}
示例4: ObjectMapperResolver
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
public ObjectMapperResolver() {
mapper = new GraviteeMapper();
//because Permissions are represented as char[]
mapper.enable(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS);
// register filter provider
registerFilterProvider();
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
@Override
public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config,
final JavaType type,
BeanDescription beanDesc,
final JsonDeserializer<?> deserializer) {
return new JsonDeserializer<Enum>() {
@Override
public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase());
}
};
}
});
module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) {
@Override
public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(value.name().toLowerCase());
}
});
mapper.registerModule(module);
}
示例5: registerEnumModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
/**
* Register Jackson module that maps enums as lowercase. Per http://stackoverflow.com/a/24173645.
*/
@SuppressWarnings("rawtypes")
private static void registerEnumModule(ObjectMapper mapper) {
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
@Override
public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config,
final JavaType type,
BeanDescription beanDesc,
final JsonDeserializer<?> deserializer) {
return new JsonDeserializer<Enum>() {
@Override
public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
@SuppressWarnings("unchecked") Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase());
}
};
}
});
module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) {
@Override
public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(value.name().toLowerCase());
}
});
mapper.registerModule(module);
}
示例6: CustomSerializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
protected CustomSerializer(StdSerializer<?> src) {
super(src);
}
示例7: MergerServiceImpl
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
@Inject
@SuppressWarnings({ "unchecked", "rawtypes" })
public MergerServiceImpl() {
JacksonXmlModule module = new JacksonXmlModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
@Override
public JsonDeserializer<Enum> modifyEnumDeserializer(
DeserializationConfig config, final JavaType type, BeanDescription beanDesc,
final JsonDeserializer<?> deserializer) {
return new JsonDeserializer<Enum>() {
@Override
public Enum deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase());
}
};
}
});
module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) {
private static final long serialVersionUID = 4951133737173200158L;
@Override
public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeString(StringUtils.capitalize(value.name().toLowerCase()));
}
});
// Extra non-JAXB annotations are needed to process abstract classes.
module.setMixInAnnotation(AbstractSourceT.class, AbstractMixIn.class);
mapper = new XmlMapper(module);
AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
AnnotationIntrospector pair = AnnotationIntrospectorPair.create(primary, secondary);
mapper.setAnnotationIntrospector(pair);
SimpleDateFormat sdf = new SimpleDateFormat(Utils.LONG_DATE_FORMAT);
mapper.setDateFormat(sdf);
// Serialization options
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.setSerializationInclusion(Include.NON_NULL);
}
示例8: setupModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer; //導入依賴的package包/類
@Override
public void setupModule(final SetupContext context) {
context.addSerializers(new SimpleSerializers(Collections.<JsonSerializer<?>>singletonList(new StdSerializer<SpanWrapper>(SpanWrapper.class) {
@Override
public void serialize(SpanWrapper span, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
Map<String, Object> parameters = null;
for (Map.Entry<String, Object> tag : span.getTags().entrySet()) {
if (tag.getKey().startsWith(SpanWrapper.INTERNAL_TAG_PREFIX)) {
continue;
}
if (tag.getKey().startsWith(PARAMETERS_PREFIX)) {
if (parameters == null) {
parameters = new HashMap<String, Object>();
}
parameters.put(tag.getKey().replace(PARAMETERS_PREFIX, ""), tag.getValue());
} else if (tag.getKey().equals(Tags.PEER_HOST_IPV4.getKey()) && tag.getValue() instanceof Integer) {
gen.writeStringField(IPV4_STRING, InetAddresses.fromInteger((Integer) tag.getValue()).getHostAddress());
gen.writeStringField("ip", InetAddresses.fromInteger((Integer) tag.getValue()).getHostAddress());
} else if (tag.getKey().equals(Tags.PEER_HOST_IPV6.getKey()) && tag.getValue() instanceof String) {
gen.writeStringField("ip", (String) tag.getValue());
} else {
gen.writeObjectField(tag.getKey(), tag.getValue());
}
}
if (parameters != null && !parameters.isEmpty()) {
gen.writeArrayFieldStart("parameters");
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
gen.writeStartObject();
gen.writeObjectField("key", entry.getKey());
gen.writeObjectField("value", entry.getValue());
gen.writeEndObject();
}
gen.writeEndArray();
}
// always include error tag so we can have a successful/error filter in Kibana
if (!span.getTags().containsKey(Tags.ERROR.getKey())) {
gen.writeBooleanField("error", false);
}
gen.writeStringField("name", span.getOperationName());
gen.writeNumberField("duration_ms", span.getDurationMs());
final String timestamp = StringUtils.timestampAsIsoString(span.getStartTimestampMillis());
gen.writeStringField("@timestamp", timestamp);
gen.writeEndObject();
}
})));
}