Skip to content

Commit

Permalink
Merge pull request #36 from spider-gazelle/feat/json_ann
Browse files Browse the repository at this point in the history
feat: add support for including fields in json serde when persistence…
  • Loading branch information
naqvis authored Apr 17, 2023
2 parents 1e4225c + 2f41457 commit 032e13a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: active-model
version: 4.3.0
version: 4.3.1
crystal: ">= 1.0.0"
license: MIT

Expand Down
11 changes: 7 additions & 4 deletions src/active-model/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ abstract class ActiveModel::Model
{% name = kv[0] %}
{% opts = kv[1] %}
%value = @{{name}}
json.field({{ name.stringify }}) do
json.field({{ (opts[:tags] && opts[:tags][:json_key]) || name.stringify }}) do
{% if opts[:converter] %}
if !%value.nil?
{{ opts[:converter] }}.to_json(%value, json)
Expand Down Expand Up @@ -530,7 +530,7 @@ abstract class ActiveModel::Model
def after_initialize(trusted : Bool)
if !trusted
{% for name, opts in FIELDS %}
{% if !opts[:mass_assign] %}
{% if !opts[:mass_assign] && !opts[:show] %}
@{{name}} = nil
{% end %}
{% end %}
Expand Down Expand Up @@ -705,6 +705,7 @@ abstract class ActiveModel::Model
name,
mass_assignment = true,
persistence = true,
show = false,
serialization_group = [] of Symbol,
**tags,
&block
Expand All @@ -721,15 +722,15 @@ abstract class ActiveModel::Model
# Assign instance variable to correct type
@[JSON::Field(
presence: true,
ignore: {{ !persistence }},
ignore: {{ !show && !persistence }},
key: {{tags[:json_key]}},
emit_null: {{tags[:json_emit_null]}},
root: {{tags[:json_root]}},
{{**tags}}
)]
@[YAML::Field(
presence: true,
ignore: {{ !persistence }},
ignore: {{ !show && !persistence }},
key: {{tags[:yaml_key]}},
emit_null: {{tags[:yaml_emit_null]}},
{{**tags}}
Expand Down Expand Up @@ -768,6 +769,7 @@ abstract class ActiveModel::Model
klass: resolved_type,
converter: tags[:converter],
mass_assign: mass_assignment,
show: show,
should_persist: persistence,
serialization_group: serialization_group,
tags: tags.empty? == true ? nil : tags,
Expand All @@ -780,6 +782,7 @@ abstract class ActiveModel::Model
klass: resolved_type,
converter: tags[:converter],
mass_assign: mass_assignment,
show: show,
should_persist: persistence,
serialization_group: serialization_group,
tags: tags.empty? == true ? nil : tags,
Expand Down

0 comments on commit 032e13a

Please sign in to comment.