Skip to content

Using serialize/deserialize and #statebox{} #7

@rpt

Description

@rpt

I'm using statebox + statebox_riak and I want to serialize/deserialize the statebox record to JSON, so that it's not ETF and can be read in other languages (i.e. Python Riak client is crashing on ETF binaries:). The problem is that the statebox record is not in a header file and I cannot import it to use it directly. Looking at the accessors (value/1, last_modified/1) and constructors (new/1, new/2) from statebox.erl I don't see a way to actually (de)construct the whole statebox record to (de)serialize it. I need to (de)serialize the queue field, right? Am I missing anything?

For now I just copied the record to my module, which is bad. Here's some code:

-spec serialize(statebox:statebox()) -> binary().
serialize(#statebox{value = Dict,
                    queue = Events,
                    last_modified = Ts}) ->
    Value = orddict:fetch(key, Dict),
    Queue = [serialize_event(E) || E <- Events],
    Json = [{<<"value">>, Value},
            {<<"queue">>, Queue},
            {<<"last_modified">>, Ts}],
    jsx:encode(Json).

If this is really a "bug", I would be happy to make a pull request, just let me know if you prefer to move the record into a header file or add queue/1 and new/3.

-spec serialize(statebox:statebox()) -> binary().
serialize(Statebox) ->
    Value = orddict:fetch(key, statebox:value(Statebox)),
    Queue = [serialize_event(E) || E <- statebox:queue(Statebox)],
    Ts = statebox:last_modified(Statebox),
    Json = [{<<"value">>, Value},
            {<<"queue">>, Queue},
            {<<"last_modified">>, Ts}],
    jsx:encode(Json).

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions