Skip to content

Commit c79c4a4

Browse files
authored
Merge pull request #18 from xp-forge/feature/json_io
Extend Json::read() and Json::write() to accept files and streams
2 parents 64890c1 + 7ba8977 commit c79c4a4

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

src/main/php/text/json/Json.class.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace text\json;
22

3+
use io\File;
4+
use io\streams\{InputStream, OutputStream};
5+
use lang\IllegalArgumentException;
6+
37
/**
48
* Simple entry point class
59
*
@@ -11,25 +15,42 @@ abstract class Json {
1115
/**
1216
* Reads from an input
1317
*
14-
* @param string|text.json.Input $input
18+
* @param string|text.json.Input|io.File|io.stream.InputStream $input
1519
* @return var
1620
*/
1721
public static function read($input) {
1822
if ($input instanceof Input) {
19-
return $input->read();
23+
// NOOP
24+
} else if ($input instanceof File) {
25+
$input= new FileInput($input);
26+
} else if ($input instanceof InputStream) {
27+
$input= new StreamInput($input);
2028
} else {
21-
return (new StringInput($input))->read();
29+
$input= new StringInput((string)$input);
2230
}
31+
32+
return $input->read();
2333
}
2434

2535
/**
2636
* Writes to an output and returns the output
2737
*
2838
* @param var $value
29-
* @param text.json.Output $output
39+
* @param text.json.Output|io.File|io.stream.OutputStream $output
3040
* @return text.json.Output The given output
41+
* @throws lang.IllegalArgumentException
3142
*/
32-
public static function write($value, Output $output) {
43+
public static function write($value, $output) {
44+
if ($output instanceof Output) {
45+
// NOOP
46+
} else if ($output instanceof File) {
47+
$output= new FileOutput($output);
48+
} else if ($output instanceof OutputStream) {
49+
$output= new StreamOutput($output);
50+
} else {
51+
throw new IllegalArgumentException('Expected an Output, File or OutputStream, have '.typeof($output));
52+
}
53+
3354
$output->write($value);
3455
return $output;
3556
}

src/test/php/text/json/unittest/JsonTest.class.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php namespace text\json\unittest;
22

3-
use lang\FormatException;
3+
use io\streams\{MemoryInputStream, MemoryOutputStream};
4+
use io\{Files, TempFile};
5+
use lang\{FormatException, IllegalArgumentException};
46
use test\{Assert, Expect, Test};
57
use text\json\{Format, Json, StringInput, StringOutput};
8+
use util\Bytes;
69

710
class JsonTest {
811

@@ -16,6 +19,26 @@ public function read_string() {
1619
Assert::equals('Test', Json::read('"Test"'));
1720
}
1821

22+
#[Test]
23+
public function read_casts_input_to_string() {
24+
Assert::equals('Test', Json::read(new Bytes('"Test"')));
25+
}
26+
27+
#[Test]
28+
public function read_file() {
29+
$file= new TempFile();
30+
try {
31+
Assert::equals('Test', Json::read($file->containing('"Test"')));
32+
} finally {
33+
$file->unlink();
34+
}
35+
}
36+
37+
#[Test]
38+
public function read_stream() {
39+
Assert::equals('Test', Json::read(new MemoryInputStream('"Test"')));
40+
}
41+
1942
#[Test, Expect(FormatException::class)]
2043
public function read_malformed_string() {
2144
Json::read('this.is.not.json');
@@ -31,6 +54,26 @@ public function write_output() {
3154
Assert::equals('"Test"', Json::write('Test', new StringOutput())->bytes());
3255
}
3356

57+
#[Test]
58+
public function write_file() {
59+
$file= new TempFile();
60+
try {
61+
Assert::equals('"Test"', Files::read(Json::write('Test', $file)->file()));
62+
} finally {
63+
$file->unlink();
64+
}
65+
}
66+
67+
#[Test]
68+
public function write_stream() {
69+
Assert::equals('"Test"', Json::write('Test', new MemoryOutputStream())->stream()->bytes());
70+
}
71+
72+
#[Test, Expect(IllegalArgumentException::class)]
73+
public function write_incorrect_type() {
74+
Json::write('Test', $this);
75+
}
76+
3477
#[Test]
3578
public function of_string() {
3679
Assert::equals('"Test"', Json::of('Test'));

0 commit comments

Comments
 (0)