Obsolete
/
libipc-old
Archived
3
0
Fork 0

Crystal bindings: add support for JSON messages.

master
Philippe Pittoli 2023-02-01 10:03:33 +01:00
parent 1269b55c05
commit af709a665c
1 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,43 @@
require "json"
class IPC::JSON
include ::JSON::Serializable
class_property type = -1
property id : ::JSON::Any?
def type
@@type
end
macro message(id, type, &block)
class {{id}} < ::IPC::JSON
include ::JSON::Serializable
@@type = {{type}}
{{yield}}
end
end
end
class IPC
def schedule(fd : Int32, message : IPC::JSON)
schedule fd, message.type.to_u8, message.to_json
end
end
# CAUTION: Only use this method on an Array(IPC::JSON.class)
class Array(T)
def parse_ipc_json(message : IPCMessage::TypedMessage) : IPC::JSON?
message_type = find &.type.==(message.type)
payload = String.new message.payload
if message_type.nil?
raise "invalid message type (#{message.type})"
end
message_type.from_json payload
end
end