Skip to content

Commit

Permalink
Merge pull request #15 from baldurrensch/type_fix
Browse files Browse the repository at this point in the history
Typecast when serializing primitive types
  • Loading branch information
schmittjoh committed Dec 19, 2012
2 parents 29d4665 + 04e02d8 commit b8f87c9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function visitString($data, array $type)
$this->root = $data;
}

return $data;
return (string) $data;
}

public function visitBoolean($data, array $type)
Expand All @@ -60,7 +60,7 @@ public function visitBoolean($data, array $type)
$this->root = $data;
}

return $data;
return (boolean) $data;
}

public function visitInteger($data, array $type)
Expand All @@ -69,7 +69,7 @@ public function visitInteger($data, array $type)
$this->root = $data;
}

return $data;
return (int) $data;
}

public function visitDouble($data, array $type)
Expand All @@ -78,7 +78,7 @@ public function visitDouble($data, array $type)
$this->root = $data;
}

return $data;
return (float) $data;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,58 @@ function(VisitorInterface $visitor, AuthorList $data, array $type) {
$this->assertEquals('[{"full_name":"foo","_links":{"details":"http:\/\/foo.bar\/details\/foo","comments":"http:\/\/foo.bar\/details\/foo\/comments"}},{"full_name":"bar","_links":{"details":"http:\/\/foo.bar\/details\/bar","comments":"http:\/\/foo.bar\/details\/bar\/comments"}}]', $this->serialize($list));
}

public function getPrimitiveTypes()
{
return array(
array(
'type' => 'boolean',
'data' => true,
),
array(
'type' => 'boolean',
'data' => 1,
),
array(
'type' => 'integer',
'data' => 123,
),
array(
'type' => 'integer',
'data' => "123",
),
array(
'type' => 'string',
'data' => "hello",
),
array(
'type' => 'string',
'data' => 123,
),
array(
'type' => 'double',
'data' => 0.1234,
),
array(
'type' => 'double',
'data' => "0.1234",
),
);
}

/**
* @dataProvider getPrimitiveTypes
*/
public function testPrimitiveTypes($primitiveType, $data)
{
$visitor = $this->serializationVisitors->get('json')->get();
$functionToCall = 'visit' . ucfirst($primitiveType);
$result = $visitor->$functionToCall($data, array());
if ($primitiveType == 'double') {
$primitiveType = 'float';
}
$this->assertInternalType($primitiveType, $result);
}

protected function getFormat()
{
return 'json';
Expand Down

0 comments on commit b8f87c9

Please sign in to comment.