smalljson 1.0.0
smalljson: ^1.0.0 copied to clipboard
Transparently decode SmallJson-encoded API responses with a drop-in Dio interceptor. Pairs with the nylo/smalljson Laravel package.
example/smalljson_example.dart
import 'package:dio/dio.dart';
import 'package:smalljson/smalljson.dart';
Future<void> main() async {
final dio = Dio(BaseOptions(baseUrl: 'https://api.example.com'))
..interceptors.add(SmallJsonInterceptor());
// The interceptor adds `X-Small-Json: pz` to every request. When the server
// answers with application/vnd.smalljson+json, the body is decoded in place
// — response.data is ordinary JSON data, exactly as if the server had sent
// plain JSON.
final response = await dio.get<Object?>('/users');
print(response.data);
// Manual decoding (websockets, cached blobs, …) works too:
const envelope =
'{"_sj":1,"m":"p","k":["id","name"],"b":[2,[0,1],[1,"Ada"],[2,"Grace"]]}';
print(const SmallJsonCodec().decode(envelope));
}