Reputation: 129
I am a relatively newcomer to the world of application development in Symfony. I am presently working in Symfony 2.8 and I often find a debate amongst my colleagues about the use of generic PHP functions like json_decode or json_encode vs Symfony's own JsonDecode and JsonEncode. I am often told that instead of using json_encode or json_decode, we must rather use the encoder classes provided by Symfony namely the JsonEncode and JsonDecode to achieve the same. What I am curious to know,
Is it really that important to discard the use of such generic functions in favour of the Symfony encoder classes ?
Does it help in performance in any way ?
Is such recommendations only a part of better coding strategy or are there any downsides to using, say json_encode in code ?
I really want to know what's behind this hype and any help will be welcome.
Thanks in advance !
Upvotes: 2
Views: 1547
Reputation: 2643
If you are truly achieving the same result, than you are better off using PHP's native json_decode
. as it requires working with Symfony Serializer Component to take advantage of Symfony's encoders. However you can create your own encoder to work with Symfony's Serializer.
Does it help in performance in any way?
No. It adds overhead as it has basically added extra functionality and wrapped it around PHP's native json_decode
. Link to current code.
Is it really that important to discard the use of such generic functions in favour of the Symfony encoder classes ?
Short answer is No, but the real answer is more complicated. Symfony's encoder classes have some advantages when working with entities, this is especially true in newer versions of Symfony. If you setup your project to use Symfony Serializer Component then it might be worth it. even more so if you write your own encoder.
Is such recommendations only a part of better coding strategy or are there any downsides to using, say json_encode in code?
The downside is that you may have to write more code to deserialize your object back to match your entity. The upside is code will not be coupled to Symfony and be easier to port if needed.
Upvotes: 1
Reputation: 166
1) I suppose, since there is no word covering this topic in Symfony Best Practices part of Symfony documentation - no, the usage of such function is not mandatory - you can choose the way which you find more suitable to you, but it could do your code more sustainable to change of PHP versions you using which you will see in answer to your next question.
2) No, it does not - it just a wrappers for json_decode and json_encode with some additional tools provided to us. In case of JsonDecoder it merges the default options of the Json Decoder with context, passed to the function and resolves signature of json_decode between php versions.
3) I think the only downside of using json_decode will be in need of changing parameter set passed to it in every place of your code if you are using it with four arguments in PHP version >= 5.4 and than you suddenly decide to change the version of PHP on your project to something that is < 5.4, but nothing like that with json_encode. For me it's only a part of better coding strategy and nothing more than effort to provide you ease and quickness of made by Symfony project contributors team.
Upvotes: 1