class
Amber::WebSockets::Decoders::JsonDecoder
Overview
JSON decoder for WebSocket messages.
This is the default decoder used by ClientSocket. It encodes and decodes
messages as JSON strings, which is the standard format for Amber WebSocket
communication.
Example:
decoder = Amber::WebSockets::Decoders::JsonDecoder.new
decoded = decoder.decode(%|{"event":"message","topic":"room:1"}|)
decoded["event"].as_s # => "message"
encoded = decoder.encode({"event" => "message", "topic" => "room:1"})
# => "{\"event\":\"message\",\"topic\":\"room:1\"}"
Defined in:
amber/websockets/decoders/json_decoder.crInstance Method Summary
-
#content_type : String
Returns the MIME content type associated with this decoder.
-
#decode(raw : String) : JSON::Any
Decodes a raw message string into a structured JSON::Any value.
-
#encode(payload : Hash) : String
Encodes a Hash payload into a string suitable for sending over the socket.
-
#encode(payload : JSON::Any) : String
Encodes a JSON::Any payload into a string suitable for sending over the socket.
Instance methods inherited from class Amber::WebSockets::Decoders::Decoder
content_type : String
content_type,
decode(raw : String) : JSON::Any
decode,
encode(payload : Hash) : Stringencode(payload : JSON::Any) : String encode
Instance Method Detail
Returns the MIME content type associated with this decoder.
Decodes a raw message string into a structured JSON::Any value.
Returns a JSON::Any representing the decoded message. Implementations
should raise DecoderError if the message cannot be decoded.
Encodes a Hash payload into a string suitable for sending over the socket.
Encodes a JSON::Any payload into a string suitable for sending over the socket.