JSON文档映射成对象的PHP库:cartographer

jopen 10年前
这是一个超级简单的PHP库用于将JSON文档映射成对象,类似于Java的 Jackson。
// Create a new instance of the Mapper  $mapper = new \Xenolope\Cartographer\Mapper();    // Map a JSON string to a POPO    // PHP 5.4  $object = $mapper->mapString(      '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',      'Vendor\Package\Entity\Contact'  );    // PHP >=5.5  $object = $mapper->mapString(      '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',      Contact::class  );    // Map an already decoded (to array) JSON document to a POPO    // This might happen automatically in your Request class, for example  $jsonDocument = json_decode(      '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',      true  );    // PHP 5.4  $object = $mapper->map($jsonDocument, 'Vendor\Package\Entity\Contact');    // PHP >= 5.5  $object = $mapper->map(      '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',      Contact::class  );

项目主页:http://www.open-open.com/lib/view/home/1419339290437