Skip to content

Commit

Permalink
fixed serialization to serialize datetime fields as strings instead o…
Browse files Browse the repository at this point in the history
…f the actual DateTime objects. important format change for anyone using serialized datetime fields

DateTime fields were previously being serialized like below which was not the intention:

"created_at":{"date":"2010-05-18 23:25:10","timezone_type":2,"timezone":"EDT"}

It will now be serialized to a string like so:

"created_at":"2010-05-18T23:26:26-0400"

The default format is ISO8601 but this can be changed. See http://github.com/kla/php-activerecord/commit/faef7a853870d3efa3e7dce48a256f46478a630d for details
  • Loading branch information
kla committed May 19, 2010
1 parent aad50cf commit 41e52fe
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/Serialization.php
Expand Up @@ -233,7 +233,7 @@ class JsonSerializer extends Serialization

public function to_s()
{
return json_encode(self::$include_root ? array(strtolower(get_class($this->model)) => $this->attributes) : $this->attributes);
return json_encode(self::$include_root ? array(strtolower(get_class($this->model)) => $this->to_a()) : $this->to_a());
}
}

Expand Down Expand Up @@ -263,7 +263,7 @@ private function xml_encode()
$this->writer->openMemory();
$this->writer->startDocument('1.0', 'UTF-8');
$this->writer->startElement(strtolower(denamespace(($this->model))));
$this->write($this->attributes);
$this->write($this->to_a());
$this->writer->endElement();
$this->writer->endDocument();
$xml = $this->writer->outputMemory(true);
Expand Down Expand Up @@ -299,4 +299,4 @@ private function write($data, $tag=null)
}
}
}
?>
?>
3 changes: 2 additions & 1 deletion test/SerializationTest.php
Expand Up @@ -126,7 +126,8 @@ public function test_to_xml()
public function test_to_xml_works_with_datetime()
{
Author::find(1)->update_attribute('created_at',new DateTime());
$this->assert_reg_exp('/<updated_at><date>/',Author::find(1)->to_xml());
$this->assert_reg_exp('/<updated_at>[0-9]{4}-[0-9]{2}-[0-9]{2}/',Author::find(1)->to_xml());
$this->assert_reg_exp('/"updated_at":"[0-9]{4}-[0-9]{2}-[0-9]{2}/',Author::find(1)->to_json());
}

public function test_to_xml_skip_instruct()
Expand Down

0 comments on commit 41e52fe

Please sign in to comment.