Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/active_model/entity/parsers/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ def set_attribute_from_json(name, value)
# Class-level methods.
module ClassMethods
def from_json(json)
unified_json = json.stringify_keys

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deep stringify keys? Or nested entities will be handled by their own from_json?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I expect so

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will with_indifferent_access be a better/faster alternative?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no method with_indifferent_access an instance of ActionController::Parameters

new.tap do |instance|
instance.attributes.each_key do |name|
field_name = name.camelize(:lower)

instance.set_attribute_from_json(name, json[field_name])
instance.set_attribute_from_json(name, unified_json[field_name])
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions spec/active_model/entity/parsers/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,44 @@ class Person
})
end
end

context "parsing symbolized json" do
let(:source) do
{
fieldObj: { x: 1 },
fieldBigInteger: 137,
fieldBoolean: false,
fieldDate: "2024-01-01",
fieldDatetime: "2024-01-01T01:03:07",
fieldFloat: 1.37,
fieldInteger: 138,
fieldString: "string",
fieldTime: "2024-02-03T01:03:08",
fieldRole: { fieldName: "nom", fieldNotMapped: "not_mapped", fieldPosition: { fieldName: "ceo" } },
fieldRoles: [{ fieldName: "prenom", fieldPositions: [{ fieldName: "cpo" }] }],
fieldIntegers: [1, 3, 7],
fieldNotMapped: "not_mapped"
}
end

it "loads an object" do
person = ParsersTest::Person.from_json(source)

expect(person).to be_valid
expect(person.as_json.deep_symbolize_keys).to eq({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it just be to eq(source)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, source have camelized keys

field_obj: { x: 1 },
field_big_integer: 137,
field_boolean: false,
field_date: "2024-01-01",
field_datetime: "2024-01-01T01:03:07.000Z",
field_float: 1.37,
field_integer: 138,
field_string: "string",
field_time: "2000-01-01T01:03:08.000Z",
field_role: { field_name: "nom", field_position: { field_name: "ceo" }, field_positions: nil },
field_roles: [{ field_name: "prenom", field_position: nil, field_positions: [{ field_name: "cpo" }] }],
field_integers: [1, 3, 7]
})
end
end
end