Coder Social home page Coder Social logo

Comments (2)

daniel-koudouna avatar daniel-koudouna commented on June 13, 2024

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.

yosiat avatar yosiat commented on June 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.