background preloader

Serializers Tools

Facebook Twitter

Convert to Binary stream tools

How to use serialization in Scala (Serializable trait) By Alvin Alexander.

How to use serialization in Scala (Serializable trait)

Last updated: Jun 19, 2015 This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.8, “How to use serialization in Scala (Serializable trait).” Problem You want to serialize a Scala class and save it as a file, or send it across a network. Solution The general approach is the same as Java, but the syntax to make a class serializable is different. To make a Scala class serializable, extend the Serializable trait and add the @SerialVersionUID annotation to the class: @SerialVersionUID(100L) class Stock(var symbol: String, var price: BigDecimal) extends Serializable { // code here ... } Because Serializable is a trait, you can mix it into a class, even if your class already extends another class: @SerialVersionUID(114L) class Employee extends Person with Serializable ...

Discussion The following code demonstrates the proper approach. This code prints the following output when run: NFLX is 85.00 See Also. Hygienic Closures for Scala Function Serialization - tool monkey. In most use cases of Scala closures, what you see is what you get, but there are exceptions where looks can be deceiving and this can have a big impact on closure serialization.

Hygienic Closures for Scala Function Serialization - tool monkey

Closure serialization is of more than academic interest. Tools like Apache Spark cannot operate without serializing functions over the network. In this post I’ll describe some scenarios where closures include more than what is evident in the code, and then a technique for preventing unwanted inclusions. To establish a bit of context, consider this simple example that obtains a function and serializes it to disk, and which does behave as expected: When this app is compiled and run, it will serialize f to “/tmp/demo.f1”, which of course includes the value of v as part of the closure for f. $ scalac -d /tmp closures.scala $ scala -cp /tmp Demo $ ls /tmp/demo* /tmp/demo.f Now, imagine you wanted to make a straightforward change, where object foo becomes class foo: class foo() extends Serializable { // ... } Serialization - Scala: Reading a serialized object from a file. Serialization.

So, lets say that you want to create your own Serializer, you saw the docs.serialization.MyOwnSerializer in the config example above?

Serialization

§Creating new Serializers First you need to create a class definition of your Serializer like so: The manifest is a type hint so that the same serializer can be used for different classes. The manifest parameter in fromBinary is the class of the object that was serialized. In fromBinary you can match on the class and deserialize the bytes to different objects. Then you only need to fill in the blanks, bind it to a name in your Configuration and then list which classes that should be serialized using it.

§Serializer with String Manifest The Serializer illustrated above supports a class based manifest (type hint). The manifest string can also encode a version number that can be used in fromBinary to deserialize in different ways to migrate old data to new domain objects. Object serialization example in Scala. GitHub - mikeaddison93/pickling: Fast, customizable, boilerplate-free pickling support for Scala. GitHub - mikeaddison93/chill: Scala extensions for the Kryo serialization library. GitHub - mikeaddison93/akka-kryo-serialization: Kryo-based serialization for Akka.

Mikeaddison93/kryo: Java serialization and cloning: fast, efficient, automatic. Welcome to Apache Avro! Protobuf. "Protocol Buffers - Google's data interchange format" Camel provides a Data Format to serialse between Java and the Protocol Buffer protocol.

Protobuf

The project's site details why you may wish to choose this format over xml. Protocol Buffer is language-neutral and platform-neutral, so messages produced by your Camel routes may be consumed by other language implementations. API SiteProtobuf ImplementationProtobuf Java Tutorial Protobuf overview This quick overview of how to use Protobuf. Defining the proto format The first step is to define the format for the body of your exchange. Generating Java classes The Protobuf SDK provides a compiler which will generate the Java classes for the format we defined in our .proto file. Protoc --java_out=. . This will generate a single Java class named AddressBookProtos which contains inner classes for Person and AddressBook. Java DSL You can use create the ProtobufDataFormat instance and pass it to Camel DataFormat marshal and unmarsha API like this.

Spring DSL.