yosiat / panko_serializer

High Performance JSON Serialization for ActiveRecord & Ruby Objects
https://panko.dev
MIT License
579 stars 35 forks source link

Documentation questions #157

Closed silva96 closed 1 day ago

silva96 commented 3 months ago

Hey! After reading the documentation, I was wondering what's the difference between all these options, in terms of the output they give and the performance of each.

In a rails controller:

# 1. "new(...).to_json"
render json: {
  users: Panko::ArraySerializer.new(users, each_serializer: UserSerializer).to_json
}

# 2. "new.serialize_to_json(...)"
render json: {
  users: Panko::ArraySerializer.new.serialize_to_json(users, each_serializer: UserSerializer)
}

# 3. "new.serialize(...)"
render json: {
  users: Panko::ArraySerializer.new.serialize(users, each_serializer: UserSerializer)
}

# 4. "new.serialize(...).to_json"
render json: {
  users: Panko::ArraySerializer.new.serialize(users, each_serializer: UserSerializer).to_json
}

# 5. "Panko::Response"
render json: Panko::Response.new(
  users: Panko::ArraySerializer.new(users, each_serializer: UserSerializer)
)

Thanks in advance!

yosiat commented 3 months ago

Hey!

"new(...).to_json" / "new.serialize_to_json(...)" - will return a JSON serialized String, so if you do render json: { .. } you will get an output of object with value as string, this is something I don't think you expect. The way to solve this String issue, is with Panko::Response (option 5).

"new.serialize(...)" / "new.serialize(...).to_json" - serialize creates an array of hashes, together with to_json will return a JSON string on it. For performance reasons it's not recommended to use it, since:

Let me know if you have more questions.

silva96 commented 3 months ago

Thanks! very clear.

metalelf0 commented 2 months ago

Hey @yosiat , thanks for this answer - I think it deserves a place in the documentation on its own :) What's the equivalent for single object serialization?

ObjectSerializer.new.serialize_to_json(object) is preferable over ObjectSerializer.new.serialize(object).to_json, right? In the "getting started" documentation I don't think it's clear which one is recommended.

I think existing documentation could greatly benefit from a page with common use cases, like:

I know most of these things are outside the scope of a serialization library, but they could give hints about possible ways of integrating Panko with the existing rails ecosystem for starters.

Thanks for the great work on this gem!

yosiat commented 1 day ago

@metalelf0 I agree with you here that there are too many options to serialize an array and single object. The next major version of Panko (no ETA) should address this with breaking API changes.

For now, I will mark it as documentation label and will accept PRs for clarifying the documentation.

yosiat commented 1 day ago

I'll close this issue, if there any more questions about this topic, please open discussion. If you find bugs - please open new issue.