mead-steve/json-er

This package is abandoned and no longer maintained. The author suggests using the mead-steve/json-er package instead.

Simple helper class for converting data to json.

1.0.0 2013-01-24 22:03 UTC

This package is auto-updated.

Last update: 2021-03-29 06:14:06 UTC


README

Small utility class to help out with converting PHP objects to json.

Example Usage

Basic usage is very straight forward:

$jsonBuilder = new \MeadSteve\JSONer\JSONer();

$dataToOutput = array(
  'id'               => "y76",
  'requestedData'	 => $ComplexObject
);

$outputString = $jsonBuilder->convertToJSON($dataToOutput);

This assumes that you want to use php's default behaviour to encode the ComplexObject. If you wanted a bit more control then ideally you would implement the JsonSerializable in the class. However If this isn't an option you can provide handler functions to the JSONer object:

$jsonBuilder->registerSerializeFunction('ComplexObject', function($Object) {
  $moreSimpleObject = new stdClass();
  $moreSimpleObject->propertyOne = $Object->getPropertyOne();
  return $moreSimpleObject;
});