Giter Site home page Giter Site logo

borsh-clj's Introduction

borsh-clj Test Clojars Project

A pure Clojure/Script implementation for Borsh, the binary serializer for security-critical projects.

Examples

(require '[borsh.macros :as m]
         '[borsh.core :as borsh])

;; Define schemas
(m/defstruct Point
    [^:u32 x
     ^:u32 y])

(m/defstruct Rect
    [^{:struct Point} p1
     ^{:struct Point} p2])

(def r (->Rect
        (->Point 0 0)
        (->Point 42 42)))

;; Serialize an object
(def bs (borsh/serialize r))

;; Deserialize a byte array
(def r1 (borsh/deserialize ->Rect bs))

Type Mappings

Borsh Clojure Meta
boolean Boolean ^:bool
u8 integer Long or Number ^:u8
u16 integer Long or Number ^:u16
u32 integer Long or Number ^:u32
u64 integer Long or BigInt ^:u64
usize integer Long or Number ^:usize
UTF-8 String String ^:string
Option nil or type ^{:option type}
Vec Vector ^{:vec item-type}
Map HashMap ^{:map [key-type val-type]}
Struct Record ^{:struct record}
Simple enum Keyword ^{:enum [kws]}
Enum of structs Record ^{:enum variants}
Dynamic-sized byte array Byte[] or Uint8Array ^:bytes
Extended type ^{:ext ext}

Enums

Simple enums can be defined with a vector of keywords

(require '[borsh.macros :as m])

(m/defstruct HasStatus
  [^{:enum [:status/a :status/b]} status])

;; Another approach
(def status-enums [:status/a :status/b])
(m/defstruct HasStatus
  [^{:enum status-enums} status])

Complicated enums can be defined with borsh.macro/defvariants.

(require '[borsh.macros :as m])

(m/defstruct Point [^:u64 x ^:u64 y])
(m/defstruct Line [^{:struct Point} p1 ^{:struct Point} p2])

(m/defvariants shapes [Point Line])
(m/defstruct HasVariants
  [^{:enum shapes} shape])

Extended Types

To extend a custom type, use IExtendWriter and IExtendReader.

(require '[borsh.ext :as ext]
         '[borsh.types :as t]
         '[borsh.macros :as m])

(defrecord DateExt [])

(extend-type DateExt
  ext/IExtendWriter
  (write [this buf value] ;; the type of value is java.util.Date
    (t/write-u64 buf (.getTime value)))
  ext/IExtendReader
  (read [this buf]        ;; return java.util.Date
    (java.util.Date. (t/read-u64 buf))))

(def date-ext (->DateExt))

(m/defstruct X [^{:ext date-ext} date])

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.