Consider that you've got a POJO that you intend to serialize and send through a socket.
You can use whatever serialization strategy you wish (JSON, XML, protobuf, ..., etc) to serialize the actual POJO into a byte[], then you send it through the socket.
The byte[] arrives on the other end, but in this receiving context you do not know what class the information represents, so how do you know which POJO class to construct to begin populating its fields?
I'd want to do this without the need to have multiple endpoints/sockets within the context of which I could assume the type of data that is being received. I want to receive all sorts of different POJOs in the same socket context.
One idea was to share a mapping across these contexts, mapping a type code to a class type. I could then build some sort of user defined frame with which to transport the data.
| 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 |
+----------------+---------------+---------------+----------------+
| TYPE CODE SECTION (32 bit int) |
+----------------+---------------+---------------+----------------+
| PAYLOAD LENGTH SECTION (32 bit int) |
+----------------+---------------+---------------+----------------+
| PAYLOAD SECTION |
+----------------+---------------+---------------+----------------+
| PAYLOAD CONTINUED.... |
+----------------+---------------+---------------+----------------+
: ..... :
+----------------+---------------+---------------+----------------+
On serialization, I would insert this type code into the frame, then append on the byte[] that is the serialized POJO, then send this frame!
On the other end, I could extract this type code from the frame, and look it up in the shared mapping. Voilla! I now know which class was sent through the socket, and can de-serialize smoothly
However, this seems bad, because now all of these contexts are coupled together with this shared mapping. What if I want to make my project micro-service oriented? It could get ugly, especially if the mapping could be different for a different use case.
It occurred to me that this is a problem people have solved, and maybe I just don't know the name for this type of thing, or the high level design patterns/ideas.
Could someone provide some context? What solutions already exist that already solve this problem? Is there a name for this type of thing?