Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to write a deserializer?

Reply
Thread Tools

How to write a deserializer?

 
 
Wibble
Guest
Posts: n/a
 
      01-26-2006
I wrote a serializer/deserializer only to realize
that you can't create a new object that
doesn't have a noArg constructor or populate
final slots.

I'm serializing a large structure. Java default
serialization gets stack overflows and won't
deserialize final slots either. The classes
I'm serializing are immutable data carriers.

Am I missing something or do I have to use JNI
to cheat object creation and field setting?
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-26-2006
On Wed, 25 Jan 2006 22:30:55 -0500, Wibble <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>I wrote a serializer/deserializer only to realize
>that you can't create a new object that
>doesn't have a noArg constructor or populate
>final slots.
>
>I'm serializing a large structure. Java default
>serialization gets stack overflows and won't
>deserialize final slots either. The classes
>I'm serializing are immutable data carriers.


IIRC the constructor is not executed on reconstitution. Transient
fields are not reconstituted. As far as I know, final values should
be reconstituted. To deal with the problem, you compose a readObject
method for the class that calls the usual readObject the patches up
the missing fields.

See http://mindprod.com/jgloss/serialization.html
for what I have learned. If you discover errors, please let me know.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Wibble
Guest
Posts: n/a
 
      01-26-2006
Roedy Green wrote:
> On Wed, 25 Jan 2006 22:30:55 -0500, Wibble <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
>
>>I wrote a serializer/deserializer only to realize
>>that you can't create a new object that
>>doesn't have a noArg constructor or populate
>>final slots.
>>
>>I'm serializing a large structure. Java default
>>serialization gets stack overflows and won't
>>deserialize final slots either. The classes
>>I'm serializing are immutable data carriers.

>
>
> IIRC the constructor is not executed on reconstitution. Transient
> fields are not reconstituted. As far as I know, final values should
> be reconstituted. To deal with the problem, you compose a readObject
> method for the class that calls the usual readObject the patches up
> the missing fields.
>
> See http://mindprod.com/jgloss/serialization.html
> for what I have learned. If you discover errors, please let me know.
>


Thanks R,

I need this to be non-intrusive. I cant/dontWantTo modify the
class definitions. I also need to be able to read this from
notJava. I don't think I can rely on classes implementing
deserializers since I'm not using default java serialization
format.

I'll look at ObjectStreamClass.setPrimFieldValues
and see if it does the job.
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      01-26-2006
Wibble wrote:

> I wrote a serializer/deserializer only to realize
> that you can't create a new object that
> doesn't have a noArg constructor or populate
> final slots.
> [...]
> Am I missing something or do I have to use JNI
> to cheat object creation and field setting?


I fear that -- unless you can change the target classes -- you will be forced
to use JNI.

You might find a third-party serialiser that has already done the work for you,
either to use directly or as a source of code to cannibalise. Someone
mentioned XStream a little while ago:

http://xstream.codehaus.org/

it looks quite good, but I have never used it myself.

-- chris


 
Reply With Quote
 
Wibble0@gmail.com
Guest
Posts: n/a
 
      01-26-2006
I figured this out, but its not pretty.

import sun.misc.Unsafe;

private static Unsafe getUnsafe() {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (Unsafe)field.get(null);
} catch (Exception ex) {
throw new RuntimeException("can't get Unsafe instance", ex);
}
}
private static final Unsafe unsafe__ = getUnsafe();
private static void setBoolean(Field f, Object o, boolean v) {
unsafe__.putBoolean(o, unsafe__.objectFieldOffset(f), v);
}
private static Object newInstance(Class clazz) throws Exception {
return(unsafe__.allocateInstance(clazz));
}

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      01-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I figured this out, but its not pretty.
>
> import sun.misc.Unsafe;


I'd forgotten about the "unsafe" stuff; never more than glanced at it to be
honest.

Let's hope it's still there in the next release

(If not you can always stoop to JNI /then/ -- sufficient unto the day are the
evils thereof...)

-- chris


 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      01-26-2006
Chris Uppal wrote:
> (E-Mail Removed) wrote:
>> I figured this out, but its not pretty.
>>
>> import sun.misc.Unsafe;

>
> I'd forgotten about the "unsafe" stuff; never more than glanced at it to be
> honest.
>
> Let's hope it's still there in the next release


Your hope may come to nowt. I suspect you might find the non-documented
packages disappearing from view.

http://weblogs.java.net/blog/ray_gan...we_are_wi.html
"So why will Mustang slip to autumn?
"We've recently decided to make changes to address some issues in
sensitive areas of the codebase (e.g., the classloader) ..."

Elsewhere there have been various mumblings about not relying on
undocumented features.

> (If not you can always stoop to JNI /then/ -- sufficient unto the day are the
> evils thereof...)


Remember that sun.misc.Unsafe is not necessarily what it appears. Calls
to those methods will usually be considered intrinsics (just think how
slow NIO buffers would be without that). Using JNI in place may well be
significantly slower.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM
DVD Write speed - Not able to write at 8 speed Vincent Wonnacott Computer Support 1 09-16-2004 03:14 PM
write a union object using write JasBascom C++ 1 02-24-2004 08:18 AM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM
How to open a new console and write something out write printf() Peter Hansen C Programming 1 08-24-2003 11:49 AM



Advertisments