0

In my app i need to serialize an array of a custom collection type:

IntList[] collections; // need to be serialized

Due to the nature of the coding environment we're using, i cannot rely on 3rd party or any Java built-in packages for doing the serialization and have to perform it all myself.

The best way i could come up with is to store it all in a big byte array, encoding the length of each element before serializing it.

for example, for an array of collections that looks like this:

| 0 |  (1, 6, 3, 7)
| 1 |  (7, 2, 4, 6)
| 2 |  ( 1 )

Would be serialized as:

4 (length of collection at 0) followed by the elements
4 (length of collection at 1) followed by the elements
1 (length of collection at 2) followed by the elements

Is there a better option that would optimize data sizes needed for serialization?

4
  • 1
    Make up your mind. Do you want simplest or smallest? Commented Oct 31, 2013 at 23:20
  • The smallest in size is the solution we need Commented Oct 31, 2013 at 23:31
  • I must be missing something? What's wrong with a CSV is the format of something like this: index,sizeOfCollection,x1,x2,x3 ... newline and so on for each entry in the array? Are you asking for something human readable? And why do you need to denormalize your data, is it really necessary to add the length, when you are listing all the elements? Commented Oct 31, 2013 at 23:35
  • Why can't you use 'built-in packages'? Doesn't leave you much, not even DataOutputStream. Why can't you use ObjectOutputStream for example? Commented Nov 1, 2013 at 0:11

2 Answers 2

1

If size efficiency is the goal then do two things:

  1. Use your system of writing a size value followed by the actual values.
  2. feed the result in to a GZipOutputStream

Compare the two, and if the compressed version is smaller (for more than about 100 values or so it will be smaller) then you can use that version.

When deserializing the values you can try to decompress the stream, and if it is not valid, then just assume it was not the compressed version to start with (put the decompress in a try/catch block and the uncompressed decoding in the catch side).

Sign up to request clarification or add additional context in comments.

Comments

1

I see two possible solutions ...

1) Use a CSV style something like index,sizeOfCollection,x1,x2,x3 ... newline and so on for each entry in the array.

2) This idea is less simple, but at least you would be using a format that's not some weird one you made up. That's not such a good practice.

Write a simple JSON parser. Outputs something like the following ...

[
    {
        "index": 0,
        "size": 4,
        "values": [
            1,
            6,
            3,
            7
        ]
    },
    {
        "index": 1,
        "size": 4,
        "values": [
            7,
            2,
            4,
            6
        ]
    },
    {
        "index": 2,
        "size": 1,
        "values": [
            1
        ]
    }
]

Again be wary of denormalization, not sure why you really need index and size in your serialization?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.