alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.75k stars 6.5k forks source link

need WriteClassName with autoboxing Long value not ending prefix 'L' #243

Closed zhoufenglokki closed 8 years ago

zhoufenglokki commented 10 years ago

sometime, long(design-time) maybe autoboxing to Long(run-time)

problem in code:

@Test
public void testSerialize() {
    RpcResponse response = new RpcResponse(2, new Object());
    //String json = JSON.toJSONString(response, SerializerFeature.WriteClassName); // codeA with WriteClassName, requestId is not ending with 'L'
    String json = response.toCommandJson(); //  codeA with WriteClassName, requestId is ending with 'L', and trouble other json framework
    System.out.println(json);
}

package com.test.testjson.example.domain;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class RpcResponse {
    private int msgType = 50;
    private long requestId = 0;
    private JSONObject details = new JSONObject();
    private Object[] yieldResult = new Object[1];

    public RpcResponse() {

    }

    public RpcResponse(long requestId, Object result) {
        this.requestId = requestId;
        yieldResult[0] = result;
    }

    public int getMsgType() {
        return msgType;
    }

    public void setMsgType(int msgType) {
        this.msgType = msgType;
    }

    public long getRequestId() {
        return requestId;
    }

    public void setRequestId(long requestId) {
        this.requestId = requestId;
    }

    public JSONObject getDetails() {
        return details;
    }

    public void setDetails(JSONObject details) {
        this.details = details;
    }

    public Object[] getYieldResult() {
        return yieldResult;
    }

    public void setYieldResult(String[] yieldResult) {
        this.yieldResult = yieldResult;
    }

    protected Object[] fieldToArray() {
        return new Object[] { msgType, requestId, details, yieldResult };
    }

    public String toCommandJson() {
        return JSON.toJSONString(fieldToArray(), SerializerFeature.WriteClassName);
    }
}
yakolee commented 9 years ago

protected Object[] fieldToArray() { return new Object[] { msgType, requestId, details, yieldResult }; } object数组中requestId就是Long类型的啊

wenshao commented 8 years ago

很遗憾告诉你,这是一个设计问题,无法修复。