Giter Club home page Giter Club logo

ser's Introduction

Since 2016-02-16, Ser is deprecated. I will still fix reported bugs, but for new projects, I recommend bitser if you're using LuaJIT, and binser otherwise.

Ser

Ser is a fast, robust, richly-featured table serialization library for Lua. It was specifically written to store configuration and save files for LÖVE games, but can be used anywhere.

Originally, this was the code to write save games for Space, but was released as a stand-alone library after many much-needed improvements.

Like Space itself, you use, distribute and extend Ser under the terms of the MIT license.

Simple

Ser is very simple and easy to use:

local serialize = require 'ser'

print(serialize({"Hello", world = true}))
-- prints:
-- return {"Hello", world = true}

Fast

Using Serpent's benchmark code, Ser is 33% faster than Serpent.

Robust

Sometimes you have strange, non-euclidean geometries in your table constructions. It happens, I don't judge. Ser can deal with that, where some other serialization libraries cry "Iä! Iä! Cthulhu fhtagn!" and give up — or worse, silently produce incorrect data.

local serialize = require 'ser'

local cthulhu = {{}, {}, {}}
cthulhu.fhtagn = cthulhu
cthulhu[1][cthulhu[2]] = cthulhu[3]
cthulhu[2][cthulhu[1]] = cthulhu[2]
cthulhu[3][cthulhu[3]] = cthulhu
print(serialize(cthulhu))
-- prints:
-- local _3 = {}
-- local _2 = {}
-- local _1 = {[_2] = _3}
-- local _0 = {_1, _2, _3}
-- _0.fhtagn = _0
-- _2[_1] = _2
-- _3[_3] = _0
-- return _0

Tested

Check out tests.lua to see how Ser behaves with all kinds of inputs.

Other solutions

Check out the Lua-users wiki for other libraries that do roughly the same thing.

See also

  • Lady: for trusted-source savegames
  • Smallfolk: for untrusted-source serialization

ser's People

Contributors

gvx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ser's Issues

serializing odd values

Hello,

inf and -inf values are not always "inf" and "-inf". When Lua is compiled with mingw on Windows, it's rather "1.#INF" and "-1.#INF".

oddvals should be:
local oddvals = {[tostring(1/0)] = '1/0', [tostring(-1/0)] = '-1/0', [tostring(0/0)] = '0/0'}

Best regards,
Christophe Delord.

Two types of NaN in Lua

LuaJIT 2.0 doesn't seem to disginguish inteterminate from NaN, but Lua 5.1 does. In Lua 5.1, the tostring() of indeterminate forms in my system is -nan, in others it may be different. They are distinguished by having the sign bit set.

This causes ser to not always serialize NaNs under Lua correctly (though it correctly outputs indeterminates, and it correctly works under LuaJIT 2.0 in the systems I could test).

This is the suggested replacement:

local oddvals = {[tostring(1/0)] = '1/0', [tostring(-1/0)] = '-1/0', [tostring(-(0/0))] = '-(0/0)', [tostring(0/0)] = '0/0'}

Under LuaJIT, one overwrites the other.

I tested only with:

  • Lua 5.1.5 32 bits on Debian
  • LuaJIT 2.0.3 32 bits on Debian
  • Lua 5.1.5 32 bits on Windows
  • LuaJIT embedded with LÖVE 0.9.1 32 bits on Windows.

Example (Debian 32 bits):

$ luajit
LuaJIT 2.0.3 -- Copyright (C) 2005-2014 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
> print(0/0)
nan
> print(math.acos(2))
nan
> 
$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print(0/0)
-nan
> print(math.acos(2))
nan
> 

Under Windows, Lua 5.1.5 doesn't seem to generate NaNs, only indeterminates, but it distinguishes both with tostring:

C:\Lua>lua5.1
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print(tostring(math.asin(2)))
-1.#IND
> print(tostring(0/0))
-1.#IND
> print(tostring(-(0/0)))
1.#QNAN
>

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.