ratchetphp / Ratchet

Asynchronous WebSocket server
http://socketo.me
MIT License
6.28k stars 743 forks source link

Support for custom serializer missing #299

Closed janukobytsch closed 5 years ago

janukobytsch commented 9 years ago

Currently I have to override the WampConnection to plug in my own serializer. Otherwise the custom serialized response is passed into json_encode again and I have to deal with double quotes. There is a number of reasons you would want to use a custom serializer (e.g. annotation based exclusion of certain fields). I feel like this would be a valuable addition to Ratchet.

cboden commented 9 years ago

Can you elaborate a bit? The WAMP specification states the format transmitted is to be JSON. I'm open to the idea of a custom serializer if it makes sense.

janukobytsch commented 9 years ago

As of now, every WAMP message sent via callResult or callError is passed into the default json_encode (referring to WampConnection.php). This makes sense in terms of complying with the WAMP specification, but doesn't offer a whole lot of flexibility. Of course you could also implement the JsonSerializable interface to provide a custom serialized representation of your objects. But why bother when there are great serialization libraries like JMS (http://jmsyst.com/libs/serializer), which allow for much more flexibility and control by enabling exclusion strategies and custom annotations. This is especially relevant if you want to serialize Doctrine entities that contain lots of fields which are irrelevant to the client.

For this reason, I believe there should be the option to inject your own json serializer with json_encode as a fallback. If you are concerned about developers breaking the WAMP spec by serializing to other formats, there could be additional validation as well. I would be happy to send a pull request with an implementation if you consider this sensible.

cboden commented 9 years ago

My apologies...I still don't quite understand. It looks like a good library but I don't understand when a developer would ever try not to use JSON with WAMP because the client wouldn't understand it...perhaps when using a different protocol other than WAMP this would be desirable.

Can you perhaps post some of your code where you were running into an issue to help me understand?

kinncj commented 9 years ago

He doesn't want to serialize to an object which is not a JavaScript Object Notation; He does want the flexibility to use his own serializer (or a vendor) which may add abilities such as how to serialize/transform a given data, add handlers and etc (That's why he mentioned JMS).

mbonneau commented 5 years ago

There are no plans to implement custom serializers inside of Ratchet.

If you would like to use a custom serializer, you can explicitly use them inside of callResult, etc., by serializing to an array:

// with JMS
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$array = $serializer->toArray($object);

or otherwise wrapping the call.