What Is Ion?

Quoting the official Ion documentation:

Amazon Ion is a richly-typed, self-describing, hierarchical data serialization format offering interchangeable binary and text representations. The text format (a superset of JSON) is easy to read and author, supporting rapid prototyping. The binary representation is efficient to store, transmit, and skip-scan parse. The rich type system provides unambiguous semantics for long-term preservation of data which can survive multiple generations of software evolution.

Ion was built to address rapid development, decoupling, and efficiency challenges faced every day while engineering large-scale, service-oriented architectures. It has been addressing these challenges within Amazon for nearly a decade, and we believe others will benefit as well.

Simple serialization


<?php
echo ion\serialize([
  
"key" => "value",
  
"more" => [
    
"data" => 123
  
]
]);
?>

Output:


{key:"value",more:{data:123}}

If you now think that this looks a lot like JSON, you're probably right, because Ion is a superset of JSON:


<?php
echo json_encode([
  
"key" => "value",
  
"more" => [
    
"data" => 123
  
]
]);
?>

Output:


{"key":"value","more":{"data":123}}

So, all valid JSON is also valid Ion. Please refer to the official spec to learn more about this topic.

Simple unserialization


<?=
var_representation
(
  
ion\unserialize('{key:"value",more:{data:123}}')
);
?>

Output:


[
  'key' => 'value',
  'more' => [
    'data' => 123,
  ],
]

If you try the same with the JSON equivalent, you'll see that it's basically valid Ion, too:


<?=
var_representation
(
  
ion\unserialize('{"key":"value","more":{"data":123}}')
);
?>

Output:


[
  'key' => 'value',
  'more' => [
    'data' => 123,
  ],
]