當前位置: 首頁>>代碼示例>>Java>>正文


Java JsonSerializer類代碼示例

本文整理匯總了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;
        }
    });
}
 
開發者ID:aws,項目名稱:aws-xray-sdk-java,代碼行數:18,代碼來源:EntityImpl.java

示例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);
				}});
			
		}
	};
}
 
開發者ID:jigsaw-projects,項目名稱:jigsaw-payment,代碼行數:22,代碼來源:ProtobufConfiguration.java

示例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;
}
 
開發者ID:jmfgdev,項目名稱:gitplex-mit,代碼行數:26,代碼來源:HibernateProxySerializer.java

示例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("");
		}
	});
	
}
 
開發者ID:lemon-china,項目名稱:lemon-framework,代碼行數:23,代碼來源:JsonMapper.java

示例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;
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:17,代碼來源:JSONUtil.java

示例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);
}
 
開發者ID:tanhaichao,項目名稱:leopard,代碼行數:22,代碼來源:ContextualJsonSerializer.java

示例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);
}
 
開發者ID:tanhaichao,項目名稱:leopard,代碼行數:26,代碼來源:FieldDetailJsonSerializer.java

示例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;
        }
    });
}
 
開發者ID:joyent,項目名稱:java-triton,代碼行數:23,代碼來源:PostprocessModificationModule.java

示例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();
        }
    });
}
 
開發者ID:yahoo,項目名稱:yql-plus,代碼行數:24,代碼來源:JsonOperatorDump.java

示例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("");
				}

			});
		}
	}
 
開發者ID:ls960972314,項目名稱:report,代碼行數:26,代碼來源:JsonMapper.java

示例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);
}
 
開發者ID:easycodebox,項目名稱:easycode,代碼行數:23,代碼來源:GroupController.java

示例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);
}
 
開發者ID:easycodebox,項目名稱:easycode,代碼行數:30,代碼來源:PermissionController.java

示例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);
}
 
開發者ID:easycodebox,項目名稱:easycode,代碼行數:24,代碼來源:PermissionController.java


注:本文中的com.fasterxml.jackson.databind.JsonSerializer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。