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

Question about JSON::Serializable #190

Closed
taylorfinnell opened this Issue Dec 2, 2018 · 3 comments

Comments

3 participants
@taylorfinnell

taylorfinnell commented Dec 2, 2018

When I use this in a model I end up with stuff like:

{
...
game_id_changed": false,
        "text_changed": false,
        "eliminated_changed": false,
        "advancing_changed": false,
        "category_changed": false,
        "question_number_changed": false,
        "question_count_changed": false,
...
}

Is there a way for Jennifer to exclude its internals from this automatically?

Thanks!

@imdrasil

This comment has been minimized.

Owner

imdrasil commented Dec 3, 2018

Hm, ATM out of the box the only way how to deal with this is use instance.to_h.to_json. ATM Jennifer has no specialized tools for JSON serializing (or integration with any serializing mechanism. If this doesn't help you - I will think out some more clear solution

@imdrasil imdrasil added the question label Dec 3, 2018

@imdrasil imdrasil self-assigned this Dec 3, 2018

@z64

This comment has been minimized.

Contributor

z64 commented Dec 3, 2018

@imdrasil You should only need to generate @[JSON::Field(ignore: true)] in mapping

require "json"

class Base
  macro mapping(*values)
    {% for value in values %}
      @[JSON::Field(ignore: true)]
      @{{value}}_id_changed : Bool = false
    {% end %}
  end
end

class Model < Base
  include JSON::Serializable

  mapping(foo, bar, baz)
end

p Model.new.to_json # "{}"

https://carc.in/#/r/5ojz

@imdrasil

This comment has been minimized.

Owner

imdrasil commented Dec 3, 2018

@z64 Nice catch, I'll add this as a temporary solution - I'm thinking about adding built-in #to_json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment