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

add json mapping extra fields #6009

Closed
wants to merge 3 commits into from
Closed

Conversation

kostya
Copy link
Contributor

@kostya kostya commented Apr 25, 2018

this is extracted for json from #4411

@asterite
Copy link
Member

If we do this, I guess to_json should also take this into account.

@@ -293,6 +293,13 @@ describe "JSON mapping" do
person.other["extra2"].should eq [1, 2, 3]
end

it "should pack extra fields" do
person = JSONPersonWithExtra.from_json(%({"name": "John", "age": 30, "extra1" : 1, "extra2" : [1,2,3]}))
Copy link
Contributor

Choose a reason for hiding this comment

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

Invalid space after key names (extra1, extra2).

@straight-shoota
Copy link
Member

I'm not sure if this feature is a good idea.

What's the use case? Why do we need this in JSON.mapping? To me it doesn't seem to be commonly needed. And it's relatively easy to implement in a custom to_json method.

There are ideas to change the way mappings are defined by using meta attributes (see #3620) which will probably mean removing JSON.mapping at some point. It is still open how a future solution will look like. Unless there is a pressing reason to implement this, it might make sense to figure out the general mechanics first.

@kostya
Copy link
Contributor Author

kostya commented Apr 25, 2018

For me, this is often needed, and needed mostly for from_json method (not to_json). When you have api, which receive some defined data structure, it also sometimes add not defined fields (different every time), which you want to receive. Before that, there is only way to parse it manually, which is pain.

@straight-shoota
Copy link
Member

which you want to receive

Pardon me insisting, but why? You described how this is used (which is quite obvious) but not a real reason.

there is only way to parse it manually, which is pain.

Would it perhaps be an option to improve the manual implementation and make it easier to use?

@kostya
Copy link
Contributor Author

kostya commented Apr 25, 2018

Sometimes third-party api send such json, and you should process it correctly. I not think that manually parse ~30 fields with JSON::Any would be fun.

@straight-shoota
Copy link
Member

straight-shoota commented Apr 25, 2018

I'm still not convinced this is useful enough. But I don't have to decide that either ;)

But I have an idea to solve your problem in a more general and less intrusive way: JSON.mapping could have more advanced handling of keys not included in the mapping (the else branch in the big case statement produced by the macro). Currently, there is strict which configures to either raise or skip. What if there was a third way, a custom handler?

JSON.mapping(strict: ->(key : String, parser : JSON::PullParser) { @extras[key] = JSON::Any.new(parser) })

This needs only one little change in JSON.mapping and is a far more versatile solution. You can even raise for specific conditions.

@kostya
Copy link
Contributor Author

kostya commented Apr 25, 2018

callback is nice, but there is also should be way to extend to_json method. May be

to_json(js) { |js| @extras.each { |k, v| js.field(key) { v.to_json(js) } } }

@straight-shoota
Copy link
Member

Implementing a custom to_json is much easier than parsing.

@kostya
Copy link
Contributor Author

kostya commented Apr 26, 2018

so, should i implement callback? or it not decided.

@asterite
Copy link
Member

The idea of a callback is nice. Maybe JSON.mapping could define a method that by default raises an error, and which you can override to deserialize unknown keys. And same goes for to_json.

If eventually we have meta attributes, it would work the same way.

@asterite
Copy link
Member

But let's first design it: what's the name of the method, etc.

@kostya
Copy link
Contributor Author

kostya commented Jun 16, 2018

fixed by serializable

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

Successfully merging this pull request may close these issues.

None yet

4 participants