[erlang-questions] Erlang de/serializer and data type validator

Motiejus Jakštys <>
Tue Aug 2 10:27:52 CEST 2011


Hello,

I have a data structure validator. It should check if variable has
a pre-defined data structure.

The validator takes Erlang data type (in this example, #'bp.message'{})
and checks if:
1) to is either {participant | bpid, binary() | undefined}
2) module either se_port_v2 or se_transfer_v2

Part of validator schema:

schema('bp.message') ->
    {record, 'bp.message', [
        {to, schema('bp.message.destination')}
        {module, schema('bp.message.module')},
    ]};

schema('bp.message.destination') ->
    {tuple, {choice, ['participant', 'bpid']}, {choice, ['binary', undefined]}};

schema('bp.message.module') ->
    {choice, [
        se_assign_v2,
        se_release_v2,
    ]};

Two questions:
1) I feel I could do the same with erlang -type. How do I check the data
structure complies?
2) I need serializer and deserializer to JSON, XML or any other common
data format. Maybe there is such a thing in the wild?

Serialized structure must not hold Erlang data type information as I
have to share it with third parties. It should distinguish between atom
and binary from schema (as well as list from tuple..). Serialized message
to JSON would look like this:

{ "bp.message" : {
    "to" : [participant, "yadda"],
    "module" : "se_assign_v2"
     }
}

If I will have to write one, how do I recursively crawl type definition
information?

Motiejus



More information about the erlang-questions mailing list