Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is serialization possible for a struct [closed]

Can I serialize a struct type directly since its a value type.

I have used it inside class but wondering if its possible for a struct alone.

e.g.

struct student
{
   name string; --string is a reference type
   age int;
   designation string; --string is a reference type
   salary double;
};

class foo
{
 foo(){
   student s;
   s.name = "example";

   serialize(s);
  }
}

This link says "I tried having my struct implement ISerializable, but I can't implement the required constructor because this is a struct and not an object."

like image 634
Shantanu Gupta Avatar asked May 22 '15 15:05

Shantanu Gupta


People also ask

What type of members are not serialized?

5. What type of members are not serialized? Explanation: All static and transient variables are not serialized.

When should we use serialization?

Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.

Why do we need serialization and deserialization?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

Can we serialize static class?

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). But, static variables belong to class therefore, you cannot serialize static variables in Java.


1 Answers

Yes, you can. I just did so with the following code.

[Serializable]
public struct TestStruct
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }
    public string Value3 { get; set; }
    public double Value4 { get; set; }
}

TestStruct s1 = new TestStruct();
s1.Value1 = 43265;
s1.Value2 = 2346;
s1.Value3 = "SE";

string serialized = jss.Serialize(s1);
s2 = jss.Deserialize<TestStruct>(serialized);
Console.WriteLine(serialized);
Console.WriteLine(s2.Value1 + " " + s2.Value2 + " " + s2.Value3 + " " + s2.Value4);

What did it do? Exactly what it should have, serialized and deserialized the struct.

Output:

{"Value1":43265,"Value2":2346,"Value3":"SE","Value4":5235.3}
43265 2346 SE 5235.3

Funny, that's the TestStruct serialized and deserialized to/from JSON.

What about a default constructor? All struct objects have a default constructor, it's one of the fundamental properties of a struct object, and that default constructor is merely responsible for clearing the memory required for a struct object to the default values. Therefore, the serializer already knows there is a default constructor, and therefore can proceed as if it's a regular object (which it is).

Notes:

This example uses System.Web.Script.Serialization.JavaScriptSerializer.

This example assumes all the variables inside the struct are properties. If they are not, then this answer may not work. It seems to work for me with fields in place of the properties, but that's not a best-practice. You should always make sure that the public variables in all objects are properties.

like image 184
Der Kommissar Avatar answered Sep 22 '22 12:09

Der Kommissar