Git Product home page Git Product logo

lua-marshal's Introduction

Fast serialization for Lua

(forked from richardhundt/lua-marshal@fc9451f)

Provides:

  • s = marshal.encode(v[, constants]) - serializes a value to a byte stream
  • t = marshal.decode(s[, constants]) - deserializes a byte stream to a value
  • t = marshal.clone(orig[, constants]) - deep clone a value (deep for tables and functions)

Features:

Serializes tables, which may contain cycles, Lua functions with upvalues and basic data types.

All functions take an optional constants table which, if encountered during serialization, are simply referenced from the constants table passed during deserialization. For example:

local marshal = require "marshal"

local orig = { answer = 42, print = print }
local pack = marshal.encode(orig, { print })
local copy = marshal.decode(pack, { print })
assert(copy.print == print)

Installation (from source)

A simple luarocks make should suffice.

Hooks

A hook is provided for influencing serializing behaviour via the __persist metamethod. The __persist metamethod is expected to return a closure which is called during deserialization. The return value of the closure is taken as the final decoded result.

This is useful for serializing both userdata and for use with object-oriented Lua, since metatables are not serialized.

For example:

local Point = { }
function Point:new(x, y)
   self.__index = self
   return setmetatable({ x = x, y = y }, self)
end
function Point:move(x, y)
   self.x = x
   self.y = y
end
function Point:__persist()
   local x = self.x
   local y = self.y
   return function()
      -- do NOT refer to self in this scope
      return setmetatable({ x = x, y = y }, Point)
   end
end

The above shows a way to persist an "instance" of Point (if you're thinking in OO terms). In this case Point itself will be included in the encoded chunk because it's referenced as an upvalue of the returned closure.

The __persist hook may NOT refer to the receiver (i.e. self in the example) because this will cause deep recursion when upvalues are serialized.

Limitations:

Coroutines are not serialized. Userdata doesn't serialize either however support for userdata the __persist metatable hook can be used.

Metatables and function environments are not serialized.

Attempt to serialize C functions, threads and userdata without a __persist hook raises an exception.

Serialized code is not portable.

lua-marshal's People

Contributors

richardhundt avatar dabrady avatar romariorios avatar

Watchers

James Cloos avatar

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.