ejson icon indicating copy to clipboard operation
ejson copied to clipboard

Fully implement generic (encode, decode)

Open jonasrichard opened this issue 10 years ago • 2 comments

We have two possibilities here:

  • encode just gets the term and it creates a jsx term. In this case decode should behave accordingly (as right now it does this)
  • encode get the term and it generates a new term, and ejson call convert recursively. In this case ejson should decode in that way (decode the content and call post_decode function after that)

The rule can be generic and generic_jsx

jonasrichard avatar Nov 30 '15 15:11 jonasrichard

Seeing the code the generic rule should be something like:

{generic, field_name, [{pre_encode, fun/2}, {post_decode/1}, recursive]}

Encoding: pre_encode function is called, and its result will be encoded recursively if recursive keyword is specified. Decoding: the field json value will be decoded as a json recursively, it the keyword is there, and post_decode function is called then.

If we have an out of scope field rule (which doesn't have a field in the record), we should specify a virtual rule.

{virtual, field_name, [{pre_encode, fun/1}, {post_decode fun/2}]}

Encoding: since there is no field value, the pre_encode function is called which produces jsx:term(). So there is no recursive encoding can be imagined. Decoding: the value which belongs to the virtual field name, is passed to the post_decode_fun, and also an internalish representation (jsx:term()) of the record what we have decoded so far. The post_decode function can modify the jsx term, and it will be received by the ejson decoded. So it works as a filter, too.

jonasrichard avatar Dec 01 '15 07:12 jonasrichard

Virtual is implemented, the generic recursive is the only one is waiting to be implemented.

jonasrichard avatar May 09 '16 21:05 jonasrichard