【问题标题】:How to serialize JSON document using Messagepack (Cannot find template for class java.lang.Object)?如何使用 Messagepack 序列化 JSON 文档(找不到类 java.lang.Object 的模板)?
【发布时间】:2013-09-19 12:20:56
【问题描述】:

我打算在我们的项目中使用 Messagepack。目前,我们在项目中使用 JSON,并且正在将序列化 JSON 文档写入 Cassandra。现在我们正在考虑使用 Messagepack,它是一种高效的二进制序列化格式。

我正在尝试找到一个很好的示例来展示我在 JSON 文档上使用 Messagepack 但我还没有找到它。

下面是我的主类代码,它将使用 Value 类使用 Jackson 制作 JSON 文档,然后使用 ObjectMapper 序列化 JSON。

public static void main(String[] args) {

    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("id", 123);
    properties.put("test", 200);
    properties.put("marks", 100);

    Value val = new Value();
    val.setProperties(properties);

    MessagePack msgpack = new MessagePack();
    // Serialize
    byte[] raw = msgpack.write(av);

    System.out.println(raw);

            // deserialize
    Value actual = new MessagePack().read(raw, Value.class);

    System.out.println(actual);
}

下面是我的 Value 类,它使用 Jackson 制作 JSON 文档,并将使用 ObjectMapper 序列化文档并使用 Messagepack。

@JsonPropertyOrder(value = { "v" })
@Message
public class Value {

    private Map<String, Object> properties;

    /**
     * Default constructor.
     */
    @JsonCreator
    public Value() {
        properties = new HashMap<String, Object>();
    }


    /**
     * Gets the properties of this attribute value.
     * @return the properties of this attribute value.
     */
    @JsonProperty("v")
    public Map<String, Object> getProperties() {
        return properties;
    }

    /**
     * Sets the properties of this attribute value.
     * @param v the properties of this attribute value to set
     */
    @JsonProperty("v")
    public void setProperties(Map<String, Object> v) {
        properties = v;
    }


    @Override
    public String toString() {
        try {
            return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
        } catch (Exception e) {
            // log exception
        }

        return ToStringBuilder.reflectionToString(this);
    }    
}

但是,我不确定如何在这个 JSON 文档上使用 Messagepack?谁能给我一些例子吗?

但我想,让我们尝试一下,每当我在上面的代码中尝试像这样使用 Messagepack 时-

    MessagePack msgpack = new MessagePack();
    // Serialize
    byte[] raw = msgpack.write(av);

我总是遇到如下异常-

Exception in thread "main" org.msgpack.MessageTypeException: Cannot find template for class java.lang.Object class.  Try to add @Message annotation to the class or call MessagePack.register(Type).

而且我相信 MessagePack 不允许用户序列化 java.lang.Object 类型的变量,但就我而言,不可能将属性类型替换为某些原始类型。

我在我的 HashMap 中使用 Object,我需要它是这样的..

【问题讨论】:

    标签: java json serialization jackson messagepack


    【解决方案1】:

    您是否考虑过使用Smile,而不是尝试MessagePack,这是一种高效、100% 与 JSON API 兼容的二进制数据格式。它应该匹配或超过MessagePack 速度,并产生稍微更紧凑的输出。 Jackson 在以下位置实施:

    https://github.com/FasterXML/jackson-dataformat-smile

    这将“仅适用于”对象格式。所以而不是:

    String json = new ObjectMapper().writeValueAsString(value);
    

    你会使用

    byte[] smileEncoded = new ObjectMapper(new SmileFactory()).writeValueAsBytes(value);
    

    所有 Jackson 代码的工作方式都类似于 JSON,包括 JAX-RS 提供程序、数据类型(Guava、Joda、Hibernate 等)等。因此,您不会失去 JSON 的便利性(除了像所有二进制格式一样,Smile 不如 JSON 可读),而是获得存储和性能效率。

    【讨论】:

    • :谢谢 StaxMan.. 这对我来说非常有意义......所以这意味着我可以将 smileEncoded 值原样写入 Cassandra.. 对吗?还有如何使用 Smile 反序列化相同的数据?您能否根据我的代码提供一个简单的示例?谢谢。
    • 正确。我没有使用过 Cassandra,但我记得值可以是 blob (byte[]),并且需要这样声明,因为它不识别外部数据格式(它使用自己的结构来存储列值)。
    猜你喜欢
    • 1970-01-01
    • 2019-06-19
    • 2014-04-30
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多