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


Java JsonSerializer.serialize方法代碼示例

本文整理匯總了Java中com.fasterxml.jackson.databind.JsonSerializer.serialize方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonSerializer.serialize方法的具體用法?Java JsonSerializer.serialize怎麽用?Java JsonSerializer.serialize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.fasterxml.jackson.databind.JsonSerializer的用法示例。


在下文中一共展示了JsonSerializer.serialize方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: 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

示例2: write

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
void write(Object val, int containedTypeIndex, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (val != null) {
        if (type.containedTypeCount() > containedTypeIndex) {
            JsonSerializer<Object> ser;
            JavaType containedType = type.containedType(containedTypeIndex);
            if (containedType != null && containedType.hasGenericTypes()) {
                List<JavaType> types = containedType.getBindings().getTypeParameters();
                JavaType[] generics = new JavaType[types.size()];
                for (int i = 0; i < types.size(); i++) {
                    generics[i] = types.get(i);
                }
                JavaType adjusted = provider.getTypeFactory().constructSimpleType(val.getClass(), generics);
                ser = provider.findTypedValueSerializer(adjusted, true, null);
            } else {
                ser = provider.findTypedValueSerializer(val.getClass(), true, null);
            }
            ser.serialize(val, gen, provider);
        } else {
            gen.writeObject(val);
        }
    } else {
        gen.writeNull();
    }
}
 
開發者ID:vavr-io,項目名稱:vavr-jackson,代碼行數:25,代碼來源:HListSerializer.java

示例3: serialize

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@Override
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    Object obj = toJavaObj(value);
    if (obj == null) {
        provider.getDefaultNullValueSerializer().serialize(null, gen, provider);
    } else {
        JsonSerializer<Object> ser;
        try {
            JavaType emulated = emulatedJavaType(type, provider.getTypeFactory());
            if (emulated.getRawClass() != Object.class) {
                ser = provider.findTypedValueSerializer(emulated, true, null);
            } else {
                ser = provider.findTypedValueSerializer(obj.getClass(), true, null);
            }
        } catch (Exception ignore) {
            ser = provider.findTypedValueSerializer(obj.getClass(), true, null);
        }
        ser.serialize(obj, gen, provider);
    }
}
 
開發者ID:vavr-io,項目名稱:vavr-jackson,代碼行數:21,代碼來源:ValueSerializer.java

示例4: serialize

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@Override
public void serialize(
        Resources value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException
{
    // Extracted the actual data inside of the Resources object that we care about
    Object content = value.getContent();

    // Instead of all of the Resources member variables, etc.
    // Just mashall the actual content into the JSON
    JsonSerializer<Object> s = provider.findValueSerializer(content.getClass(), null);
    s.serialize(content, jgen, provider);
}
 
開發者ID:ftsakiris,項目名稱:async-engine,代碼行數:14,代碼來源:ResourcesMapper.java

示例5: serialize

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@Override
public void serialize(StringInDatabase obj, JsonGenerator jgen, SerializerProvider sp) throws IOException {

  if (sp.getActiveView().isAssignableFrom(View.Db.class)) {
    jgen.writeString(obj.stringInDatabase());
  } else {
    final JsonSerializer<Object> beanSerializer = JsonHelper.getBeanSerializer(sp, obj.getClass());
    beanSerializer.serialize(obj, jgen, sp);
  }
}
 
開發者ID:atgse,項目名稱:sam,代碼行數:11,代碼來源:StringInDatabaseSerializer.java

示例6: serialize

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@Override
public void serialize(Resources value, JsonGenerator jgen,
		SerializerProvider provider) throws IOException,
		JsonProcessingException {
	// Extracted the actual data inside of the Resources object
	// that we care about (e.g., the list of Video objects)
	Object content = value.getContent();
	// Instead of all of the Resources member variables, etc.
	// Just mashall the actual content (Videos) into the JSON
	JsonSerializer<Object> s = provider.findValueSerializer(
			content.getClass(), null);
	s.serialize(content, jgen, provider);
}
 
開發者ID:juleswhite,項目名稱:CSX278,代碼行數:14,代碼來源:ResourcesMapper.java

示例7: serialize

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@Override
public void serialize(ExtractionFunctionDimension value, JsonGenerator gen, SerializerProvider provider)
        throws IOException {

    if (!LookupDimension.class.isInstance(value) && !RegisteredLookupDimension.class.isInstance(value)) {
        throw new TypeMismatchException("Lookup dimension serializer was given a non-lookup dimension.");
    }

    Optional<ExtractionFunction> extractionFunction = value.getExtractionFunction();

    // Use DimensionToDefaultDimensionSpec serializer if LookupDimension does not contain any namespace or lookups
    // or is not the inner most query
    if (!extractionFunction.isPresent() || SerializerUtil.hasInnerQuery(gen)) {
        JsonSerializer<Object> dimensionSerializer = provider.findValueSerializer(Dimension.class);
        dimensionSerializer.serialize(value, gen, provider);
        return;
    }

    String apiName = value.getApiName();
    String physicalName = SerializerUtil.findPhysicalName(value, gen).orElseThrow(() -> {
                LOG.error(ErrorMessageFormat.PHYSICAL_NAME_NOT_FOUND.logFormat(value.getApiName()));
                return new IllegalStateException(ErrorMessageFormat.PHYSICAL_NAME_NOT_FOUND.format());
            }
    );

    gen.writeObject(new ExtractionDimensionSpec(physicalName, apiName, extractionFunction.get()));
}
 
開發者ID:yahoo,項目名稱:fili,代碼行數:28,代碼來源:LookupDimensionToDimensionSpec.java

示例8: serializeContents

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@Override
public void serializeContents(Object value, JsonGenerator jgen, SerializerProvider provider)
    throws IOException
{
    JsonPropertyOrder order = value.getClass().getAnnotation(JsonPropertyOrder.class);
    String[] propOrder = (order == null) ? null : order.value();

    if (propOrder == null) {
        throw new IllegalStateException("Bean must declare JsonPropertyOrder!");
    }

    if (propOrder.length == 0) {
        return;
    }

    int i = 0;
    try {
        do {
            Field field = value.getClass().getDeclaredField(propOrder[i]);
            ReflectionUtils.makeAccessible(field);
            Object elem = field.get(value);
            if (elem == null) {
                provider.defaultSerializeNull(jgen);
            }
            else {
                Class<?> cc = elem.getClass();
                JsonSerializer<Object> serializer = provider.findValueSerializer(cc, null);
                serializer.serialize(elem, jgen, provider);
            }
            ++i;
        }
        while (i < propOrder.length);
    }
    catch (Exception e) {
        // [JACKSON-55] Need to add reference information
        wrapAndThrow(provider, e, value, i);
    }
}
 
開發者ID:webanno,項目名稱:webanno,代碼行數:39,代碼來源:BeanAsArraySerializer.java

示例9: serialize

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@Override
public void serialize(
  JsonValues value, JsonGenerator jgen, SerializerProvider provider
) throws IOException, JsonProcessingException {
  JsonSerializer<Object> serializer = provider.findValueSerializer(value.values.getClass(), null);
  serializer.serialize(value.values, jgen, provider);
}
 
開發者ID:shunfei,項目名稱:DCMonitor,代碼行數:8,代碼來源:JsonValues.java

示例10: serializeNotNullValue

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
public void serializeNotNullValue( JsonSerializer serializer, T value, JsonGenerator generator, SerializerProvider provider ) throws IOException, JsonProcessingException {
    serializer.serialize( value, generator, provider );
}
 
開發者ID:NyBatis,項目名稱:NyBatisCore,代碼行數:4,代碼來源:AbstractJsonSerializer.java

示例11: writeObject

import com.fasterxml.jackson.databind.JsonSerializer; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
void writeObject(Object obj) throws IOException {
  jgen.writeStartObject();

  Class cls = obj.getClass();
  while(!cls.equals(Object.class)) {
    Field[] fields = getDeclaredFields(cls);
    for(Field field : fields) {
      try {
        field.setAccessible(true);

        //if the field has a serializer annotation on it, serialize with it
        if(fieldAllowed(field, obj.getClass())) {
          Object val = readField(obj, field);
          if(!valueAllowed(val, obj.getClass())) {
            continue;
          }

          String name = getFieldName(field);
          jgen.writeFieldName(name);

          JsonSerializer fieldSerializer = annotatedWithJsonSerialize(field);
          if(fieldSerializer != null) {
            fieldSerializer.serialize(val, jgen, serializerProvider);
          } else if(customSerializersMap != null && val != null) {
            JsonSerializer<Object> serializer = customSerializersMap.get(val.getClass());
            if(serializer != null) {
              serializer.serialize(val, jgen, serializerProvider);
            } else {
              new JsonWriter(jgen, result, currentMatch, currentPath, path, field, serializerProvider).write(name, val);
            }
          } else {
            new JsonWriter(jgen, result, currentMatch, currentPath, path, field, serializerProvider).write(name, val);
          }
        }
      } catch(IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException(e);
      }
    }
    cls = cls.getSuperclass();
  }

  jgen.writeEndObject();
}
 
開發者ID:monitorjbl,項目名稱:json-view,代碼行數:45,代碼來源:JsonViewSerializer.java


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