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

The following examples show how to use com.alibaba.fastjson.serializer.JSONSerializer#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: AbstractInvocationCodec.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final JSONSerializer serializer, final Object object, final Object fieldName, final Type fieldType, final int features) throws IOException {
    if (object == null) {
        serializer.writeNull();
    } else {
        Call call = (Call) object;
        writeObjectBegin(serializer);
        //1、class name
        writeString(serializer, classNameKey, call.getClassName());
        //2、alias
        writeString(serializer, aliasKey, call.getAlias());
        //3、method name
        writeString(serializer, methodNameKey, call.getMethodName());
        //4.argsType
        //TODO 应该根据泛型变量来决定是否要参数类型
        if (call.isCallback()) {
            //回调需要写上实际的参数类型
            write(serializer, argsTypeKey, call.computeArgsType(), AFTER);
        }
        //5、args
        writeArgs(serializer, call);
        //7、attachments
        write(serializer, attachmentsKey, call.getAttachments(), true, BEFORE);
        writeObjectEnd(serializer);
    }
}
 
Example 2
Source File: BackupShardSerializer.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final JSONSerializer serializer, final Object object, final Object fieldName, final Type fieldType, final int features) throws IOException {
    if (object == null) {
        serializer.writeNull();
    } else {
        SerializeWriter out = serializer.getWriter();
        out.write('{');
        BackupShard backupShard = (BackupShard) object;
        writeString(out, "name", backupShard.getName());
        writeString(out, "region", backupShard.getRegion());
        writeString(out, "dataCenter", backupShard.getDataCenter());
        writeString(out, "protocol", backupShard.getProtocol());
        writeString(out, "address", backupShard.getAddress());
        out.writeFieldName("weight");
        out.writeInt(backupShard.getWeight());
        out.write('}');
    }
}
 
Example 3
Source File: AbstractResponsePayloadCodec.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final JSONSerializer serializer, final Object object, final Object fieldName,
                  final Type fieldType, final int features) {
    if (object == null) {
        serializer.writeNull();
    } else {
        writeObjectBegin(serializer);
        ResponsePayload payload = (object instanceof ResponseMessage ? ((ResponseMessage<ResponsePayload>) object).getPayLoad() : (ResponsePayload) object);
        if (payload != null) {
            Throwable exception = payload.getException();
            Object response = payload.getResponse();
            if (response != null) {
                write(serializer, RES_CLASS, getTypeName(response, payload.getType()), AFTER);
                write(serializer, RESPONSE, response, NONE);
            } else if (exception != null) {
                write(serializer, RES_CLASS, getCanonicalName(exception.getClass()), AFTER);
                write(serializer, EXCEPTION, exception, NONE);
            }
        }
        writeObjectEnd(serializer);
    }
}
 
Example 4
Source File: JsonUtils.java    From canal-1.1.3 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 {
    if (object == null) {
        serializer.writeNull();
        return;
    }

    InetAddress address = (InetAddress) object;
    // 优先使用name
    serializer.write(address.getHostName());
}
 
Example 5
Source File: MyJavaBeanSerializer.java    From dpCms with Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeReference(JSONSerializer serializer, Object object,
		int fieldFeatures) {

	SerialContext context = serializer.getContext();
	{

		if (context != null
				&& SerializerFeature.isEnabled(context.getFeatures(),
						fieldFeatures,
						SerializerFeature.DisableCircularReferenceDetect)) {
			return false;
		}
	}

	if (!serializer.containsReference(object)) {
		return false;
	}

	SerialContext parentCtx = context;
	while ((parentCtx = parentCtx.getParent()) != null) {
		Object parentObj = parentCtx.getObject();
		if (object.equals(parentObj)) {
			serializer.writeNull();
			return true;
		}
	}
	return false;
}
 
Example 6
Source File: JsonUtils.java    From canal 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 {
    if (object == null) {
        serializer.writeNull();
        return;
    }

    InetAddress address = (InetAddress) object;
    // 优先使用name
    serializer.write(address.getHostName());
}