-
Notifications
You must be signed in to change notification settings - Fork 3
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
de-serialization support? #2
Comments
Not yet. I would absolutely love to have it, it was part of the plan from the beginning, but it requires knowledge of two things that I haven't determined how to codify:
If we have an
What I usually do is define a class OrderSerializer < Primalize::Single
def self.deserialize_json(string)
deserialize JSON.parse(string, symbolize_names: true)
end
def self.deserialize(customer_name:, delivery_address:, line_item_ids:, **attrs)
Order.new(
**attrs,
delivery_address: DeliveryAddress.from_string(delivery_address),
line_items: LineItem.find(line_item_ids),
)
end
end (except I usually use |
Okay, thanks for response, makes sense. I was struck by how similar the tasks here are in some ways to what I dealt with in my experimental json_attribute gem. That gem is focused on serializing models to json for the purpose of storing them in a postgres json column, and getting them back. The API ends up being similar, except I base it on ActiveModel::Type stuff, since I was trying to stay close to ActiveRecord but with attributes in JSON. (For that matter, dry-struct also has somewhat similar 'domain' and APIs, but also, I think, is "one way". Several of us seem to be approaching similar things. ) My json_attribute experiment enforces only serializing multi-value instances that have a constructor that take the serialized hash as an initializer, to get around the "how do we know how to restore this" problem. Actually beyond that, at the moment it enforces include'ing a certain module. |
Funny you mention It'd probably be interesting to see attributes(
name: optional(string),
age: integer,
) … into their example code … attribute :name, Types::String.optional
attribute :age, Types::Int
This is hilarious. I settled on |
Is there any way to turn a serialized data structure back into it's original?
The text was updated successfully, but these errors were encountered: