Decode a String (does not fail silently)
$object = JSONConverter::create()->parse($jsonString);
Parse a File
use Webforge\Common\System\File;
$object = JSONConverter::create()->parseFile(new File('composer.json'));
Stringify an object in pretty print
use Webforge\Common\JS\JSONConverter;
$jsonc = new JSONConverter();
$jsonc->stringify($object, JSONConveter::PRETTY_PRINT)
Catch parsing errors
use Webforge\Common\JS\JSONParsingException;
try {
$object = JSONConverter::create()->parseFile(new File('composer.json'));
} catch (JSONParsingException $e) {
// error handling
}
use Webforge\Common\JS\JSONFile;
$jsonFile = new File('composer.json');
$jsonFile->modify(function() {
return (object) array('and-now'=>'something-completely-different');
});
print $jsonFile
$jsonFile->modify(function($json) {
$json->name = 'webforge/new-common';
});