Permalink
Browse files

fixed serialization to serialize datetime fields as strings instead o…

…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 41e52fe532ec6bdd35925955af41a0e3bf6920a2
Showing with 5 additions and 4 deletions.
  1. +3 −3 lib/Serialization.php
  2. +2 −1 test/SerializationTest.php
View
@@ -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());
}
}
@@ -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);
@@ -299,4 +299,4 @@ private function write($data, $tag=null)
}
}
}
?>
?>
@@ -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()

0 comments on commit 41e52fe

Please sign in to comment.