Skip to content

About

Decode and encode request/response bodies by content type: json, jsonx, ndjson (json lines), url-encoded, xml and yaml.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

chubbyphp-decode-encode

CI Coverage Status Mutation testing badge Latest Stable Version Total Downloads Monthly Downloads

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

Decode and encode request/response bodies by content type: json, jsonx, ndjson (json lines), url-encoded, xml and yaml.

Requirements

  • php: ^8.3
  • symfony/yaml: ^6.4.48|^7.4.21|^8.1.9

Suggest

  • chubbyphp/chubbyphp-container: ^2.5.3
  • psr/container: ^1.1.2|^2.0.2

Installation

Through Composer as chubbyphp/chubbyphp-decode-encode.

composer require chubbyphp/chubbyphp-decode-encode "^1.5"

Usage

Decoder

<?php

use Chubbyphp\DecodeEncode\Decoder\Decoder;
use Chubbyphp\DecodeEncode\Decoder\JsonTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\JsonxTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\NdjsonTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\UrlEncodedTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\XmlTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\YamlTypeDecoder;

$decoder = new Decoder([
    new JsonTypeDecoder(),
    new JsonxTypeDecoder(),
    new NdjsonTypeDecoder(),
    new UrlEncodedTypeDecoder(),
    new XmlTypeDecoder(),
    new YamlTypeDecoder()
]);

print_r($decoder->getContentTypes());
//[
//    'application/json',
//    'application/jsonx+xml',
//    'application/jsonl',
//    'application/x-www-form-urlencoded',
//    'application/xml',
//    'application/x-yaml'
//]

print_r($decoder->decode(
    '{"name": "php"}',
    'application/json'
));
// ['name' => 'php']

print_r($decoder->decode(
    "{\"name\": \"php\"}\n{\"name\": \"typescript\"}\n",
    'application/jsonl'
));
// [['name' => 'php'], ['name' => 'typescript']]

Type Decoder

Encoder

Encoder

<?php

use Chubbyphp\DecodeEncode\Encoder\Encoder;
use Chubbyphp\DecodeEncode\Encoder\JsonTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\JsonxTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\NdjsonTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\UrlEncodedTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\XmlTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\YamlTypeEncoder;

$encoder = new Encoder([
    new JsonTypeEncoder(),
    new JsonxTypeEncoder(),
    new NdjsonTypeEncoder(),
    new UrlEncodedTypeEncoder(),
    new XmlTypeEncoder(),
    new YamlTypeEncoder()
]);

print_r($encoder->getContentTypes());
//[
//    'application/json',
//    'application/jsonx+xml',
//    'application/jsonl',
//    'application/x-www-form-urlencoded',
//    'application/xml',
//    'application/x-yaml'
//]

echo $encoder->encode(
    ['name' => 'php'],
    'application/json'
);
// '{"name":"php"}'

echo $encoder->encode(
    [['name' => 'php'], ['name' => 'typescript']],
    'application/jsonl'
);
// "{\"name\":\"php\"}\n{\"name\":\"typescript\"}\n"

Type Encoder

ServiceFactory

chubbyphp-laminas-config-factory

Copyright

2026 Dominik Zogg

About

Decode and encode request/response bodies by content type: json, jsonx, ndjson (json lines), url-encoded, xml and yaml.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages