Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify how to Serialize my object if it is in a list

I have a somewhat complicated Class that may or may not contain a list of items of the same type

Class Items
{
   List<Items> SubItems ...

}

The class itself is serializeable, however its going to be a giant waste of space to serialize the objects in the list since they are serialized and added to the database prior to being included in the list.

Is there a way that I can specify that the list should be represented as a list of integers when its serialized ?

Note: These integers will represent primary keys of the rows where the serialized object is located.

like image 990
TacticalMin Avatar asked Aug 05 '13 20:08

TacticalMin


1 Answers

Is there a way that I can specify that the list should be represented as a list of integers when its serialized?

Yep. There are two main options:

1) Implement the ISerializable-Interface, where you can control how the object will be serialized / deserialized.

OR

2) Declare your List as [NonSerialized] and also manage a private Member List, which contains your primary keys. You will have to implement the logic for loading / storing the Integer-List by your own, though.

If your class to serialize is quite big, I would recommend you the second approach, because in the other case you have to manually serialize / deserialize each property.

like image 194
Fabian Bigler Avatar answered Oct 02 '22 20:10

Fabian Bigler