Add json message serializer

Basic serializer to serialize server messages to json.
This commit is contained in:
Tim Kächele 2024-02-18 21:26:30 +01:00
parent bfe77a7c2e
commit 84b0cb9540
2 changed files with 24 additions and 0 deletions

View File

@ -11,6 +11,8 @@ require_relative "server/matching_engine"
require_relative "server/message_broker" require_relative "server/message_broker"
require_relative "server/message_processor" require_relative "server/message_processor"
require_relative "server/websocket_server" require_relative "server/websocket_server"
require_relative "server/json_message_serializer"
module Rex module Rex
module Server module Server
VERSION = "0.1.0" VERSION = "0.1.0"

View 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