Sha256: f67bd7fa443a8d2bb4f481954a7e4b579d2a6df6d54121c28ddab8048ef9a670

Contents?: true

Size: 1.68 KB

Versions: 12

Compression:

Stored size: 1.68 KB

Contents

%%% See http://github.com/mojombo/bert.erl for documentation.
%%% MIT License - Copyright (c) 2009 Tom Preston-Werner <tom@mojombo.com>

-module(bert).
-version('1.1.0').
-author("Tom Preston-Werner").

-export([encode/1, decode/1]).

-ifdef(TEST).
-include("test/bert_test.erl").
-endif.

%%---------------------------------------------------------------------------
%% Public API

-spec encode(term()) -> binary().

encode(Term) ->
  term_to_binary(encode_term(Term)).

-spec decode(binary()) -> term().

decode(Bin) ->
  decode_term(binary_to_term(Bin)).

%%---------------------------------------------------------------------------
%% Encode

-spec encode_term(term()) -> term().

encode_term(Term) ->
  case Term of
    [] -> {bert, nil};
    true -> {bert, true};
    false -> {bert, false};
    Dict when is_record(Term, dict, 8) ->
      {bert, dict, dict:to_list(Dict)};
    List when is_list(Term) ->
      lists:map((fun encode_term/1), List);
    Tuple when is_tuple(Term) ->
      TList = tuple_to_list(Tuple),
      TList2 = lists:map((fun encode_term/1), TList),
      list_to_tuple(TList2);
    _Else -> Term
  end.

%%---------------------------------------------------------------------------
%% Decode

-spec decode_term(term()) -> term().

decode_term(Term) ->
  case Term of
    {bert, nil} -> [];
    {bert, true} -> true;
    {bert, false} -> false;
    {bert, dict, Dict} ->
      dict:from_list(Dict);
    {bert, Other} ->
      {bert, Other};
    List when is_list(Term) ->
      lists:map((fun decode_term/1), List);
    Tuple when is_tuple(Term) ->
      TList = tuple_to_list(Tuple),
      TList2 = lists:map((fun decode_term/1), TList),
      list_to_tuple(TList2);
    _Else -> Term
  end.

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
ernie-2.5.2 elib/bert.erl
ernie-2.5.1 elib/bert.erl
ernie-2.5.0 elib/bert.erl
ernie-2.4.0 elib/bert.erl
ernie-2.3.0 elib/bert.erl
ernie-2.2.1 elib/bert.erl
schleyfox-ernie-2.2.3 elib/bert.erl
schleyfox-ernie-2.2.2 elib/bert.erl
schleyfox-ernie-2.2.1 elib/bert.erl
ernie-2.2.0 elib/bert.erl
ernie-2.1.0 elib/bert.erl
ernie-2.0.0 elib/bert.erl