twitter / chill
1
/*
2

3
Licensed under the Apache License, Version 2.0 (the "License");
4
you may not use this file except in compliance with the License.
5
You may obtain a copy of the License at
6

7
http://www.apache.org/licenses/LICENSE-2.0
8

9
Unless required by applicable law or agreed to in writing, software
10
distributed under the License is distributed on an "AS IS" BASIS,
11
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
See the License for the specific language governing permissions and
13
limitations under the License.
14
 */
15
package com.twitter.chill.avro
16

17
import com.twitter.bijection.Injection
18
import com.twitter.bijection.avro.{GenericAvroCodecs, SpecificAvroCodecs}
19
import com.twitter.chill.{InjectiveSerializer, KSerializer}
20
import org.apache.avro.Schema
21
import org.apache.avro.generic.GenericRecord
22
import org.apache.avro.specific.SpecificRecordBase
23

24
import scala.reflect.ClassTag
25

26
/**
27
 * @author Mansur Ashraf
28
 * @since 2/9/14.
29
 */
30
object AvroSerializer {
31
  def SpecificRecordSerializer[T <: SpecificRecordBase: ClassTag]: KSerializer[T] = {
32 1
    implicit val inj: Injection[T, Array[Byte]] = SpecificAvroCodecs[T]
33 1
    InjectiveSerializer.asKryo
34
  }
35

36
  def SpecificRecordBinarySerializer[T <: SpecificRecordBase: ClassTag]: KSerializer[T] = {
37 1
    implicit val inj: Injection[T, Array[Byte]] = SpecificAvroCodecs.toBinary[T]
38 1
    InjectiveSerializer.asKryo
39
  }
40

41
  def SpecificRecordJsonSerializer[T <: SpecificRecordBase: ClassTag](schema: Schema): KSerializer[T] = {
42
    import com.twitter.bijection.StringCodec.utf8
43 1
    implicit val inj: Injection[T, String] = SpecificAvroCodecs.toJson[T](schema)
44 1
    implicit val avroToArray: Injection[T, Array[Byte]] = Injection.connect[T, String, Array[Byte]]
45 1
    InjectiveSerializer.asKryo
46
  }
47

48
  def GenericRecordSerializer[T <: GenericRecord: ClassTag](schema: Schema = null): KSerializer[T] = {
49 1
    implicit val inj: Injection[T, Array[Byte]] = GenericAvroCodecs[T](schema)
50 1
    InjectiveSerializer.asKryo
51
  }
52
}

Read our documentation on viewing source code .

Loading