@guardian/thrift-serializer

2.1.0 • Public • Published

Thrift Serializer

  • 2.11: Maven Central
  • 2.12: Maven Central

A library for serializing a thrift model into bytes.

How to use

  • Add the library as a depency by adding
"com.gu" %% "thrift-serializer" % "1.1.1"

to your applications libraryDepencies.

  • You can then use ThriftSerializer in your application:
import com.gu.thrift.serializer.ThriftSerializer

object myObject extends ThriftSerializer {

  def myMethod(thriftObject: MyThriftObject): Unit = {
    val bytes = serializeToBytes(thriftObject)
    //Do something with bytes
  }
}
  • You can use the ThriftDeserializer like this:

MyDeserializer.deserialize(buffer).map(myEvent => {
  // process the event
}

def myMethod(thriftObject: MyThriftObject): Unit = {
  val bytes = serializeToBytes(thriftObject)
  //Do something with bytes
}

If there is no compression type recorded in the bytes you wish to deserialize or if you don't want these to be included when serializing a thrift object, you can set the last arguments of the function calls to true. They are false by default.

MyDeserializer.deserialize(buffer, true).map(myEvent => {
    // process the event
}
def myMethod(thriftObject: MyThriftObject, true): Unit = {
  val bytes = serializeToBytes(thriftObject)
  //Do something with bytes
}

How to publish

  • The library is published to Maven Central. To publish, register to Sonatype and get added to Guardian projects.

  • Run sbt release. You will be asked about what version you want the release to be during the release process, you do not have to update it manually.

Node JS

  • Install using npm
npm install --save thrift-serializer

in your application or lambda you can decode messages

var Message = require('your-thrift-model');
var serializer = require('thrift-serializer');

serializer.read(Message, bytes, function (err, msg) {
	console.log(msg);
});

or encode them

var Message = require('your-thrift-model');
var serializer = require('thrift-serializer');

var message = new Message({
	someData: ''
});

serializer.write(message, serializer.Compression.Gzip, function (err, bytes) {
	// do something with your bytes, e.g. convert the buffer into a base64 string
	console.log(bytes.toString('base64'));
});

Keywords

none

Install

npm i @guardian/thrift-serializer

DownloadsWeekly Downloads

1

Version

2.1.0

License

none

Last publish

Collaborators

  • seanclarkeguardian
  • andyg421
  • rikroots
  • dskamiotis
  • georgeblahblah
  • idris-elliott-guardian
  • pete_f
  • novembertang
  • ab-gnm
  • yusuf.faraji
  • joecowton1
  • alinaboghiu.g
  • robertkenny_gu
  • jamesgorrie
  • arelra
  • marjank
  • jamieb-gu
  • zekehg
  • rtyley
  • dlawes
  • ghaberis
  • jsherbert
  • david.furey.gu
  • guardian-developers
  • simonadcock-gu
  • akash1810
  • reetta
  • ajwl
  • tomrf1
  • mchv
  • itsibitzi
  • justinpinner
  • nicl
  • philmcmahon
  • tjmw
  • thaliasilver
  • sndrs
  • sam.hession
  • 0x101
  • frankie_hammond
  • aoifemcl15
  • mxdvl
  • rhysmills
  • jacobwinch
  • annabeddow
  • michaelclapham_gnm
  • olly.namey.guardian