I am trying to read and write JSON directly to and from a socket stream using the gson
library.
I have a base class that all messages are derived from:
abstract class MessageBase
{
public String type = this.getClass().toString();
}
An example would be:
class RequestMessage extends MessageBase
{
public String request = "my request";
}
What I wish to do is have the following methods:
MessageBase readMessage(Reader reader);
void writeMessage(Writer writer, MessageBase message);
Where they read and write directly from the stream.
This will allow me to call something like:
writeMessage(myWriter, (MessageBase) (new RequestMessage()))
or
MessageBase message = readMessage(myReader);
if (message instance of RequestMessage)
{
RequestMessage request = (RequestMessage) message;
// do something here
}
I hope it makes sense what I am hoping to achieve, is it even possible?
Any input would be greatly appreciated. Thanks