3

I encounter a problem that with Java, I have a map,such as map<K,V>, K and V can be arbitrary type, e.g int, Long, String, Time, etc.

After the map is serialized, can I get the length of K or the V? Can I write a common method to implement this idea? Something like:

public long getLength(object obj) {
    //how to get the length of this obj, obj can be any type
}

How could do that?

jww
  • 97,681
  • 90
  • 411
  • 885
user3231931
  • 331
  • 1
  • 2
  • 8
  • Serialization also implies serialization of the classes and fields, so values of some type having a fixed size is rarely valid. – Joop Eggen Jan 24 '14 at 12:42
  • 4
    What do you mean by the "length of a type"? – Thomas Jan 24 '14 at 12:43
  • I mean any type(e.g Long int Time String...) after serializtion, how to get the bytes length, in the interface of Serializable,there's no method to get the length. – user3231931 Jan 24 '14 at 12:58
  • To clarify, do you mean you want to be able to find out *before* you serialize, how many bytes long the result would be *after*? – Dan Getz Jan 24 '14 at 18:10
  • possible duplicate of [Estimate serialization size of objects?](http://stackoverflow.com/questions/3069018/estimate-serialization-size-of-objects) – jww Jan 25 '14 at 15:54

2 Answers2

0

Nope.

But you can approximate though, here's a nice article about a sizeof function.

The reason is that you can change the default binary serialization (which is kinda verbose). There are comparisons for these tools, the last time I was in this topic Kyro was the most optimal (10x smaller than the default Java binary serialization, because it does not neither export redundant nor anything verification-related data).

enter image description here

Here's a comparison about the tools.

rlegendi
  • 10,466
  • 3
  • 38
  • 50
0

There's no way to get the length of an object after serialization except by serializing it.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413