本文整理汇总了Java中com.fasterxml.jackson.databind.JsonSerializer类的典型用法代码示例。如果您正苦于以下问题:Java JsonSerializer类的具体用法?Java JsonSerializer怎么用?Java JsonSerializer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonSerializer类属于com.fasterxml.jackson.databind包,在下文中一共展示了JsonSerializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setupModule
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@Override
public void setupModule(SetupContext setupContext) {
super.setupModule(setupContext);
setupContext.addBeanSerializerModifier(new BeanSerializerModifier() {
@SuppressWarnings("unchecked")
@Override
public JsonSerializer<?> modifySerializer(SerializationConfig serializationConfig, BeanDescription beanDescription, JsonSerializer<?> jsonSerializer) {
Class<?> beanClass = beanDescription.getBeanClass();
if (Cause.class.isAssignableFrom(beanClass)) {
return new CauseSerializer((JsonSerializer<Object>) jsonSerializer);
} else if (StackTraceElement.class.isAssignableFrom(beanClass)) {
return new StackTraceElementSerializer();
}
return jsonSerializer;
}
});
}
示例2: configProtobufSerializer
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@Bean
public Jackson2ObjectMapperBuilderCustomizer configProtobufSerializer() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(
Jackson2ObjectMapperBuilder builder) {
builder.serializerByType(Message.class, new JsonSerializer<Message>(){
@Override
public void serialize(Message message, JsonGenerator generator,
SerializerProvider provider) throws IOException {
if(message == null)
return;
JsonJacksonFormat format = new JsonJacksonFormat();
format.print(message, generator);
}});
}
};
}
示例3: findSerializer
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
protected JsonSerializer<Object> findSerializer(
SerializerProvider provider, Object value) throws IOException,
JsonProcessingException {
/*
* TODO: if Hibernate did use generics, or we wanted to allow use of
* Jackson annotations to indicate type, should take that into user.
*/
Class<?> type = value.getClass();
/*
* we will use a map to contain serializers found so far, keyed by type:
* this avoids potentially costly lookup from global caches and/or
* construction of new serializers
*/
/*
* 18-Oct-2013, tatu: Whether this is for the primary property or
* secondary is really anyone's guess at this point; proxies can exist
* at any level?
*/
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers
.findAndAddPrimarySerializer(type, provider, null);
if (_dynamicSerializers != result.map) {
_dynamicSerializers = result.map;
}
return result.serializer;
}
示例4: JsonMapper
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
public JsonMapper()
{
super();
// 允许单引号
//this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// 字段和值都加引号
this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
// 数字也加引号
this.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
this.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, true);
// 空值处理为空串
this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
{
@Override
public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException,
JsonProcessingException
{
jg.writeString("");
}
});
}
示例5: newWrappingModule
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
private static <T1, T2> SimpleModule newWrappingModule(final Class<T1> wrapped, final Class<T2> wrapper, final Converter<T1, T2> converter) {
SimpleModule module = new SimpleModule();
module.addDeserializer(wrapper, new JsonDeserializer<T2>() {
@Override
public T2 deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return converter.convert(ctxt.readValue(p, wrapped));
}
});
module.addSerializer(wrapper, new JsonSerializer<T2>() {
@Override
public void serialize(T2 value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
serializers.defaultSerializeValue(converter.revert(value), gen);
}
});
return module;
}
示例6: createContextual
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
if (beanProperty != null) {
// System.err.println("beanProperty:" + beanProperty + " name:" + beanProperty.getName() + " type:" + beanProperty.getMember().getGenericType());
Class<A> clazz = annotation();
A anno = beanProperty.getAnnotation(clazz);
if (anno == null) {
anno = beanProperty.getContextAnnotation(clazz);
}
if (anno != null) {
try {
return this.create(clazz, anno, beanProperty);
}
catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
}
return serializerProvider.findNullValueSerializer(beanProperty);
}
示例7: serialize
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
// gen.writeObject(value);
// String fieldName = gen.getOutputContext().getCurrentName();
// System.err.println("FieldDetailJsonSerializer serialize: className:" + className);
try {
Class<?> clazz;
String className = anno.className();
if (className.length() <= 0) {
clazz = anno.using();
}
else {
clazz = Class.forName(className);
}
@SuppressWarnings("unchecked")
JsonSerializer<Object> serializer = (JsonSerializer<Object>) clazz.newInstance();
serializer.serialize(value, gen, serializers);
}
catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
// System.err.println("JsonDetailJsonSerializer serialize value:" + value + " fieldName:" + fieldName + " type:" + beanProperty.getMember().getGenericType().getTypeName());
// Object detail = null;
// gen.writeObject(detail);
}
示例8: setupModule
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@Override
public void setupModule(final SetupContext context) {
super.setupModule(context);
context.addBeanSerializerModifier(new BeanSerializerModifier() {
public JsonSerializer<?> modifySerializer(
final SerializationConfig config,
final BeanDescription beanDesc,
final JsonSerializer<?> serializer) {
Class<?> valueClass = beanDesc.getType().getRawClass();
if (serializer instanceof BeanSerializerBase
&& MODIFIABLE_CLASSES.contains(valueClass)) {
return new FlatteningModifySerializer(
(BeanSerializerBase) serializer);
}
return serializer;
}
});
}
示例9: addJsonBean
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
private void addJsonBean(Object bean) {
if (bean instanceof JsonSerializer) {
addSerializerWithDeducedType((JsonSerializer<?>) bean);
}
if (bean instanceof JsonDeserializer) {
addDeserializerWithDeducedType((JsonDeserializer<?>) bean);
}
for (Class<?> innerClass : bean.getClass().getDeclaredClasses()) {
if (JsonSerializer.class.isAssignableFrom(innerClass)
|| JsonDeserializer.class.isAssignableFrom(innerClass)) {
try {
addJsonBean(innerClass.newInstance());
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:20,代码来源:JsonComponentModule.java
示例10: jacksonModule
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@Bean
public Module jacksonModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(Foo.class, new JsonSerializer<Foo>() {
@Override
public void serialize(Foo value, JsonGenerator jgen,
SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("foo", "bar");
jgen.writeEndObject();
}
});
return module;
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:17,代码来源:JacksonAutoConfigurationTests.java
示例11: ProgramDumpModule
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
public ProgramDumpModule() {
addSerializer(OperatorNode.class, new JsonSerializer<OperatorNode>() {
@Override
public void serialize(OperatorNode value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
Operator op = value.getOperator();
jgen.writeStartObject();
if (value.getLocation() != null) {
jgen.writeArrayFieldStart("location");
jgen.writeNumber(value.getLocation().getLineNumber());
jgen.writeNumber(value.getLocation().getCharacterOffset());
jgen.writeEndArray();
}
jgen.writeStringField("type", op.getClass().getName());
jgen.writeStringField("operator", op.toString());
if (!value.getAnnotations().isEmpty()) {
jgen.writeObjectField("annotations", value.getAnnotations());
}
jgen.writeFieldName("arguments");
provider.defaultSerializeValue(value.getArguments(), jgen);
jgen.writeEndObject();
}
});
}
示例12: JsonMapper
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
public JsonMapper(Inclusion inclusion, DateFormat df, boolean replaceNull)
{
mapper = new ObjectMapper();
// 设置输出时包含属性的风格
// mapper.setSerializationInclusion(inclusion);
// // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// // 禁止使用int代表Enum的order()來反序列化Enum
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
// 允许单引号
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
mapper.setDateFormat(df);
if (replaceNull) {
// null 转换为 ""
mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
{
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException
{
jgen.writeString("");
}
});
}
}
示例13: listAll
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@ResponseBody
public String listAll() throws Exception {
List<Group> gs = groupService.list(null);
Group g = new Group();
g.setId(-1);
g.setName("--无--");
gs.add(0, g);
Jacksons j = mvnJacksons.addJsonSerializer(Group.class, new JsonSerializer<Group>(){
@Override
public void serialize(Group value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeObjectField("id", value.getId());
gen.writeObjectField("pId", value.getParentId());
gen.writeStringField("name", value.getName());
gen.writeEndObject();
}
});
return j.toJson(gs);
}
示例14: listByProject
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
/**
* 根据项目ID获取权限
* @param projectId
* @return
* @throws Exception
*/
@ResponseBody
public String listByProject(Integer projectId) throws Exception {
List<Permission> os = permissionService.list(projectId, null, null);
Permission no = new Permission();
no.setId(-1L);
no.setName("--无--");
os.add(0, no);
Jacksons j = mvnJacksons.addJsonSerializer(Permission.class, new JsonSerializer<Permission>(){
@Override
public void serialize(Permission value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeObjectField("id", value.getId());
gen.writeObjectField("pId", value.getParentId());
gen.writeBooleanField("isParent", !(value.getIsMenu() == null || value.getIsMenu() == YesNo.NO));
gen.writeStringField("name", value.getName());
gen.writeEndObject();
}
});
return j.toJson(os);
}
示例15: cfgPermissionByRoleId
import com.fasterxml.jackson.databind.JsonSerializer; //导入依赖的package包/类
@ResponseBody
public String cfgPermissionByRoleId(Integer roleId) throws Exception {
List<Permission> os = permissionService.listAllGroupByProject(roleId);
Jacksons j = mvnJacksons.addJsonSerializer(Permission.class, new JsonSerializer<Permission>(){
@Override
public void serialize(Permission value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeObjectField("id", value.getId());
if(value.getId() == null) {
gen.writeObjectField("projectId", value.getProject().getId());
}
gen.writeObjectField("children", value.getChildren());
gen.writeStringField("name", value.getName());
gen.writeBooleanField("isParent", !(value.getIsMenu() == null || value.getIsMenu() == YesNo.NO));
gen.writeObjectField("checked", value.getIsOwn() != null && value.getIsOwn() == YesNo.YES);
gen.writeEndObject();
}
});
return j.toJson(os);
}