moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.6k stars 440 forks source link

Deserialization Money object #672

Closed md81 closed 2 years ago

md81 commented 2 years ago

Hi,

maybe I am wrong but I think serialization result of Money object should looks like: {"amount": "720000", "currency": {"code": "EUR"}}

instead of: {"amount": "720000", "currency": "EUR"}

Why? Because for example deserialization usually relay on class's constructor, so if \Money\Currency has public function __construct(string $code)

then in this case deserializer will look for code property in JSON, and in fact won't find.

Am I right?

makareth commented 2 years ago

@md81 ,I'd rather agree with you. I tumbled accross this (https://github.com/moneyphp/money/pull/86), and in particular this commit d2a3320022511687962daf8d6ddb376786d327f3.

From what I see, the Symfony Denormalizer will fai lto denormalize a Money/Money normalized object when using JsonSerializableNormalizer, since the code field is absent in currency field.

frederikbosch commented 2 years ago

While I agree that we could have a debate on the serialization structure, I do not think the current structure is problematic. There is no information missing. It is flattened, which could cause problems with automatic (de)serializers that follow the code structure, but that is a problem outside this package.

Besides, interface {} exists for a reason, you can create (de)serializers yourself implementing an interface from your framework, solving (de)serialization Money objects for your particular issue.