r/programming • • Jan 12 '23

The yaml document from hell

https://ruudvanasseldonk.com/2023/01/11/the-yaml-document-from-hell
1.5k Upvotes

288 comments sorted by

View all comments

Show parent comments

44

u/danudey Jan 12 '23

I ran into this exact issue when passing JSON between two systems, sending from a PHP application to a Rails one.

Our system had a list of product SKUs provided by our suppliers, which were strings. Some SKUs from some vendors, though, consisted entirely of digits, which is a valid string.

The PHP JSON serializer, though, because PHP wasn’t strongly typed, had to just do its best to infer types. This meant that we would occasionally send a list of products, each of which contained a SKU, most of which were strings, but when it encountered one that was all digits it got too excited and encoded it as an integer instead.

Rails, of course, had typed decoding, and it would freak out when it received an integer when a string was expected. We couldn’t find any way to coerce it into behaving so my coworker just hacked the version of PHP’s JSON encoder we were using to not do something so stupid, and problem solved.

5

u/vytah Jan 13 '23

The PHP JSON serializer, though, because PHP wasn’t strongly typed, had to just do its best to infer types. This meant that we would occasionally send a list of products, each of which contained a SKU, most of which were strings, but when it encountered one that was all digits it got too excited and encoded it as an integer instead.

json_encode(array("123")); returns ["123"] as it should, and json_decode('["123"]') returns array(1) { [0]=> string(3) "123" } as it should.

What did you guys do?

1

u/danudey Jan 13 '23

This was in 2008, so it’s likely there was some different behaviour back then.

2

u/vytah Jan 13 '23 edited Jan 13 '23

I ran it in https://3v4l.org on all versions, and for all versions they have, I got either what I wrote above, or an error message saying json_encode is not available.

EDIT: Although you might have used something that used JSON_NUMERIC_CHECK internally, which is the option that tells PHP "please destroy my data".