You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-9Lines changed: 19 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,35 +12,43 @@ Reads and writes JSON to and from various input sources.
12
12
13
13
Examples
14
14
--------
15
-
Reading can be done from a string using one of the `Input` implementations:
15
+
Reading can be done from a string, file or stream:
16
16
17
17
```php
18
+
use text\json\Json;
19
+
use io\File;
20
+
use peer\SocketInputStream;
21
+
18
22
// Strings
19
23
$value= Json::read('"Test"');
20
24
21
25
// Input
22
-
$in= new FileInput(new File('input.json'));
23
-
$in= new StringInput('{"Hello": "World"}');
24
-
$in= new StreamInput(new SocketInputStream(...));
26
+
$in= '{"Hello": "World"}');
27
+
$in= new File('input.json');
28
+
$in= new SocketInputStream(/* ... */);
25
29
26
30
$value= Json::read($in);
27
31
```
28
32
29
-
Writing can be done to a stringor using one of the `Output` implementations:
33
+
Writing can be done to a string, file or stream:
30
34
31
35
```php
36
+
use text\json\Json;
37
+
use io\File;
38
+
use peer\SocketOutputStream;
39
+
32
40
// Strings
33
41
$json= Json::of('Test');
34
42
35
43
// Output
36
-
$out= new FileOutput(new File('output.json'));
37
-
$out= new StreamOutput(new SocketOuputStream(...));
44
+
$out= new File('output.json');
45
+
$out= new SocketOuputStream(/* ... */);
38
46
39
47
Json::write($value, $out);
40
48
```
41
49
42
50
### Formatted output
43
-
To change the output format, pass a `Format` instance to the output's constructor. The formats available are:
51
+
To change the output format, use one of the `Output` implementations and pass a `Format` instance to the output's constructor. The formats available are:
44
52
45
53
*`DenseFormat($options)`: Best for network I/O, no unsignificant whitespace, default if nothing given and accessible via `Format::dense($options= ~Format::ESCAPE_SLASHES)`.
46
54
*`WrappedFormat($indent, $options)`: Wraps first-level arrays and all objects, uses whitespace after commas colons. An instance of this format using 4 spaces for indentation and per default leaving forward slashes unescaped is available via `Format::wrapped($indent= " ", $options= ~Format::ESCAPE_SLASHES)`.
@@ -52,7 +60,9 @@ The available options that can be or'ed together are:
52
60
*`Format::ESCAPE_ENTITIES`: Escape XML entities `&`, `"`, `<` and `>`. Per default, these are represented in their literal form.
53
61
54
62
```php
55
-
$out= new FileOutput(new File('glue.json'), Format::wrapped());
63
+
use text\json\{FileOutput, Format};
64
+
65
+
$out= new FileOutput('glue.json', Format::wrapped());
0 commit comments