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

Serializing a resource to json and then building it back from json #354

Open
kujma10-zz opened this issue Jul 23, 2019 · 2 comments
Open
Assignees

Comments

@kujma10-zz
Copy link

kujma10-zz commented Jul 23, 2019

Hello,

I have implemented caching logic in Redis for the json api client resource in order to not request external service every time. In my example this resource is called User.

The problem with caching is that if I store the whole User object and if they are many then it is too slow (to write 1000 users already takes ~ 4-5 seconds, same is with reading).

To make it fast I changed it so that instead of writing the object itself - I store the json (converting it with user.to_json, like it is done on active records) and then when reading building the resource back with User.new(ActiveSupport::JSON.decode(user_json)) (again similar to active record).

This approach I thought it worked at first but then I noticed that during this serialisation and deserialisation the roles relationship of the object is lost. After adding this relation with has_many :roles to the model it worked but then the attributes of role e.g name is not there. To sum up - my goal is to have this equality:
User.new(ActiveSupport::JSON.decode(User.first.to_json)) == User.first which is true in case of active record but not true if it is about JsonApiClient::Resource.

Can you please recommend any way to retrieve the same object back from json? If there is none then maybe you could give suggestion for alternative caching solution. Thanks in advance.

@gaorlov
Copy link
Collaborator

gaorlov commented Sep 4, 2019

This is an excellent point. I will take a look and see what the work would be like to make this happen. I bet there's a bunch of use cases for this.

@gaorlov gaorlov self-assigned this Sep 4, 2019
@umitozturk
Copy link

umitozturk commented Oct 6, 2021

It's been a long time but I guess i've found a solution for this :)

format_user = JsonApiClient::Parsers::Parser.parse(
                              ParasutApi::Resources::SalesInvoice,
                              OpenStruct.new(body: JSON.parse(user_data.to_json), env: {})
                            )

according to this parser's explanation you need to convert your hash data to json after that you need to pass an object to JsonApiClient's parser. JsonApiClient handles data, meta, relationships, included etc.

https://github.com/JsonApiClient/json_api_client/blob/118fa6cd3a9a5445f4e7ad9085efa680f5828d2d/lib/json_api_client/parsers/parser.rb

you can reach user_roles by calling format_user.user_roles or format_user.user_roles like that.

regarding this, a new method can be added to JsonApiClient what do you think about it @gaorlov ?

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

No branches or pull requests

3 participants