5

As we know Serialization in java ,to quote from a blog here is used

to convert the state of an object into a byte stream, which can be persisted into disk/file or sent over the network to any other running Java virtual machine.

  • REST API CASE :

Now considering the second case , to send over a network to another running jvm , if we consider an example of a Rest API , i.e "host:port/path/resource".

Usually I use Spring's @RequestMapping to return the resource model pojo class as ResponseEntity. I do not implement Serializable interface in the model class and all works fine , I get the response of the API in json.

ModelX.java

public class ModelX {

    private int x = 2 ;
    private String xs = "stringx";

    // getters and setters

}

Controller method :

@RequestMapping(value = "/test",method=RequestMethod.POST)
public ResponseEntity<ModelX> getTestModel(@RequestBody ModelX mox){

    ModelX mx = new ModelX();
    mx.setX(mox.getX());
    mx.setXs(mox.getXs());

    return new ResponseEntity<ModelX>(mx, HttpStatus.OK) ;
}

Is it because Spring framework makes it Serializable under the hood with those RestAPI annotations ? If not , how without making it serializable we are able to send over a network .


  • Persistence Case :

Just for more thought , Even in the case of persisting the Objects in database , we use @Entity from JPA , now I tested if the instance of any @Entity annotated class IS-A Serializable or not . and it gives false.

@Entity
class Car {

int id ;
String name ;

//getters and setters

}

testMethod -

Car c = new Car();
System.out.println(c instanceof Serializable);

O/p - false

So even when we try to save this object's state in a database , ORM also does some kind of Serialization under the hood ?

devcodes
  • 1,038
  • 19
  • 38
  • 2
    For the REST part, the state of the object is not converted to a byte stream, but rather to a JSON text document. For the Hibernate/JPA part, see this question : https://stackoverflow.com/questions/2726300/do-hibernate-table-classes-need-to-be-serializable – Arnaud Jul 19 '19 at 07:40
  • @Arnaud alright , makes sense . In modern applications , mostly used format I see is JSON , are there any specific scenarios or applications (maybe if you are aware of )where sending bytestream is more beneficial ? – devcodes Jul 19 '19 at 07:47
  • Converting the state into a bytestream is relevant if the data is to be "sent to"/"read by" another JVM (which will use ObjectInputStream to convert the bytes back to a java object) . – Arnaud Jul 19 '19 at 07:51
  • @Arnaud that I get , but I can still use APIs to talk to another jvms right ? so i was asking if any scenario where bytestream sending/reading would be beneficial , because incase of serialization , still it would have a little overhead of writing to a file and then again reading it . correct me if I am wrong – devcodes Jul 19 '19 at 08:17
  • ok probably one thing could be we could save the state to file , so if the app that sends the data goes down , data will still be available for the other jvm . (won't happen in case of API) – devcodes Jul 19 '19 at 08:29

1 Answers1

1

Serialization is a general notion:

In computer science, in the context of data storage, serialization (or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment).

Serializable is used for a single specific implementation of serialization, which happens to be built into JVM and called "Java serialization". But conversion to JSON, or to whatever protocol database uses for communication are also serialization; they just have nothing to do with Serializable.

are there any specific scenarios or applications (maybe if you are aware of )where sending bytestream is more beneficial

For one, binary formats are smaller than JSON and can be read/written faster. And they can still be cross-platform. But for this you generally would use a different binary format than Java serialization: e.g. Protocol Buffers or Thrift (or many others).

so if the app that sends the data goes down , data will still be available for the other jvm . (won't happen in case of API)

This works perfectly well with JSON as well (or PB or Thrift as above).

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • "This works perfectly well with JSON as well" , right , if we are serializing in json format . – devcodes Jul 19 '19 at 09:25
  • Would this be a good example if I got it all right , suppose I have a game and after some checkpoints/levels I store/write to client file system the progress in a file . So whenever after sometime of game crash or even after system restart , I always pick the progress from that file to resume game – devcodes Jul 19 '19 at 09:27
  • a use case for serialization – devcodes Jul 19 '19 at 10:09
  • 1
    Then yes. For Java serialization in particular, it becomes much more complicated if you want to load save files from older versions of your game, so you may still want to use other frameworks. – Alexey Romanov Jul 19 '19 at 10:23