GeoJson\Feature\Feature::jsonSerialize PHP Method

jsonSerialize() public method

See also: http://php.net/manual/en/jsonserializable.jsonserialize.php
public jsonSerialize ( )
    public function jsonSerialize()
    {
        $json = parent::jsonSerialize();
        $json['geometry'] = isset($this->geometry) ? $this->geometry->jsonSerialize() : null;
        $json['properties'] = isset($this->properties) ? $this->properties : null;
        // Ensure empty associative arrays are encoded as JSON objects
        if ($json['properties'] === array()) {
            $json['properties'] = new \stdClass();
        }
        if (isset($this->id)) {
            $json['id'] = $this->id;
        }
        return $json;
    }

Usage Example

Esempio n. 1
0
 public function testSerializationShouldConvertEmptyPropertiesArrayToObject()
 {
     $feature = new Feature(null, array());
     $expected = array('type' => 'Feature', 'geometry' => null, 'properties' => new \stdClass());
     $this->assertEquals($expected, $feature->jsonSerialize());
 }