Download the PHP package nylo/smalljson without Composer
On this page you can find all versions of the php package nylo/smalljson. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nylo/smalljson
More information about nylo/smalljson
Files in nylo/smalljson
Package smalljson
Short Description Shrink Laravel JSON responses with a compact structural encoding and optional deflate. Adds response()->smallJson() and a drop-in middleware; pairs with the smalljson Dio interceptor for Flutter.
License MIT
Homepage https://github.com/nylo-core/smalljson
Informations about the package smalljson
SmallJson for Laravel
Shrink your API's JSON responses with a compact, reversible structural encoding — plus optional deflate — and serve them from a familiar one-liner:
Clients that advertise support get the small payload; everyone else transparently gets plain
JSON. The smalljson Flutter package decodes it with a
drop-in Dio interceptor, so your app code never knows the encoding happened.
How it works
SmallJson pulls every object key into a single key table and collapses uniform collections into row tuples:
For large payloads it can additionally deflate the result (mode z).
This is compression, not encryption. The payload is obfuscated to casual eyes, but anyone with the (open) spec can decode it — confidentiality comes from TLS.
Measured on realistic Laravel API payloads (composer bench):
All sizes in KB; % saved vs plain JSON.
| Payload | plain JSON | smalljson p |
smalljson z |
plain + gzip | p + gzip |
|---|---|---|---|---|---|
| users index, 100 rows + meta | 37.5 KB | 24.7 KB (−34.0%) | 5.2 KB (−86.0%) | 4.0 KB (−89.3%) | 3.9 KB (−89.5%) |
| users index, 15 rows | 5.5 KB | 3.8 KB (−31.4%) | 1.2 KB (−78.7%) | 0.9 KB (−83.8%) | 0.9 KB (−83.8%) |
| single user + 10 posts | 1.7 KB | 1.4 KB (−21.6%) | 0.7 KB (−62.0%) | 0.5 KB (−73.3%) | 0.5 KB (−71.0%) |
Full benchmark output
Read that table honestly: if your web server already gzips JSON responses, SmallJson's
byte savings are modest. It shines when HTTP compression isn't in play — chunked/streamed
responses, misconfigured proxies, shared hosting, internal service calls — and mode z
brings its own compression with it. When the encoded form wouldn't be smaller, SmallJson
automatically sends plain JSON instead, so it never costs you bytes.
Requirements
- PHP 8.1+ (
ext-json,ext-zlib) - Laravel 10, 11, 12 or 13
Installation
The service provider and SmallJson facade are auto-discovered. Optionally publish the
config:
Usage
The macro
Works exactly like response()->json() — same arguments, same serialisation semantics:
The middleware (zero controller changes)
Prefer this if you want existing endpoints — resource responses, paginators, even JSON validation errors — encoded without touching any controller:
The middleware only rewrites JsonResponses, skips HEAD requests, and leaves anything
that isn't plain JSON (JSONP, binary, streams) untouched.
Returning a resource from a route wraps it in
databefore the middleware runs, so the wrapper is preserved. The macro mirrorsresponse()->json()instead, which — like Laravel itself — does not apply resource wrapping.
Manual encode/decode
Useful for websockets, queued payloads, and cache entries. The codec itself
(SmallJson\Codec\Codec) is framework-free if you need it outside Laravel.
Negotiation
By default nothing changes for clients that don't opt in:
- A capable client sends
X-Small-Json: pz(the modes it accepts). The Flutter interceptor does this automatically. - The server responds with
Content-Type: application/vnd.smalljson+jsonand the encoded body — or plain JSON when encoding wouldn't help.Vary: X-Small-Jsonis set either way so shared caches keep the variants apart.
Set 'negotiate' => false to encode for every client — only do this when you control all
consumers. Browsers calling your API with the header from JavaScript will need
X-Small-Json whitelisted in your CORS allowed_headers.
Configuration
| Key | Default | Meaning |
|---|---|---|
enabled |
true (SMALLJSON_ENABLED) |
Master switch; off = plain JSON everywhere |
modes |
'pz' (SMALLJSON_MODES) |
Encodings the server may produce |
negotiate |
true (SMALLJSON_NEGOTIATE) |
Require the request header before encoding |
request_header |
'X-Small-Json' |
Capability header name |
content_type |
application/vnd.smalljson+json |
Marker content type on encoded responses |
min_bytes |
0 (SMALLJSON_MIN_BYTES) |
Skip encoding below this plain-JSON size |
only_when_smaller |
true |
Fall back to plain JSON unless encoding shrinks it |
deflate.min_bytes |
1024 |
Only try mode z at or above this envelope size |
deflate.level |
6 |
DEFLATE level (1–9) |
stats_header |
false (SMALLJSON_STATS) |
Add X-Small-Json-Stats with the savings |
Tip: while evaluating, set SMALLJSON_STATS=true and watch the header:
X-Small-Json-Stats: mode=z; plain=37462; sent=5236; saved=86.0%.
Clients
- Flutter / Dart — the
smalljsonpackage'sSmallJsonInterceptor(Dio). Advertises support, decodes bodies (including error responses), rewrites the content type back toapplication/json. - Browser JS — the packed format decodes in ~30 lines of JavaScript (mode
zadditionally needsnew DecompressionStream('deflate-raw')). - PHP —
SmallJson::decode()in this package.
Testing
The suite includes shared wire-format vectors (tests/Fixtures/vectors.json) verified
byte-for-byte against the Dart implementation — both directions.
License
MIT © Anthony Gordon
All versions of smalljson with dependencies
ext-json Version *
ext-zlib Version *
illuminate/http Version ^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^10.0|^11.0|^12.0|^13.0