Comments (2)
As an update, I was able to recursively render children of the same model by defining them as an attribute and using a method as below:
# frozen_string_literal: true
class CommentSerializer < Panko::Serializer
attributes :id, :children
def children
Panko::ArraySerializer.new(object.children, each_serializer: CommentSerializer).to_a
end
end
I am not sure what the impact to performance this would have, but it seems to be working with no issues for now. Let me know if there is a better way of doing this, I would think this is quite a common use case.
from panko_serializer.
Hey @daniel-koudouna !
First I am sorry for long reply time, I have looked at the bug here and looks like this happen because I am building a "serialization descriptor" (contains attributes, associations and their serializer info) ahead of time and not lazily, from first look it looks like a big one to fix.
I'll try to experiment with multiple options to solve this, but don't have promise of solution for the near-time.
Regarding your suggested solution, it works, but it's not efficient as it could be, since has_many
builds the json-string incrementally (using Oj::StringWriter
) and with your approach it will serialize the children to array of hashes and then to JSON. I might be able to have quick release to address by doing something like:
class CommentSerializer < Panko::Serializer
attributes :id, :children
def children
serialized_json = Panko::ArraySerializer.new(object.children, each_serializer: CommentSerializer).to_json
Panko::JsonValue.from(serialized_json)
end
end
which will be more efficient, if this something that interests you, let me know and I'll work on it.
from panko_serializer.
Related Issues (20)
- Can't compile on windows. Fix seems easy. HOT 5
- Serialization of single object with context / options HOT 2
- How to use a raw JSON string inside a serializer? HOT 2
- Delegate + aliases not working as expected HOT 1
- Adding a custom root name for array serializer HOT 1
- Relax `oj` dependency HOT 3
- Is anyone actively maintaining this project? HOT 2
- Using ActiveRecord's pluck when possible
- Serialize attributes which is used `serialize` in active model definition
- Benchmarks task not working HOT 1
- How do I selectively display a key/value pair? HOT 2
- Alias declaration is ignored when method is added HOT 2
- Migration from AMS HOT 2
- Filter on array serializer? HOT 4
- Conditionally include associations in JSON response HOT 3
- Add context to relations has_one/has_many
- Ability to skip nil values
- Loading dynamic field names during serialization
- Documentation questions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from panko_serializer.