Add json message serializer
Basic serializer to serialize server messages to json.
This commit is contained in:
parent
bfe77a7c2e
commit
84b0cb9540
@ -11,6 +11,8 @@ require_relative "server/matching_engine"
|
||||
require_relative "server/message_broker"
|
||||
require_relative "server/message_processor"
|
||||
require_relative "server/websocket_server"
|
||||
|
||||
require_relative "server/json_message_serializer"
|
||||
module Rex
|
||||
module Server
|
||||
VERSION = "0.1.0"
|
||||
|
22
lib/rex/server/json_message_serializer.rb
Normal file
22
lib/rex/server/json_message_serializer.rb
Normal file
@ -0,0 +1,22 @@
|
||||
module Rex
|
||||
module Server
|
||||
class JsonMessageSerializer
|
||||
def serialize(obj)
|
||||
obj = if obj.is_a?(Array)
|
||||
{
|
||||
type: "response",
|
||||
name: "validation.errors",
|
||||
data: obj.map(&:to_h)
|
||||
}
|
||||
else
|
||||
{
|
||||
type: "response",
|
||||
name: obj.class.name.split("::").last,
|
||||
data: obj.to_h
|
||||
}
|
||||
end
|
||||
obj.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user