Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: JSON emit_null option #14

Closed
jackturnbull opened this issue Sep 4, 2019 · 1 comment · Fixed by #34
Closed

feat: JSON emit_null option #14

jackturnbull opened this issue Sep 4, 2019 · 1 comment · Fixed by #34

Comments

@jackturnbull
Copy link

At the moment we're unable to emit null JSON (as this is the crystal default when an option is not supplied).

emit_null: if true, emits a null value for nilable properties (by default nulls are not emitted)
https://crystal-lang.org/api/0.30.1/JSON.html

We ought to be able to add an additional option which will then be forwarded to the options given to the JSON mapping for each property. I can't see a nice enough way to add this without an additional opt on the attribute definition, although this is where I'd expect it to be anyway.

Happy to submit a PR if you're okay with the design of this;

attribute description : String, emit_null: true
JSON.mapping(
  {% for name, opts in PERSIST %}
    {% if opts[:converter] %}
      {{name}}: { type: {{opts[:klass]}} | Nil, converter: {{opts[:converter]}}, emit_null: {{opts[:emit_null]}} },
    {% else %}
      {{name}}: { type: {{opts[:klass]}} | Nil, emit_null: {{opts[:emit_null]}} }
    {% end %}
  {% end %}
)

macro attribute(name, converter = nil, mass_assignment = true, persistence = true, emit_null = false, **tags, &block)
  # ...
end
@stakach
Copy link
Member

stakach commented May 21, 2020

totally missed this, yeah feel free to make a pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants