在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:cweiske/jsonmapper开源软件地址:https://github.com/cweiske/jsonmapper开源编程语言:PHP 100.0%开源软件介绍:JsonMapper - map nested JSON structures onto PHP classesTakes data retrieved from a JSON web service and converts them into nested object and arrays - using your own model classes. Starting from a base object, it maps JSON data on class properties, converting them into the correct simple types or objects. It's a bit like the native SOAP parameter mapping PHP's Type detection works by parsing You do not have to modify your model classes by adding JSON specific code; it works automatically by parsing already-existing docblocks. Keywords: deserialization, hydration Pro & contraBenefits
Drawbacks
UsageBasic usage
Map a normal object: <?php
require 'autoload.php';
$mapper = new JsonMapper();
$contactObject = $mapper->map($jsonContact, new Contact());
?> Map an array of objects: <?php
require 'autoload.php';
$mapper = new JsonMapper();
$contactsArray = $mapper->mapArray(
$jsonContacts, array(), 'Contact'
);
?> Instead of ExampleJSON from an address book web service: {
'name':'Sheldon Cooper',
'address': {
'street': '2311 N. Los Robles Avenue',
'city': 'Pasadena'
}
} Your local <?php
class Contact
{
/**
* Full name
* @var string
*/
public $name;
/**
* @var Address
*/
public $address;
}
?> Your local <?php
class Address
{
public $street;
public $city;
public function getGeoCoords()
{
//do something with $street and $city
}
}
?> Your application code: <?php
$json = json_decode(file_get_contents('http://example.org/sheldon.json'));
$mapper = new JsonMapper();
$contact = $mapper->map($json, new Contact());
echo "Geo coordinates for " . $contact->name . ": "
. var_export($contact->address->getGeoCoords(), true);
?> Property type mapping
Supported type names
ArrayObjects and extending classes are treated as arrays. Variables without a type or with type See phpdoc's type documentation for more information. Simple type mappingWhen an object shall be created but the JSON contains a simple type only (e.g. string, float, boolean), this value is passed to the classes' constructor. Example: PHP code: /**
* @var DateTime
*/
public $date; JSON: {"date":"2014-05-15"} This will result in Class mapWhen variables are defined as objects of abstract classes or interfaces, JsonMapper would normally try to instantiate those directly and crash. Using JsonMapper's $jm = new JsonMapper();
$jm->classMap['Foo'] = 'Bar';
$jm->map(...); This would create objects of type It is also possible to use a callable in case the actual implementation class needs to be determined dynamically (for example in case of a union). The mapped class ('Foo' in the example below) and the Json data are passed as parameters into the call. $mapper = function ($class, $jvalue) {
// examine $class and $jvalue to figure out what class to use...
return 'DateTime';
};
$jm = new JsonMapper();
$jm->classMap['Foo'] = $mapper;
$jm->map(...); NullablesJsonMapper throws an exception when a JSON property is If your API contains many fields that may be $jm->bStrictNullTypes = false; LoggingJsonMapper's Events that get logged:
Handling invalid or missing dataDuring development, APIs often change. To get notified about such changes, JsonMapper can be configured to throw exceptions in case of either missing or yet unknown data. Unknown propertiesWhen JsonMapper sees properties in the JSON data that are
not defined in the PHP class, you can let it throw an exception
by setting $jm = new JsonMapper();
$jm->bExceptionOnUndefinedProperty = true;
$jm->map(...); You may also choose to handle those properties yourself by setting
a callable to /**
* Handle undefined properties during JsonMapper::map()
*
* @param object $object Object that is being filled
* @param string $propName Name of the unknown JSON property
* @param mixed $jsonValue JSON value of the property
*
* @return void
*/
function setUndefinedProperty($object, $propName, $jsonValue)
{
$object->{'UNDEF' . $propName} = $jsonValue;
}
$jm = new JsonMapper();
$jm->undefinedPropertyHandler = 'setUndefinedProperty';
$jm->map(...); Missing propertiesProperties in your PHP classes can be marked as "required" by
putting /**
* @var string
* @required
*/
public $someDatum; When the JSON data do not contain this property, JsonMapper will throw
an exception when $jm = new JsonMapper();
$jm->bExceptionOnMissingData = true;
$jm->map(...); Option $jm = new JsonMapper();
$jm->bRemoveUndefinedAttributes = true;
$jm->map(...); Private properties and functionsYou can allow mapping to private and protected properties and
setter methods by setting $jm = new JsonMapper();
$jm->bIgnoreVisibility = true;
$jm->map(...); Simple types instead of objectsWhen a variable's type is a class and JSON data is a simple type
like If you do not want this, set $jm = new JsonMapper();
$jm->bStrictObjectTypeChecking = true;
$jm->map(...); An exception is then thrown in such cases. Passing arrays to
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论