Skip to content

Commit 4d587a3

Browse files
maxpowaRX14
authored andcommittedOct 26, 2017
Change properties key to _properties_ to reduce chance of conflict (#5180)
1 parent 4b75571 commit 4d587a3

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed
 

‎spec/std/json/mapping_spec.cr

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ private class JSONWithNilableTimeEmittingNull
5555
end
5656
end
5757

58+
private class JSONWithPropertiesKey
59+
JSON.mapping(
60+
properties: Hash(String, String),
61+
)
62+
end
63+
5864
private class JSONWithSimpleMapping
5965
JSON.mapping({name: String, age: Int32})
6066
end
@@ -252,6 +258,14 @@ describe "JSON mapping" do
252258
json.to_json.should eq(%({"value":null}))
253259
end
254260

261+
it "outputs JSON with properties key" do
262+
input = {
263+
properties: {"foo" => "bar"},
264+
}.to_json
265+
json = JSONWithPropertiesKey.from_json(input)
266+
json.to_json.should eq(input)
267+
end
268+
255269
it "parses json with keywords" do
256270
json = JSONWithKeywordsMapping.from_json(%({"end": 1, "abstract": 2}))
257271
json.end.should eq(1)

‎src/json/mapping.cr

+12-12
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ module JSON
6060
# If *strict* is `true`, unknown properties in the JSON
6161
# document will raise a parse exception. The default is `false`, so unknown properties
6262
# are silently ignored.
63-
macro mapping(properties, strict = false)
64-
{% for key, value in properties %}
65-
{% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
63+
macro mapping(_properties_, strict = false)
64+
{% for key, value in _properties_ %}
65+
{% _properties_[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
6666
{% end %}
6767

68-
{% for key, value in properties %}
68+
{% for key, value in _properties_ %}
6969
@{{key.id}} : {{value[:type]}} {{ (value[:nilable] ? "?" : "").id }}
7070

7171
{% if value[:setter] == nil ? true : value[:setter] %}
@@ -90,7 +90,7 @@ module JSON
9090
{% end %}
9191

9292
def initialize(%pull : ::JSON::PullParser)
93-
{% for key, value in properties %}
93+
{% for key, value in _properties_ %}
9494
%var{key.id} = nil
9595
%found{key.id} = false
9696
{% end %}
@@ -101,7 +101,7 @@ module JSON
101101
%key_location = %pull.location
102102
key = %pull.read_object_key
103103
case key
104-
{% for key, value in properties %}
104+
{% for key, value in _properties_ %}
105105
when {{value[:key] || key.id.stringify}}
106106
%found{key.id} = true
107107

@@ -137,15 +137,15 @@ module JSON
137137
end
138138
%pull.read_next
139139

140-
{% for key, value in properties %}
140+
{% for key, value in _properties_ %}
141141
{% unless value[:nilable] || value[:default] != nil %}
142142
if %var{key.id}.nil? && !%found{key.id} && !::Union({{value[:type]}}).nilable?
143143
raise ::JSON::ParseException.new("Missing json attribute: {{(value[:key] || key).id}}", *%location)
144144
end
145145
{% end %}
146146
{% end %}
147147

148-
{% for key, value in properties %}
148+
{% for key, value in _properties_ %}
149149
{% if value[:nilable] %}
150150
{% if value[:default] != nil %}
151151
@{{key.id}} = %found{key.id} ? %var{key.id} : {{value[:default]}}
@@ -159,7 +159,7 @@ module JSON
159159
{% end %}
160160
{% end %}
161161

162-
{% for key, value in properties %}
162+
{% for key, value in _properties_ %}
163163
{% if value[:presence] %}
164164
@{{key.id}}_present = %found{key.id}
165165
{% end %}
@@ -168,7 +168,7 @@ module JSON
168168

169169
def to_json(json : ::JSON::Builder)
170170
json.object do
171-
{% for key, value in properties %}
171+
{% for key, value in _properties_ %}
172172
_{{key.id}} = @{{key.id}}
173173

174174
{% unless value[:emit_null] %}
@@ -216,7 +216,7 @@ module JSON
216216

217217
# This is a convenience method to allow invoking `JSON.mapping`
218218
# with named arguments instead of with a hash/named-tuple literal.
219-
macro mapping(**properties)
220-
::JSON.mapping({{properties}})
219+
macro mapping(**_properties_)
220+
::JSON.mapping({{_properties_}})
221221
end
222222
end

0 commit comments

Comments
 (0)
Please sign in to comment.