Dart-RPC: Use Protocol Buffers serialisation instead of JSON

607 views Asked by At

Be default, Dart-RPC uses JSON serialisation when transferring objects (class instances) between the server and the client.

How can I use Protobuf (Protocol Buffers) serialisation instead?
Is it possible to specify the serialisation method (like a content-type) using the Accept request header?

Here's what I tried,

I've used the following .proto definition file representing a Person entity:

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

Which generated person.pb.dart for me using the protoc-gen-dart plugin, by running the command:

protoc person.proto --dart_out=. --plugin ./protoc-gen-dart

And some boilerplate dart-rpc code:

import 'dart:io';
import 'package:rpc/rpc.dart';
import 'person.pb.dart';

const String _API_PREFIX = '/api';
final ApiServer _apiServer =
new ApiServer(apiPrefix: _API_PREFIX, prettyPrint: true);

main() async {
  _apiServer.addApi(new Cloud());
  _apiServer.enableDiscoveryApi();

  HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8080);
  server.listen(_apiServer.httpRequestHandler);
}

@ApiClass(version: 'v1')
class Cloud {
  @ApiMethod(method: 'GET', path: 'resource/{name}')
  Person getResource(String name) {

    Person p = new Person()
      ..id = 1
      ..name = name
      ..email = '[email protected]';

    return p; // ??? p.writeToBuffer(); ???
  }
}

Update

Opened a feature request: https://github.com/dart-lang/rpc/issues/62

1

There are 1 answers

0
Günter Zöchbauer On BEST ANSWER

rpc only supports JSON. You can create a feature request in the GitHub repository.