cvs: pecl /sdo/DAS Json.php

From: Date: Mon, 23 Oct 2006 16:53:08 +0000
Subject: cvs: pecl /sdo/DAS Json.php
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-6535@lists.php.net to get a copy of this message
slaws Mon Oct 23 16:53:08 2006 UTC Modified files: /pecl/sdo/DAS Json.php Log: Added a PHP userspace implementation of encode to ensure that SDO many valued types are written out as arrays.

http://cvs.php.net/viewvc.cgi/pecl/sdo/DAS/Json.php?r1=1.2&r2=1.3&diff_format=u Index: pecl/sdo/DAS/Json.php diff -u pecl/sdo/DAS/Json.php:1.2 pecl/sdo/DAS/Json.php:1.3 --- pecl/sdo/DAS/Json.php:1.2 Tue Oct 17 13:25:17 2006 +++ pecl/sdo/DAS/Json.php Mon Oct 23 16:53:08 2006 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ | Author: SL | +----------------------------------------------------------------------+ -$Id: Json.php,v 1.2 2006/10/17 13:25:17 slaws Exp $ +$Id: Json.php,v 1.3 2006/10/23 16:53:08 slaws Exp $ */ @@ -83,12 +83,6 @@ * Pass 2 - recurse through the PHP object again creating * an SDO data graph based on the type model from Pass 1 * and the data from the PHP oject - * - * The stage of complexity after this is to allow XML schema to be - * passed in to define the shape of the SDO tree that is expected to - * be generated. This doesn't sit that easily with JSON RPC as would - * would not expect to find an XML schema. We need to do some work - * on how complex types might be described for the JSON format. */ public function decode($jsonString) { @@ -101,7 +95,7 @@ // that we will not be passed fragments of JSON. I.e. $jason_string // will always start with '{' and end with '}' $sdo = $this->data_factory->create('GenericNS', 'GenericType'); - $this->_copyObjectToSDO($json, $sdo); + $this->_decodeObjectToSDO($json, $sdo); //print_r($sdo); return $sdo; @@ -111,22 +105,22 @@ * Loops round all of the properties in a PHP object arranging for them * to be copied into an SDO */ - private function _copyObjectToSDO($object, $sdo) + private function _decodeObjectToSDO($object, $sdo) { foreach ( $object as $param_name => $param_value ) { - $this->_copyToSDO($param_name, $param_value, $sdo); + $this->_decodeToSDO($param_name, $param_value, $sdo); } } /** * A recursive function that copies PHP arrays to an SDO data graph */ - private function _copyArrayToSDO($array_name, $array, $sdo) + private function _decodeArrayToSDO($array_name, $array, $sdo) { $index = 0; foreach ( $array as $array_entry ) { $array_index = $array_name . $index; - $this->_copyToSDO($array_index, $array_entry, $sdo); + $this->_decodeToSDO($array_index, $array_entry, $sdo); $index = $index + 1; } } @@ -135,19 +129,23 @@ * Makes the decision on how the PHP object should be copied * and recurse as necessary */ - private function _copyToSDO($item_name, $item, $sdo) + private function _decodeToSDO($item_name, $item, $sdo) { $item_type = gettype($item); - //echo "Name: " . $item_name . " Value: " . $item . " Type: ". $item_type . "\n"; - +/* + $debug = "Name: " . $item_name . " Value: " . $item . " Type: ". $item_type . "\n"; + file_put_contents("json_messages.txt", + $debug, + FILE_APPEND); +*/ if ( $item_type == "object" ) { $new_sdo = $this->data_factory->create('GenericNS', 'GenericType'); $sdo[$item_name] = $new_sdo; - $this->_copyObjectToSDO($item, $new_sdo); + $this->_decodeObjectToSDO($item, $new_sdo); } else if ( $item_type == "array" ) { $new_sdo = $this->data_factory->create('GenericNS', 'GenericType'); $sdo[$item_name] = $new_sdo; - $this->_copyArrayToSDO($item_name, $item, $new_sdo); + $this->_decodeArrayToSDO($item_name, $item, $new_sdo); } else { $sdo[$item_name] = $item; // Could do some work here to determine the type of the parameter @@ -160,11 +158,174 @@ * json_encode funtion from the JSON PECL package and SDO's ability * to pretend to be an array */ - public function encode ( $sdo ) +/* + public function encode_old ( $sdo ) { return json_encode($sdo); } - +*/ + public function encode ( $sdo ) + { + $json_string = null; + + $this->_encodeObjectFromSDO($sdo, $json_string); + + return $json_string; + } + + private function _encodeObjectFromSDO ( $sdo, &$json_string ) + { + $json_string .= "{"; + + $reflection = new SDO_Model_ReflectionDataObject($sdo); + $sdo_type = $reflection->getType(); + + $last = end($sdo); + reset($sdo); + foreach ( $sdo as $property_name => $property_value ) { + $json_string .= "\"" . $property_name . "\":"; + + $sdo_property = null; + $sdo_property_type = null; + $sdo_property_type_name = null; + $is_array = false; + $is_object = false; + + // get the property entry from the type so that we can. + // find out if we are dealing with a many valued property. + // Need to take account of any open types where the named property + // won't exist in the model + try { + $sdo_property = $sdo_type->getProperty($property_name); + $sdo_property_type = $sdo_property->getType(); + $sdo_property_type_name = $sdo_property_type->getName(); + $is_array = $sdo_property->isMany(); + $is_object = !$sdo_property_type->isDataType(); + + } catch (SDO_PropertyNotFoundException $ex ) { + if ( $sdo_type->isOpenType() == true ) { + // We can validly have properties that + // don't appear in the model. For now we + // assume that these are single valued + // so $is_array is left as false. + // + // We have to go a little further to get the + // property type as we can't pluck it directly from + // the model (the model doesn't have the type in + // it as the model is open). + + // First take a look at the php type as we can't reflect + // on the data unless it is an SDO object (as opposed to + // a primitive type) + $php_type = gettype($property_value); + if ( $php_type == "object") { + $is_object = true; + $reflection = new SDO_Model_ReflectionDataObject($property_value); + $sdo_property_type = $reflection->getType(); + $sdo_property_type_name = $sdo_property_type->getName(); + } else { + $is_object = false; + $sdo_property_type_name = $php_type; + } + } else { + // there is a real problem so throw + // the exception on + throw $ex; + } + } + + if ( $is_array ) { + // it's an array + $this->_encodeArrayFromSDO($property_value, $json_string, $sdo_property_type); + } else if ( $is_object ) { + // it's an object + $this->_encodeObjectFromSDO($property_value, $json_string); + } else { + // it's a primitive type + $this->_encodePrimitiveFromSDO($property_value, $json_string, $sdo_property_type_name); + } + + if ( $property_value != $last ) { + $json_string .= ","; + } + } + + $json_string .= "}"; + } + + private function _encodeArrayFromSDO ( $sdo, &$json_string, $sdo_type ) + { + $json_string .= "["; + + if ( $sdo_type->isDataType() == true ) + { + // it's an array of primitives + $last = end($sdo); + reset($sdo); + foreach ( $sdo as $property_name => $property_value ) { + $this->_encodePrimitiveFromSDO($property_value, $json_string, $sdo_type); + if ( $property_value != $last ) { + $json_string .= ","; + } + } + } else { + // it's an array of objects + $last = end($sdo); + reset($sdo); + foreach ( $sdo as $property_name => $property_value ) { + $this->_encodeObjectFromSDO($property_value, $json_string); + if ( $property_value != $last ) { + $json_string .= ","; + } + } + } + + $json_string .= "]"; + } + + private function _encodePrimitiveFromSDO ( $sdo, &$json_string, $type_name ) + { + // Note. both PHP and SDO primitive types are included here to + // cover the open type case where I can't predice from the + // model what might have been added to the SDO + switch ($type_name) { + case "Boolean": + + case "boolean": + if ( $sdo == true ) { + $json_string .= "true"; + } else { + $json_string .= "true"; + } + break; + case "Byte": + case "Bytes": + case "Character": + case "Date": + case "String": + case "URI": + + case "string": + $json_string .= "\"" . $sdo . "\""; + break; + case "BigDecimal": + case "BigInteger": + case "Double": + case "Float": + case "Integer": + case "Long": + case "Short": + + case "integer": + case "float": + case "double": + $json_string .= $sdo; + break; + + // TODO - what to do about nulls + } + } + public function __destruct() { }
« previous php.pecl.cvs (#6535) next »