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

byte[] array unpack error #476

Open
heliteng opened this issue May 17, 2018 · 5 comments
Open

byte[] array unpack error #476

heliteng opened this issue May 17, 2018 · 5 comments

Comments

@heliteng
Copy link

heliteng commented May 17, 2018

# java POJO
`public class ReportHeadParam {

private Integer messageType;
private byte[] messageParam;

public Integer getMessageType() {
	return messageType;
}
public void setMessageType(Integer messageType) {
	this.messageType = messageType;
}
public byte[] getMessageParam() {
	return messageParam;
}
public void setMessageParam(byte[] messageParam) {
	this.messageParam = messageParam;
}

}`

# data from rabbitmq

`
byte[] s = (byte[]) helper.decodeMessage(message);

ReportHeadParam head = MsgPackUtil.toObject(s, ReportHeadParam.class);`

# output error message

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize a POJO (of type com.comtop.dbaas.monitor.dto.ReportInstanceStateParam) from non-Array representation (token: VALUE_NUMBER_INT): type/property designed to be serialized as JSON Array at [Source: (byte[])"?????mysql5.7?????mysql.ys.246.02???oNsAgef6?oNsAgef6-2?2018-05-17 11:30:01?ONLINE "; line: -1, column: 0] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1138) at com.fasterxml.jackson.databind.deser.impl.BeanAsArrayDeserializer._deserializeFromNonArray(BeanAsArrayDeserializer.java:365) at com.fasterxml.jackson.databind.deser.impl.BeanAsArrayDeserializer.deserialize(BeanAsArrayDeserializer.java:97) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3079) at com.comtop.dbaas.util.MsgPackUtil.toObject(MsgPackUtil.java:62) at com.comtop.dbaas.worker.mq.RabbitmqConfig$2.onMessage(RabbitmqConfig.java:269)

@komamitsu
Copy link
Member

komamitsu commented May 17, 2018

@heliteng

Could you give me the followings?

  • codes of MsgPackUtil.toObject
  • actual values in byte array s

@heliteng
Copy link
Author

@komamitsu

MsgPackUtil.java

`package com.comtop.dbaas.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.msgpack.jackson.dataformat.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MsgPackUtil {
private static ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());

/**
 * @Title: toBytes
 * @Description: 对象转byte数组
 * @author Jecced
 * @param obj
 * @return
 */
public static <T> byte[] toBytes(T obj) {
    try {
        return mapper.writeValueAsBytes(obj);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return null;
}

/**
 * @Title: toList
 * @Description: byte转list集合
 * @author Jecced
 * @param bytes
 * @param clazz
 * @return
 */
public static <T> List<T> toList(byte[] bytes, Class<T> clazz) {
    List<T> list = null;
    try {
        list = mapper.readValue(bytes, MsgPackUtil.List(clazz));
    } catch (IOException e) {
        list = new ArrayList<>();
        e.printStackTrace();
    }
    return list;
}

/**
 * @Title: toObject
 * @Description: byte转指定对象
 * @author Jecced
 * @param bytes
 * @param clazz
 * @return
 */
public static <T> T toObject(byte[] bytes, Class<T> clazz) {
    T value = null;
    try {
        value = mapper.readValue(bytes, clazz);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return value;
}

/**
 * @Title: List
 * @Description: 私有方法,获取泛型的TypeReference
 * @author Jecced
 * @param clazz
 * @return
 */
private static <T> JavaType List(Class<?> clazz) {
    return mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
}

}
`

@heliteng
Copy link
Author

@komamitsu
I use msgpack-java version is 0.8.16 ,
which version msgpack-c match 0.8.16 ?

@komamitsu
Copy link
Member

I'm not sure if msgpack-c is compatible with msgpack-java:0.8.

  • actual values in byte array s

This information would be helpful to know that.

@heliteng
Copy link
Author

msgpack-java:0.7+ incompatible msgpack-c,

depended on an internal order of Java class's variables

but msgpack-java:0.6 have a big bug issues #447

how unpack msg form c++/c by java?

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