treeform / jsony

A loose, direct to object json parser with hooks.
MIT License
270 stars 34 forks source link

extend `skipHook` to skip object fields based on values #85

Open georgelemon opened 7 months ago

georgelemon commented 7 months ago

tests included

pietroppeter commented 7 months ago

Great idea! I guess an example in the Readme would also be welcome, so that the feature is discoverable (I am not the maintainer)

georgelemon commented 7 months ago

The best use case scenario is when working with APIs. I want to use jsony to serialize/deserialize objects in requests & responses. For example

type
  Product = object 
    id, slug: string          # filled by the server
    published: bool
    title, content: string

When making a POST request, I can't send an object that contains an emptyid field (or slug), because those fields are available in a GET request, so most probably I will get an error from the server. Reusing the same Nim object and stripping fields based on their values would be possible with the runtime skipHook