Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Serialization

xsc edited this page Apr 20, 2013 · 4 revisions

You can directly serialize/deserialize objects using the functions in thrift-clj.protocol.serialize. First, create a serializer using serializer with a protocol ID and options (see here for possible values):

(require '[thrift-clj.protocol.serialize :as s])
(def json (s/serializer :json))

Then, use the following functions with the serializer as first parameter:

  • value->bytes: create byte array from a Thrift value or its imported Clojure representation;
  • value-string: create string from a Thrift value or its imported Clojure representation (takes an optional charset string as its third argument);
  • bytes->value: create Thrift value (or Clojure value, if imported) using a prototype value and a byte array;
  • bytes->value: create Thrift value (or Clojure value, if imported) using a prototype value and a string (takes an optional charset string as its fourth argument).

Example:

(def p (Person. "Some" "One" 99))
(def proto (Person. nil nil nil))

(def p-bytes (s/value->bytes json p))  
;; => #<byte[] [B@30dcce73>

(def p-string (s/value->string json p) 
;; => "{\\"1\\":{\\"str\\":\\"Some\\"},\\"2\\":{\\"str\\":\\"One\\"},\\"3\\":{\\"i8\\":99}}"

(s/bytes->value json proto p-bytes)
;; => #ns_1071852349.Person{:firstName "Some", :lastName "One", :age 99}

(s/string->value json proto p-string)
;; => #ns_1071852349.Person{:firstName "Some", :lastName "One", :age 99}
Clone this wiki locally