In PHP, you can use json_encode
to encode an object as a json string.
$string = json_encode($some_object);
However, PHP has the standard slew of datatypes which are not considered objects (ints, strings, etc.) If you pass in a string to json_encode
, this returns a string that contains a javascript statement that could be used to define the string.
In less awkward phrasing, this
echo json_encode("Hello
world, please don't " . '"' . "misuse quote's for emphasis " . "or possessive apostrophes' ");
will output this (a javascript ready string)
"Hello \n\tworld, please don't \"misuse quote'sor possessive apostrophes' "
Is this behavior part of the JSON specification? That is, does JSON define or recommend how an implementation should handle conversion of native, non-object, datatypes? Or even have an opinion on conversion at all? My reading of the RFC left this as ambiguous, but I'm crap at interpreting these things.
I ask because I'm interested in the likelihood of this behavior disappearing from a future version of the function. i.e. If it's codified in a specification somewhere, it's less likely to disappear than if it was a one off someone thought to add on during development.