Esempio n. 1
0
 def body(self):
     Template.body(self)
     message = self._message
     self.add(
         TSimple("serialize_message(Msg) when is_record(Msg, %s) ->" %
                 make_erlang_atom(message.get_name())))
     self.add(
         TSimple(""" lists:concat(["{ \\"%s\\" :  {", """ %
                 message.get_name()))
     self.add(TSimple("string:join(["))
     first = True
     #serialize fields
     for field in message.get_fields():
         if (not first):
             self.add(TSimple(","))
         first = False
         # serialize field
         self.add(
             TSimple("""lists:concat(["\\"%s\\" :", """ %
                     field.get_var_name()))
         self.add(FieldSerializersFactory().get_field_serializer(
             field, "Msg#%s.%s" % (make_erlang_atom(message.get_name()),
                                   make_erlang_atom(field.get_var_name()))))
         self.add(TSimple("])"))
     self.add(TSimple("""], ",") """))
     self.add(TSimple(""" , "}}" ]); """))
Esempio n. 2
0
    def body(self):
        Template.body(self)
        for message in self._protocol.get_messages().as_list():
            logger.debug("adding template for serializating %s message" %
                         message.get_name())
            self.add(MessageSerializer(message))
        self.add(TSimple("serialize_message(Msg) -> unknown_message."))
        # helper functions
        self.add(
            TSimple("""
serialize_int(Val) -> lists:concat(['"', Val, '"']).
serialize_float(Val) -> lists:concat(['"', lists:flatten(io_lib:format("~p",[Val])), '"']).
serialize_string(Val) ->lists:concat(['"', Val, '"']).
serialize_list(Fun, Value) -> lists:concat(["[", string:join(lists:map(Fun, Value), ","), "]"]).
        """))
Esempio n. 3
0
    def body(self):
        Template.body(self)
        for message in self._protocol.get_messages().as_list():
            logger.debug("adding template for deserializng message %s" %
                         message.get_name())
            self.add(MessageDeserializer(message))
        self.add(
            TSimple(
                'inner_deserialize_message(_UnknownMsgName, _MsgBody) -> unknown_message.'
            ))
        self.add(
            TSimple("""
deserialize_message(JsonMsgBody) ->
    {struct, DecodedJson} = mochijson2:decode(JsonMsgBody),
    inner_deserialize_message(DecodedJson).
inner_deserialize_message(DecodedJson) ->
    [MessageName] = proplists:get_keys(DecodedJson),
    {struct, DecodedMsgBody} = proplists:get_value(MessageName, DecodedJson),
    inner_deserialize_message(MessageName, DecodedMsgBody).

%% helper functions
deserialize_int(Data) ->
    bstring_to_int(Data).

deserialize_float(Data) ->
    bstring_to_float(Data).

deserialize_string(Data) ->
    binary_to_list(Data).

deserialize_list(Fun, List) ->
    lists:map(Fun, List).

bstring_to_int(Bstring) ->
    {Int, _Rest} = string:to_integer(binary_to_list(Bstring)),
    Int.

bstring_to_float(Bstring) ->
    {Float, _Rest} = string:to_float(binary_to_list(Bstring)),
    Float.
        """))