Support serializing DTOs for front-end consumption
Here is the setup. I have a user model with a DTO, which gets serialized properly to the database, and casted back as well. This works and it looks like that:
{
"full_name": "Jon Doe"
"metadata": "{\"type\":\"metadata\",\"data\":{\"theme\":\"dark\"}}"
}
However, I have another serialization context. Instead of serializing for the database, I want to serialize for front-end consumption. This means that the DTO should be serialized as an actual array, not as a JSON string. This is what I need, instead of the above:
{
"full_name": "Jon Doe"
"metadata": {
"type": "metadata",
"data": {
"theme": "dark",
}
}
}
I assume I could do map($objectFetchedFromDatabase)->with(SomeDedicatedMapper::class)->do(), but I don't think that's ideal. Imagine having to do that for every API or Inertia endpoint.
So maybe the specific JSON-string mapping (example 1) should only be done in a database context, and the normal mapping (example 2) should be the default behavior.
@brendt do you think we can tackle this next? It's a pretty important topic imo, bigger than it seems, and might imply breaking changes (or not)
Yes, we can. Do you mean you want me to look at it?
Yes, I haven't worked too much on the mapper and your insight on this topic (the way to solve this) would be welcome