nevillelyh / protobuf-generic   0.2.9

Apache License 2.0 GitHub

Generic protobuf manipulation

Scala versions: 2.13 2.12


Build Status GitHub license Maven Central Scala Steward badge

Manipulate Protocol Buffers schemas and records in a generic manner without compiled classes, similar to Avro's GenericRecord.


import me.lyh.protobuf.generic._

val schema1 = Schema.of[MyRecord]  // generic representation of the protobuf schema
val jsonString = schema1.toJson  // serialize to JSON
val schema2 = Schema.fromJson(jsonString)  // deserialize from JSON

// read protobuf binary without original class
val bytes1: Array[Byte] = // binary MyRecord
val reader = GenericReader.of(schema2)
val record1 =  // generic record, i.e. Map[String, Any]
val jsonRecord = record1.toJson  // JSON string

// write protobuf binary without orignal class
val record2 = GenericRecord.fromJson(jsonRecord)  // generic record, i.e. Map[String, Any]
val writer = GenericWriter.of(schema2)
val bytes2 = writer.write(record2)  // binary MyRecord


Copyright 2016 Neville Li.

Licensed under the Apache License, Version 2.0: