dejajson 0.1.2 copy "dejajson: ^0.1.2" to clipboard
dejajson: ^0.1.2 copied to clipboard

Transparently decode DejaJson-encoded API responses (zlib with a shared dictionary trained on your own API) with a drop-in Dio interceptor. Pairs with the nylo/dejajson Laravel package.

example/dejajson_example.dart

// ignore_for_file: avoid_print

import 'dart:convert';
import 'dart:typed_data';

import 'package:dejajson/dejajson.dart';
import 'package:dio/dio.dart';

Future<void> main() async {
  // Ship the server's trained dictionary as an asset and load it once at
  // startup. In Flutter:
  //
  //   final data = await rootBundle.load('assets/dejajson/dictionary.bin');
  //   final dictionary = DejaJsonDictionary.fromBytes(data.buffer.asUint8List());
  final dictionary =
      DejaJsonDictionary.fromBytes(utf8.encode('…dictionary bytes…'));

  final dio = Dio(BaseOptions(baseUrl: 'https://api.example.com'))
    ..interceptors.add(DejaJsonInterceptor(dictionary: dictionary));

  // The interceptor adds `X-Deja-Json: dz; dict=<id>` to every JSON request.
  // When the server answers with application/vnd.dejajson, the binary body
  // is decoded in place — response.data is ordinary JSON data, exactly as if
  // the server had sent plain JSON.
  final Response<Object?> response = await dio.get<Object?>('/users');
  print(response.data);

  // Manual decoding (websockets, cached blobs, …) works too:
  const codec = DejaJsonCodec();
  final Uint8List envelope =
      codec.encode({'hello': 'world'}, dictionary: dictionary);
  print(codec.decode(envelope, dictionary: dictionary));
}
0
likes
150
points
114
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Transparently decode DejaJson-encoded API responses (zlib with a shared dictionary trained on your own API) with a drop-in Dio interceptor. Pairs with the nylo/dejajson Laravel package.

Repository (GitHub)
View/report issues

Topics

#dio #json #compression #network #interceptor

License

MIT (license)

Dependencies

crypto, dio

More

Packages that depend on dejajson