模型类中的HazelcastJsonValue [英] HazelcastJsonValue in the model class

查看:66
本文介绍了模型类中的HazelcastJsonValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在模型类中使用HazelcastJsonValue

How to use HazelcastJsonValue in the model class

public class User implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    private HazelcastJsonValue value; 

在IMap中,我正在插入值,诸如此类

In the IMap I'm inserting the value some thing like this

map.put(i,new User(obj.getId,obj.getName,new HazelcastJsonValue(value.toString)));

它为HazelcastJsonValue抛出序列化异常

Its throwing Serialization Exception for HazelcastJsonValue

如何解决此问题..

推荐答案

我可以看到2种解决此问题的方法:

I can see 2 approaches to resolve this:

  1. 您要么不再使用模型类中的 HazelcastJsonValue ,而是使用json的字符串表示形式或任何可序列化的东西.

  1. you either move away from using HazelcastJsonValue in the model class, so you use the string representation of the json, or anything that is serializable.

您必须通过添加 readObject() writeObject()方法来覆盖此类的序列化方法,并使用写入时为json值(字符串是一个简单的选择),读取时将其解析.示例(对我来说这成功地运行了):

you have to override the serialization method of this class by adding a readObject() and a writeObject() method, and use a serializable representation of the json value while writing (string is a straightforward choice), and parse it while reading. Example (this just ran successfully for me):

package com.hazelcast.client.pncounter;

import com.hazelcast.core.HazelcastJsonValue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Whatever {
    
    static class User implements Serializable {

        private static final long serialVersionUID = 1L;
        private String id;
        private String name;
        private HazelcastJsonValue value;

        public User(String id, String name, HazelcastJsonValue value) {
            this.id = id;
            this.name = name;
            this.value = value;
        }
        
        private void writeObject(ObjectOutputStream out)
                throws IOException {
            out.writeObject(id);
            out.writeObject(name);
            out.writeObject(value.toString());
        }
        
        private void readObject(ObjectInputStream in)
                throws IOException, ClassNotFoundException {
            id = (String) in.readObject();
            name = (String) in.readObject();
            value = new HazelcastJsonValue((String) in.readObject());
        }
    }

    public static void main(String[] args)
            throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(new User("1", "name", new HazelcastJsonValue("{}")));
        out.flush();
        
        User user = (User) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
    }
}

请参考 Java SE文档有关自定义序列化过程的详细信息.

Please refer to the Java SE documentation for details about customizing the serialization process.

这篇关于模型类中的HazelcastJsonValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆