Skip to content
This repository has been archived by the owner on Apr 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #13 from xp-framework/refactor/use-xpforge-json
Browse files Browse the repository at this point in the history
Use xp-forge/json
  • Loading branch information
thekid committed Jun 8, 2016
2 parents ab7c413 + d0feda3 commit 99d84ff
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 73 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"keywords": ["module", "xp"],
"require" : {
"xp-framework/core": "^7.0 | ^6.5",
"xp-framework/webservices": "^7.0 | ^6.3",
"xp-framework/http": "^7.0 | ^6.2",
"xp-framework/xml": "^7.0 | ^6.0",
"xp-framework/scriptlet": "^8.0 | ^6.2",
"xp-framework/scriptlet": "^8.0.1",
"xp-framework/logging": "^7.0 | ^6.5",
"xp-framework/collections": "^7.0 | ^6.5",
"xp-framework/networking": "^7.0 | ^6.6",
"xp-framework/tokenize": "^7.0 | ^6.5",
"xp-forge/json": "^2.1",
"php" : ">=5.5.0"
},
"require-dev" : {
Expand Down
17 changes: 2 additions & 15 deletions src/main/php/webservices/rest/RestJsonDeserializer.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace webservices\rest;

use webservices\json\JsonFactory;

use text\json\StreamInput;

/**
* A JSON deserializer
Expand All @@ -10,14 +9,6 @@
* @test xp://net.xp_framework.unittest.webservices.rest.RestJsonDeserializerTest
*/
class RestJsonDeserializer extends RestDeserializer {
protected $json;

/**
* Constructor. Initializes decoder member
*/
public function __construct() {
$this->json= JsonFactory::create();
}

/**
* Deserialize
Expand All @@ -27,10 +18,6 @@ public function __construct() {
* @throws lang.FormatException
*/
public function deserialize($in) {
try {
return $this->json->decodeFrom($in);
} catch (\webservices\json\JsonException $e) {
throw new \lang\FormatException('Malformed JSON', $e);
}
return (new StreamInput($in))->read();
}
}
51 changes: 16 additions & 35 deletions src/main/php/webservices/rest/RestJsonSerializer.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace webservices\rest;

use webservices\json\JsonFactory;
use text\json\StreamOutput;
use text\json\Format as WireFormat;

/**
* A JSON serializer
Expand All @@ -9,19 +10,21 @@
* @test xp://net.xp_framework.unittest.webservices.rest.RestJsonSerializerTest
*/
class RestJsonSerializer extends RestSerializer {
protected $json;
private $format;

/**
* Constructor. Initializes decoder member
* Constructor.
*
* @param text.json.Format $format Optional wire format, defaults to *dense* format
*/
public function __construct() {
$this->json= JsonFactory::create();
public function __construct(WireFormat $format= null) {
$this->format= $format ?: WireFormat::dense();
}

/**
* Return the Content-Type header's value
*
* @return string
* @return string
*/
public function contentType() {
return 'application/json; charset=utf-8';
Expand All @@ -30,36 +33,14 @@ public function contentType() {
/**
* Serialize
*
* @param var $payload
* @param io.streams.OutputStream $out
* @return void
* @param var $payload
* @param io.streams.OutputStream $out
* @return void
*/
public function serialize($payload, $out) {
$val= $payload instanceof Payload ? $payload->value : $payload;
if ($val instanceof \Traversable) {
$i= 0;
$map= null;
foreach ($val as $key => $element) {
if (0 === $i++) {
$map= 0 !== $key;
$out->write($map ? '{ ' : '[ ');
} else {
$out->write(' , ');
}

if ($map) {
$this->json->encodeTo($key, $out);
$out->write(' : ');
}
$this->json->encodeTo($element, $out);
}
if (null === $map) {
$out->write('[ ]');
} else {
$out->write($map ? ' }' : ' ]');
}
} else {
$this->json->encodeTo($val, $out);
}
(new StreamOutput($out, $this->format))->write($payload instanceof Payload
? $payload->value
: $payload
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class RestFormatTest extends TestCase {
public function json_serialize() {
$res= new MemoryOutputStream();
RestFormat::$JSON->write($res, new Payload(['name' => 'Timm']));
$this->assertEquals('{ "name" : "Timm" }', $res->getBytes());
$this->assertEquals('{"name":"Timm"}', $res->getBytes());
}

#[@test]
public function json_deserialize() {
$req= new MemoryInputStream('{ "name" : "Timm" }');
$req= new MemoryInputStream('{"name":"Timm"}');
$v= RestFormat::$JSON->read($req, MapType::forName('[:string]'));
$this->assertEquals(['name' => 'Timm'], $v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public function strings($str) {
$this->assertEquals('"'.$str.'"', $this->serialize($str));
}

#[@test, @values([-1, 0, 1, 4711])]
public function integers($int) {
$this->assertEquals(''.$int, $this->serialize($int));
#[@test, @values([['-1', -1], ['0', 0], ['1', 1], ['4711', 4711]])]
public function integers($expected, $int) {
$this->assertEquals($expected, $this->serialize($int));
}

#[@test, @values([-1.0, 0.0, 1.0, 47.11])]
public function decimals($decimal) {
$this->assertEquals(''.$decimal, $this->serialize($decimal));
#[@test, @values([['-1.0', -1.0], ['0.0', 0.0], ['1.0', 1.0], ['47.11', 47.11]])]
public function decimals($expected, $decimal) {
$this->assertEquals($expected, $this->serialize($decimal));
}

#[@test]
Expand All @@ -57,23 +57,23 @@ public function boolean_false() {

#[@test]
public function empty_array() {
$this->assertEquals('[ ]', $this->serialize([]));
$this->assertEquals('[]', $this->serialize([]));
}

#[@test]
public function int_array() {
$this->assertEquals('[ 1 , 2 , 3 ]', $this->serialize([1, 2, 3]));
$this->assertEquals('[1,2,3]', $this->serialize([1, 2, 3]));
}

#[@test]
public function string_array() {
$this->assertEquals('[ "a" , "b" , "c" ]', $this->serialize(['a', 'b', 'c']));
$this->assertEquals('["a","b","c"]', $this->serialize(['a', 'b', 'c']));
}

#[@test]
public function string_map() {
$this->assertEquals(
'{ "a" : "One" , "b" : "Two" , "c" : "Three" }',
'{"a":"One","b":"Two","c":"Three"}',
$this->serialize(['a' => 'One', 'b' => 'Two', 'c' => 'Three'])
);
}
Expand All @@ -84,7 +84,7 @@ public function string_map() {
#])]
public function traversable_array($in) {
$this->assertEquals(
'[ 1 , 2 , 3 ]',
'[1,2,3]',
$this->serialize($in)
);
}
Expand All @@ -95,7 +95,7 @@ public function traversable_array($in) {
#])]
public function traversable_map($in) {
$this->assertEquals(
'{ "color" : "green" , "price" : "$12.99" }',
'{"color":"green","price":"$12.99"}',
$this->serialize($in)
);
}
Expand All @@ -106,7 +106,7 @@ public function traversable_map($in) {
#])]
public function empty_traversable($in) {
$this->assertEquals(
'[ ]',
'[]',
$this->serialize($in)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public function process_greet_with_missing_parameter() {
'output' => 'text/json'
];
$this->assertProcess(
400, ['Content-Type: text/json'], '{ "message" : "Parameter \"name\" required, but not found in path(\'name\')" }',
400, ['Content-Type: text/json'], '{"message":"Parameter \"name\" required, but not found in path(\'name\')"}',
$route, $this->newRequest()
);
}
Expand Down Expand Up @@ -557,7 +557,7 @@ public function process_exceptions_from_handler_constructor() {
];

$this->assertProcess(
500, ['Content-Type: text/json'], '{ "message" : "Cannot instantiate" }',
500, ['Content-Type: text/json'], '{"message":"Cannot instantiate"}',
$route, $this->newRequest()
);
}
Expand All @@ -574,7 +574,7 @@ public function process_exceptions_from_handler_constructor_are_not_mapped() {
];

$this->assertProcess(
500, ['Content-Type: text/json'], '{ "message" : "Cannot instantiate" }',
500, ['Content-Type: text/json'], '{"message":"Cannot instantiate"}',
$route, $this->newRequest()
);
}
Expand All @@ -591,7 +591,7 @@ public function process_errors_from_handler_method() {
];

$this->assertProcess(
500, ['Content-Type: text/json'], '{ "message" : "Invocation failed" }',
500, ['Content-Type: text/json'], '{"message":"Invocation failed"}',
$route, $this->newRequest()
);
}
Expand All @@ -608,7 +608,7 @@ public function process_exceptions_from_handler_method_are_mapped() {
];

$this->assertProcess(
409, ['Content-Type: text/json'], '{ "message" : "Invocation failed" }',
409, ['Content-Type: text/json'], '{"message":"Invocation failed"}',
$route, $this->newRequest()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ public function cannot_route() {
$res= new HttpScriptletResponse();
$fixture->doProcess($req, $res);

ob_start();
$res->sendContent();
$sent= ob_get_contents();
ob_end_clean();

$this->assertEquals(404, $res->statusCode);
$this->assertEquals('Content-Type: application/json', $res->headers[0]);
$this->assertEquals('{ "message" : "Could not route request to http:\/\/localhost\/" }', $res->getContent());
$this->assertEquals('{"message":"Could not route request to http://localhost/"}', $sent);
}

#[@test, @values([
Expand Down

0 comments on commit 99d84ff

Please sign in to comment.