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

The following examples show how to use com.alibaba.fastjson.serializer.SerializeWriter#writeNull() . 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: RbRecordCodec.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    SerializeWriter out = serializer.out;
    if (object == null) {
        out.writeNull();
        return;
    }

    Record record = (Record) object;
    Entity entity = record.getEntity();
    JSONObject map = new JSONObject();
    for (Iterator<String> iter = record.getAvailableFieldIterator(); iter.hasNext(); ) {
        String field = iter.next();
        Object value = record.getObjectValue(field);
        value = FieldValueWrapper.instance.wrapFieldValue(value, entity.getField(field), false);
        map.put(field, value);
    }
    out.write(map.toJSONString());
}
 
Example 3
Source File: MyNumberFieldSerializer.java    From dpCms with Apache License 2.0 6 votes vote down vote up
@Override
public void writeValue(JSONSerializer serializer, Object propertyValue) throws Exception {
    SerializeWriter out = serializer.getWriter();

    Object value = propertyValue;

    if (value == null) {
        if (out.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
            out.write('0');
        } else {
            out.writeNull();
        }
        return;
    }

    out.append(value.toString());
}
 
Example 4
Source File: TreeSerializer.java    From java-platform 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)
		throws IOException {
	SerializeWriter out = serializer.getWriter();
	if (object == null) {
		out.writeNull();
		return;
	}

	Tree<?> tree = (Tree<?>) object;
	if (tree.isCheckable()) {
		visitCheck(tree.getRoots(), tree.getChecked());
	}

	if (!Strings.isNullOrEmpty(tree.getIconProperty())) {
		visitIcon(tree.getRoots(), tree.getIconProperty());
	}

	if (tree.isExpandAll()) {
		expandAll(tree.getRoots());
	}

	serializer.write(tree.getRoots());
}
 
Example 5
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 6
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 7
Source File: PageSerializer.java    From java-platform with Apache License 2.0 5 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;
	}

	Page<?> page = (Page<?>) object;

	out.write('{');

	out.writeFieldName(DEFAULT_CURRENT_FIELD);
	out.writeInt(page.getNumber());
	out.write(',');

	out.writeFieldName(DEFAULT_ROWCOUNT_FIELD);
	out.writeInt(page.getSize());
	out.write(',');

	out.writeFieldName(DEFAULT_TOTAL_FIELD);
	out.writeLong(page.getTotalElements());
	out.write(',');

	out.writeFieldName(DEFAULT_ROWS_FIELD);
	serializer.write(page.getContent());

	out.write('}');
}
 
Example 8
Source File: JodaDateTimeSerializer.java    From java-platform with Apache License 2.0 5 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;
	}

	DateTime dateTime = (DateTime) object;
	serializer.write(dateTime.toDate());
}
 
Example 9
Source File: FastJsonValueObjectSerializer.java    From actframework with Apache License 2.0 5 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;
    }
    ValueObject vo = (ValueObject) object;
    out.write(vo.toJSONString());
}
 
Example 10
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 11
Source File: MyCollectionSerializer.java    From dpCms with Apache License 2.0 4 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) {
        if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
            out.write("[]");
        } 
        else {
            out.writeNull();
        }
        return;
    }

    Type elementType = null;
    if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
        if (fieldType instanceof ParameterizedType) {
            ParameterizedType param = (ParameterizedType) fieldType;
            elementType = param.getActualTypeArguments()[0];
        }
    }

    Collection<?> collection = (Collection<?>) object;

    SerialContext context = serializer.getContext();
    serializer.setContext(context, object, fieldName, 0);

    if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
        if (HashSet.class == collection.getClass()) {
            out.append("Set");
        } 
        else if (TreeSet.class == collection.getClass()) {
            out.append("TreeSet");
        }
    }

    try {
        int i = 0;
        out.append('[');
        
        for (Object item : collection) {

            if (i++ != 0) {
                out.append(',');
            }

            if (item == null) {
                out.writeNull();
                continue;
            }

            Class<?> clazz = item.getClass();

            if (clazz == Integer.class) {
                out.writeInt(((Integer) item).intValue());
                continue;
            }

            if (clazz == Long.class) {
                out.writeLong(((Long) item).longValue());

                if (out.isEnabled(SerializerFeature.WriteClassName)) {
                    out.write('L');
                }
                continue;
            }

            ObjectSerializer itemSerializer = serializer.getObjectWriter(clazz);
            itemSerializer.write(serializer, item, i - 1, elementType, 0);
        }
        out.append(']');
    } 
    finally {
        serializer.setContext(context);
    }
}
 
Example 12
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 13
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('}');
}