When reading some JSON data structures, and then trying to Dump them using YAML::Tiny, I sometimes get the error
YAML::Tiny does not support JSON::XS::Boolean
I understand why this is the case (in particular YAML::Tiny does not support booleans, which JSON is keen to clearly distinguish from other scalars), but is there a quick hack to turn those JSON::XS::Boolean objects into plain 0‘s and 1‘s just for quick dump-to-the-screen purposes?
YAML::Tiny doesn’t support objects. Unfortunately, it doesn’t even have an option to just stringify all objects, which would handle
JSON::XS::Boolean.You can do that fairly easily with a recursive function, though:
This function doesn’t bother processing scalar references, because JSON won’t produce them. It also doesn’t check whether an object actually has overloaded stringification.
Data::Rmap doesn’t work well for this because it will only visit a particular object once, no matter how many times it appears. Since the
JSON::XS::Booleanobjects are singletons, that means it will only find the firsttrueand the firstfalse. It’s possible to work around that, but it requires delving into the source code to determine how keys are generated in itsseenhash:I think the recursive function is clearer, and it’s not vulnerable to changes in
Data::Rmap.