Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 1.65 KB

README.md

File metadata and controls

57 lines (44 loc) · 1.65 KB

Pub Build Status Gitter

jaguar_serializer_protobuf

Protobuf support for jaguar_serializer

Getting Started

Install

#pubspec.yaml
dependencies:
  jaguar_serializer_protobuf: 

Usage

Import jaguar_serializer_protobuf

import 'package:jaguar_serializer/jaguar_serializer_protobuf.dart';

Generate your Dart model from your proto definition with proto compiler.

protoc --dart_out=. ./person.proto -I=.

Once your models are generated, you can use them like this:

final serializerRepo = ProtoSerializerRepo()
    ..add((data)=>Person.fromBuffer(data), toJson: (data)=>Person.fromJson(data));

You can then serialize like this:

Person person = Person();
  data.id = 1;
  data.name = 'name';
  data.email = '[email protected]';
List<int> data = serializerRepo.to(person)  

And deserialize like this:

Person person2 = serializerRepo.from<Person>(data)

You can also enable JSON support if you need to debug your network layer. To do so use the isJsonFormatEnabled param of the repo:

final serializerJsonRepo = ProtoSerializerRepo(isJsonFormatEnabled: true)
    ..add((data)=>Person.fromBuffer(data), toJson: (data)=>Person.fromJson(data));
Person personJson = serializerJsonRepo.from<Person>('{"1": "name", "2": 1, "3": "[email protected]"}');