Archived
How to serialize / deserialize an slicing object by OutputStream / InputStream?
ENV
ICE: 3.7.1
JDK: 10.0
WINDOWS 10
I want to use OutputStream / InputStream for serialize / deserialize purpose, but I get the NullPointerException.
The slice:
[["java:package:com.joey.slice"]] module test { ["java:getset"] class TestMessage { int id; string sender; string receiver; string data; }; };
The test code:
public class IceTest { public static void main(String[] args){ TestMessage testMessage = new TestMessage(); testMessage.setId(1); testMessage.setSender("Bob"); testMessage.setReceiver("Alice"); testMessage.setData("hello"); com.zeroc.Ice.OutputStream out = new com.zeroc.Ice.OutputStream(); out.startEncapsulation(); out.writeValue(testMessage); out.endEncapsulation(); com.zeroc.Ice.InputStream in = new com.zeroc.Ice.InputStream(out.finished()); TestMessage message = new TestMessage(); message._iceRead(in); System.out.println("id:::" + JSON.toJSONString(message)); } }
The exception:
Exception in thread "main" java.lang.NullPointerException
at com.zeroc.Ice.InputStream.startValue(InputStream.java:511)
at com.zeroc.Ice.Value._iceRead(Value.java:90)
So, what's wrong in the test code? what is the right way to get the serialize / deserialize purpose?
Thanks
Comments
-
Hi,
You shouldn't use "internal" methods that start with an underscore such as
_iceRead
.Instead, use the following:
com.zeroc.Ice.InputStream in = new com.zeroc.Ice.InputStream(out.finished()); in.startEncapsulation(); final TestMessage[] tmp = new TestMessage[1]; in.readValue(value -> tmp[0] = value, TestMessage.class); in.endEncapsulation(); TestMessage message = tmp[0];
Cheers,
Benoit.0 -
hi,
I just solved this problem like this:
public class IceTest {
public static void main(String[] args){com.zeroc.Ice.InitializationData initData = new com.zeroc.Ice.InitializationData(); initData.properties = com.zeroc.Ice.Util.createProperties(); initData.properties.setProperty("Ice.Default.Package", "com.joey.slice"); Communicator ic = com.zeroc.Ice.Util.initialize(initData); TestMessage testMessage = new TestMessage(); testMessage.setId(1); testMessage.setSender("Bob"); testMessage.setReceiver("Alice"); testMessage.setData("hello"); com.zeroc.Ice.OutputStream out = new com.zeroc.Ice.OutputStream(ic); out.startEncapsulation(); out.writeValue(testMessage); out.endEncapsulation(); com.zeroc.Ice.InputStream in = new com.zeroc.Ice.InputStream(ic, out.finished()); in.setValueFactoryManager( new ValueFactoryManagerI()); in.startEncapsulation(); in.readValue( m-> { System.out.println("Message::::: " + m);}, TestMessage.class); in.endEncapsulation(); } }
0 -
if use Communicator, this line is Unnecessary:
in.setValueFactoryManager( new ValueFactoryManagerI());
0