Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

序列化和反序列化嵌套结构体 throw “Expected array, but got raw value” error. msgpack是否支持嵌套结构体的序列化和反序列化 #813

Open
zlp821130 opened this issue Apr 13, 2024 · 2 comments

Comments

@zlp821130
Copy link

我在C++端序列化nested struct RequestBody,并发送到Java端反序列化
C++结构体和序列化方法如下:
struct RESP_GHM {
int uiYear; // 年
char szMs[3]; // 毫秒

MSGPACK_DEFINE(uiYear, szMs);  // 定义要序列化的成员

};

/*

  • @brief 测试消息体结构体
    */
    struct RequestBody
    {
    int id;
    string msg;
    vector usCmdWork;
    vector usTestString;
    short usWave[3][4];
    RESP_GHM ghm;

    MSGPACK_DEFINE(id, msg, usCmdWork, usTestString, usWave, ghm); // 定义要序列化的成员
    };

// 序列化方法
template
string MsgpackSerialize(const T& obj) {

stringstream buffer;

// 序列化数据
pack(buffer, obj);
buffer.seekg(0);

string bufferStr(buffer.str());

return bufferStr;

};

Java端反序列化代码如下

// java反序列化代码
public T deserialize(byte[] data, String classOfT) throws CodecException {

    Object resultObject = null;
    MessagePack messagePack = new MessagePack();

    try {
        Class<?> clazz = Class.forName(classOfT);
        resultObject = messagePack.read(data, clazz);

    } catch (ClassNotFoundException e) {
        throw new CodecException(
                classOfT + " ClassNotFoundException occurred when Msgpack serializer decode!", e);
    } catch (IOException e) {
        throw new CodecException("IOException occurred when Msgpack serializer decode!", e);
    }

    return (T) resultObject;
}

执行到resultObject = messagePack.read(data, clazz); //throw “Expected array, but got raw value”。

注: 如果RequestBody中不嵌套RESP_GHM 结构体,Java端能够正常反序列化,msgpack是否支持嵌套结构体的序列化和反序列化?

@zlp821130
Copy link
Author

java 端的两个实体
@message
public class RESP_GHM implements Serializable {

int uiYear;            // 年
char[] szMs = new char[3];            // 毫秒

public int getUiYear() {
    return uiYear;
}

public void setUiYear(int uiYear) {
    this.uiYear = uiYear;
}

public char[] getSzMs() {
    return szMs;
}

public void setSzMs(char[] szMs) {
    this.szMs = szMs;
}

}

@message
public class RequestBody implements Serializable {

/**
 * id
 */
private int id;

/**
 * msg
 */
private String msg;

private ArrayList<Short> usCmdWork;

private ArrayList<String> usTestString;

private Short[][] usWave = new Short[3][4];

private RESP_GHM ghm;

public RequestBody() {

}

public RequestBody(int id, String msg) {
    this.id = id;
    this.msg = msg;
}

/**
 * Getter method for property <tt>id</tt>.
 *
 * @return property value of id
 */
public int getId() {
    return id;
}

/**
 * Setter method for property <tt>id</tt>.
 *
 * @param id value to be assigned to property id
 */
public void setId(int id) {
    this.id = id;
}

/**
 * Getter method for property <tt>msg</tt>.
 *
 * @return property value of msg
 */
public String getMsg() {
    return msg;
}

/**
 * Setter method for property <tt>msg</tt>.
 *
 * @param msg value to be assigned to property msg
 */
public void setMsg(String msg) {
    this.msg = msg;
}

public ArrayList<Short> getUsCmdWork() {
    return usCmdWork;
}

public void setUsCmdWork(ArrayList<Short> usCmdWork) {
    this.usCmdWork = usCmdWork;
}

public ArrayList<String> getUsTestString() {
    return usTestString;
}

public void setUsTestString(ArrayList<String> usTestString) {
    this.usTestString = usTestString;
}

public Short[][] getUsWave() {
    return usWave;
}

public void setUsWave(Short[][] usWave) {
    this.usWave = usWave;
}

public RESP_GHM getGhm() {
    return ghm;
}

public void setGhm(RESP_GHM ghm) {
    this.ghm = ghm;
}

}

@komamitsu
Copy link
Member

@zlp821130 You're using msgpack-java v6? If so, it's not supported now, unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants