Skip to content

Latest commit

 

History

History
52 lines (39 loc) · 965 Bytes

json.md

File metadata and controls

52 lines (39 loc) · 965 Bytes

JSON Converter

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 with files

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';
});