I have been looking for an answer on stackoverflow and on the web more generally for the past two days and I couldn't find a decent answer. However I am surely not the first one to do that:
I am implementing a p2p overlay in Java. The aim is to exchange messages between peers. The messages are represented by a Message class which contains a sender, a receiver and a content. The content is represented as a class extending a Context interface (for each type of messages a new class extending Context is created which can contain additional fields, it is application specific). The className of the context is also store in the message. However I want to keep the messages transmitted over the network language-agnostic which is why I convert my converted them to a JSON object before transmitting them across the network. So far nothing marvelous.
The questions arise when receiving a message: I receive a JSON object which I assume is of class Message (a type checking is performed). It contains a content of the class stored in the message as a className.
Is it wise to store the classname like that and then create an instance of the class to store the content dynamically when receiving a message (i.e. the content will be an instance of the SpecificContent class but seen as the interface Content). The content will then be available at the application level and can be casted to access its specific methods.
is there any better/standard way to send messages over network without simply serializing the java class? (in this case it could not communicate with a program written in C++, etc ) (without talking about CORBA which is nothing less than a Rube Goldberg machine)
Thank you very much for your time, any comment/question/answer is appreciated.
Regards,
B.F.