The RFC7515 (JWS) and RFC7516 (JWE) introduce several serialization modes.
Compact
JSON Flattened
JSON General
The Compact mode is most know and commonly used as it is compact and URL safe i.e. it is designed for web context. JSON Flattened and General are not URL safe, but provides features that may fit on your application context.
JWS Serialization
To use the JWS serializers, you have to install the jwt-signature component.
composerrequireweb-token/jwt-signature
JWS Compact
This serialization mode is probably the one you know the most. It it a string composed of three parts encoded in Base64 Url Safe and separated by a dot (.).
The serializer class is Jose\Component\Signature\Serializer\CompactSerializer. The associated name is jws_compact.
The serializer manager can be helpful when your application deals more than one serialization mode.
<?phprequire_once'vendor/autoload.php';useJose\Component\Core\Converter\StandardConverter;useJose\Component\Signature\Serializer;$jsonConverter =newStandardConverter();$manager =Serializer\JWSSerializerManager::create([newSerializer\CompactSerializer($jsonConverter),newSerializer\JSONFlattenedSerializer($jsonConverter),newSerializer\JSONGeneralSerializer($jsonConverter),]);// Serializes the second signature (index = 1) of the variable $jws (JWS object) into JSON Flattened serialization mode.
$token = $manager->serialize('jws_json_flattened', $jws,1);// Retrieve the JWS object from a token$jws = $manager->unserialize($token);
JWE Serialization
To use the JWE serializers, you have to install the jwt-encryption component.
JWE Compact
This serialization mode is probably the one you know the most. It it a string composed of five parts encoded in Base64 Url Safe and separated by a dot (.).
The serializer class is Jose\Component\Encryption\Serializer\CompactSerializer. The associated name is jwe_compact.
The serializer manager can be helpful when your application deals more than one serialization mode.
<?phprequire_once'vendor/autoload.php';useJose\Component\Core\Converter\StandardConverter;useJose\Component\Encryption\Serializer;$jsonConverter =newStandardConverter();$manager =Serializer\JWESerializerManager::create([newSerializer\CompactSerializer($jsonConverter),newSerializer\JSONFlattenedSerializer($jsonConverter),newSerializer\JSONGeneralSerializer($jsonConverter),]);// Serializes the second recipient (index = 1) of the variable $jwe (JWE object) into JSON Flattened serialization mode.
$token = $manager->serialize('jwe_json_flattened', $jwe,1);// Retrieve the JWE object from a token$jwe = $manager->unserialize($token);