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