refactor serde interfaces and impls
[awesomized/ext-ion] / docs / tutorial / : Tutorial / :2. What is ion.md
1 # What Is Ion?
2
3 <!--
4 <?php
5 if (!function_exists("var_representation")) {
6 function var_representation($v) {
7 return print_r($v,true);
8 }
9 }
10 ?>
11 -->
12
13 Quoting the [official Ion documentation](https://amzn.github.io/ion-docs/):
14
15 **Amazon Ion** is a [richly-typed](ion/:%20Tutorial/:1.%20Getting%20started#Rich.type.system), [self-describing](ion/:%20Tutorial/:1.%20Getting%20started#Self-describing), hierarchical data serialization format offering [interchangeable binary and text](https://amzn.github.io/ion-docs/guides/why.html#dual-format-interoperability) representations. The [text format](https://amzn.github.io/ion-docs/docs/spec.html) (a superset of [JSON](http://json.org/)) is easy to read and author, supporting rapid prototyping.
16
17 The [binary representation](https://amzn.github.io/ion-docs/docs/binary.html) is [efficient to store, transmit, and skip-scan parse](https://amzn.github.io/ion-docs/guides/why.html#read-optimized-binary-format).
18
19 The rich type system provides unambiguous semantics for longterm preservation of data which can survive multiple generations of software evolution.
20
21 Ion was built to address rapid development, decoupling, and efficiency challenges faced every day while engineering large-scale, service-oriented architectures.
22
23 ### Some simple examples
24
25 #### Serialization:
26
27 ```php
28 <?=
29 ion\serialize([
30 "key" => "value",
31 "more" => [
32 "data" => 123
33 ]
34 ]);
35 ?>
36 ```
37
38 ##### Output:
39
40 ```json
41 {key:"value",more:{data:123}}
42 ```
43
44 If you now think that this looks a lot like JSON, you're probably right, because Ion is a superset of JSON:
45
46 ```php
47 <?=
48 json_encode([
49 "key" => "value",
50 "more" => [
51 "data" => 123
52 ]
53 ]);
54 ?>
55 ```
56
57 ##### Output:
58
59 ```json
60 {"key":"value","more":{"data":123}}
61 ```
62
63 So, all valid JSON is also valid Ion. Please refer to the [official spec](https://amzn.github.io/ion-docs/docs/spec.html) to learn more about this topic.
64
65 #### Unserialization:
66
67 ```php
68 <?=
69 var_representation(
70 ion\unserialize('{key:"value",more:{data:123}}')
71 );
72 ?>
73 ```
74
75 ##### Output:
76
77 ```php
78 [
79 'key' => 'value',
80 'more' => [
81 'data' => 123,
82 ],
83 ]
84 ```
85
86 If you try the same with the JSON equivalent, you'll see that it's basically valid Ion, too:
87
88 ```php
89 <?=
90 var_representation(
91 ion\unserialize('{"key":"value","more":{"data":123}}')
92 );
93 ?>
94 ```
95
96 ##### Output:
97
98 ```php
99 [
100 'key' => 'value',
101 'more' => [
102 'data' => 123,
103 ],
104 ]
105 ```
106
107 ### Multiple documents
108
109 Ion supports multiple sequences of documents within a single stream; consider the following:
110
111 ```php
112 <?=
113 var_representation(
114 ion\unserialize('
115 {"key":"value","more":{"data":123}}
116 {"key":"value","more":{"data":456}}
117 ', new ion\Unserializer\Unserializer(multiSequence: true)
118 )
119 );
120 ?>
121 ```
122
123 #### Output:
124
125 ```php
126 [
127 [
128 'key' => 'value',
129 'more' => [
130 'data' => 123,
131 ],
132 ],
133 [
134 'key' => 'value',
135 'more' => [
136 'data' => 456,
137 ],
138 ],
139 ]
140 ```
141
142 ### Annotations
143
144 Any Ion value can include one or more annotation symbols denoting the semantics of the content. This can be used to:
145
146 - Annotate individual values with schema types, for validation purposes.
147 - Associate a higher-level datatype (e.g. a Java class) during serialization processes.
148 - Indicate the notation used within a `blob` or `clob` value.
149 - Apply other application semantics to a single value.
150
151 In the text format, type annotations are denoted by a non-null symbol token and double-colons preceding any value. Multiple annotations on the same value are separated by double-colons:
152
153 ```
154 int32::12 // Suggests 32 bits as end-user type
155 degrees::'celsius'::100 // You can have multiple annotaions on a value
156 'my.custom.type' :: { x : 12 , y : -1 } // Gives a struct a user-defined type
157
158 { field: some_annotation::value } // Field's name must precede annotations of its value
159
160 jpeg :: {{ ... }} // Indicates the blob contains jpeg data
161 bool :: null.int // A very misleading annotation on the integer null
162 '' :: 1 // An empty annotation
163 null.symbol :: 1 // ERROR: type annotation cannot be null
164 ```
165
166 Except for a small number of predefined system and PHP annotations, Ion itself neither defines nor validates such annotations; that behavior is left to applications or tools (such as schema validators).
167
168 It’s important to understand that annotations are symbol *tokens*, not symbol *values*. That means they do not have annotations themselves. In particular, the text `a::c` is a single value consisting of three textual tokens (a symbol, a double-colon, and another symbol); the first symbol token is an *annotation* on the value, and the second is the *content* of the value.
169
170 #### System Annotations
171
172 ```php
173 <?php
174
175 foreach (ion\Symbol\System::cases() as $e) {
176 printf("%30s:: => %s\n", $e->value, $e->name);
177 }
178
179 /*
180 $ion:: => Ion
181 $ion_1_0:: => Ivm_1_0
182 $ion_symbol_table:: => IonSymbolTable
183 name:: => Name
184 version:: => Version
185 imports:: => Imports
186 symbols:: => Symbols
187 max_id:: => MaxId
188 $ion_shared_symbol_table:: => SharedSymbolTable
189 */
190
191 ?>
192 ```
193
194 #### PHP Annotations
195
196 There are two handful of annotations used by PHP, which are centralized in the ion\Symbol\PHP enumeration:
197
198 ```php
199 <?php
200
201 foreach (ion\Symbol\PHP::cases() as $e) {
202 printf("%3s:: => %s\n", $e->value, $e->name);
203 }
204
205 /*
206 PHP:: => PHP
207 R:: => Reference
208 r:: => Backref
209 p:: => Property
210 o:: => Object
211 c:: => ClassObject
212 O:: => MagicObject
213 C:: => CustomObject
214 E:: => Enum
215 S:: => Serializable
216 */
217
218 ?>
219 ```
220
221 ---
222
223 ## Next up
224
225 * [Standard Datatypes](ion/:%20Tutorial/:3.%20Standard%20Datatypes)