Java Code Examples for com.alibaba.fastjson.serializer.SerializeWriter#writeString()

The following examples show how to use com.alibaba.fastjson.serializer.SerializeWriter#writeString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
/**
 * 写值
 *
 * @param out        输出
 * @param field      字段
 * @param value      值
 * @param ignoreNull 是否忽略null值
 * @param separator  分隔符位置
 */
protected void writeString(final SerializeWriter out, final String field, final String value,
                           final boolean ignoreNull, final int separator) {
    if (value != null || !ignoreNull) {
        if (separator == BEFORE) {
            out.write(',');
        }
        out.writeFieldName(field);
        if (value == null) {
            out.writeNull();
        } else {
            out.writeString(value);
        }
        if (separator == AFTER) {
            out.write(',');
        }
    }
}
 
Example 2
Source File: FastJsonJodaDateCodec.java    From actframework with Apache License 2.0 6 votes vote down vote up
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    SerializeWriter out = serializer.getWriter();

    if (object == null) {
        out.writeNull();
        return;
    }

    Class cls = object.getClass();
    if (cls == DateTime.class) {
        out.writeString(dateTimeCodec().toString((DateTime) object));
        return;
    } else if (cls == LocalDateTime.class) {
        out.writeString(localDateTimeCodec().toString((LocalDateTime) object));
        return;
    } else if (cls == LocalDate.class) {
        out.writeString(localDateCodec().toString((LocalDate) object));
        return;
    } else if (cls == LocalTime.class) {
        out.writeString(localTimeCodec().toString((LocalTime) object));
        return;
    }

    out.writeString(object.toString());
}
 
Example 3
Source File: FastJsonIssue.java    From actframework with Apache License 2.0 6 votes vote down vote up
@Override
public void write(
        JSONSerializer serializer,
        Object object,
        Object fieldName,
        Type fieldType,
        int features
) {
    SerializeWriter out = serializer.getWriter();
    if (object == null) {
        out.writeNull();
    } else {
        Class cls = object.getClass();
        if (cls == DateTime.class) {
            out.writeString(object.toString());
        } else {
            out.writeString(object.toString());
        }
    }
}
 
Example 4
Source File: ObjectTypeAdapter.java    From java-unified-sdk with Apache License 2.0 5 votes vote down vote up
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
    AVObject avObject = (AVObject)object;
    SerializeWriter writer = serializer.getWriter();
    writer.write('{');

    // for 1.1.70.android fastjson, we dont use writer.writeFieldValue(seperator, field, value) method.
    writer.write(' ');

//    writer.writeFieldName("@type", false);
//    writer.writeString(avObject.getClass().getName());
//    writer.write(',');

    writer.writeFieldName(KEY_VERSION, false);
    writer.writeString(DEFAULT_VERSION);
    writer.write(',');
    writer.writeFieldName(AVObject.KEY_CLASSNAME, false);
    writer.writeString(avObject.getClassName());
    writer.write(',');
    writer.writeFieldName(KEY_SERVERDATA, false);
    if (AVOSCloud.isEnableCircularReferenceDetect()) {
      writer.write(JSON.toJSONString(avObject.serverData, ObjectValueFilter.instance, SerializerFeature.WriteClassName));
    } else {
      writer.write(JSON.toJSONString(avObject.serverData, ObjectValueFilter.instance, SerializerFeature.WriteClassName,
              SerializerFeature.DisableCircularReferenceDetect));
    }

    writer.write('}');
  }
 
Example 5
Source File: FastJsonKeywordCodec.java    From actframework with Apache License 2.0 5 votes vote down vote up
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    SerializeWriter out = serializer.getWriter();

    if (object == null) {
        out.writeNull();
        return;
    }

    out.writeString(keywordCodec.toString((Keyword) object));
}
 
Example 6
Source File: NodeSerializer.java    From java-platform with Apache License 2.0 4 votes vote down vote up
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
		throws IOException {
	SerializeWriter out = serializer.getWriter();
	if (object == null) {
		out.writeNull();
		return;
	}

	Node<?> node = (Node<?>) object;

	out.write('{');

	out.writeFieldName("children");
	serializer.write(node.getChildren());
	out.write(",");

	out.writeFieldName("leaf");
	out.write(node.getLeaf());
	out.write(",");

	if (!Strings.isNullOrEmpty(node.getIcon())) {
		out.writeFieldName("icon");
		out.writeString(node.getIcon());
		out.write(",");
	}

	if (node.getChecked() != null) {
		out.writeFieldName("checked");
		out.write(node.getChecked());
		out.write(",");
	}

	if (node.getExpanded() != null) {
		out.writeFieldName("expanded");
		out.write(node.getExpanded());
		out.write(",");
	}

	List<? extends SerializeFilter> serializeFilters = serializer.getPropertyPreFilters();

	Object data = node.getData();

	// 处理父节点
	if (data != null && data instanceof Treeable && serializeFilters != null && serializeFilters.size() > 0) {
		boolean parentProcess = false;
		for (SerializeFilter filter : serializeFilters) {
			if (filter instanceof IncludesPropertyPreFilter) {
				Set<String> includes = ((IncludesPropertyPreFilter) filter).getIncludes();
				parentProcess = includes.contains("parent");
				if (parentProcess) {
					break;
				}
			}
		}
		if (parentProcess) {
			Object parent = ((Treeable<?>) data).getParent();
			if (parent != null) {
				out.writeFieldName("parent");
				out.write(JSON.toJSONString(parent, serializeConfig,
						Iterables.toArray(serializeFilters, SerializeFilter.class)));
				out.write(",");
			}
		}
	}

	// 处理对象值
	String dataString = serializeFilters == null ? JSON.toJSONString(data, serializeConfig)
			: JSON.toJSONString(data, serializeConfig, Iterables.toArray(serializeFilters, SerializeFilter.class));
	dataString = StringUtils.substring(dataString, 1);
	out.write(dataString);
}
 
Example 7
Source File: ResultSerializer.java    From java-platform with Apache License 2.0 4 votes vote down vote up
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
		throws IOException {

	SerializeWriter out = serializer.getWriter();
	if (object == null) {
		out.writeNull();
		return;
	}

	Result result = (Result) object;

	out.write('{');

	Map<String, Object> extraProperties = result.getExtraProperties();
	for (Entry<String, Object> entry : extraProperties.entrySet()) {
		String key = entry.getKey();
		Object value = entry.getValue();

		out.writeFieldName(key);
		serializer.write(value);
		out.write(',');
	}

	out.writeFieldName(FIELD_SUCCESS);
	out.write(Objects.equal(result.getCode(), Code.success));
	out.write(',');

	out.writeFieldName(FIELD_CODE);
	out.writeString(result.getCode().name());
	out.write(',');

	out.writeFieldName(FIELD_MESSAGE);
	out.writeString(result.getMessage());
	out.write(',');

	out.writeFieldName(FIELD_DATA);
	serializer.write(result.getData());

	out.write('}');
}