Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Fully implement generic (encode, decode) #41

Open
jonasrichard opened this issue Nov 30, 2015 · 2 comments
Open

Fully implement generic (encode, decode) #41

jonasrichard opened this issue Nov 30, 2015 · 2 comments

Comments

@jonasrichard
Copy link
Owner

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
Copy link
Owner Author

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
Copy link
Owner Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant