Giter Site home page Giter Site logo

lua-serializer's Introduction

lua-serializer

A serializer for lua. Translates lua tables into custom binary data. Not human readable.

Usage

Using the serializer is very simple! Here is an example:

local serializer = require 'serializer'

local data = {
  hello = "world" -- you can store data as if it were a normal lua table!
  1, 2, 3, 4, 5, -- mixed table-arrays are allowed
  [true] = "you can index booleans too, yes",
  ["is_this_valid?"] = true
}

local binary_data = serializer.encode(data)
--> string of binary data!

local recovered_data = serializer.decode(binary_data)
--> your data is back from the bits!

print(recovered_data.hello) -- outputs "world"

But there are some rules:

  1. You cannot index the types: function, table, userdata, thread
  2. You cannot store the types: function, userdata, thread
  3. Your floats might lose precision! Non-integer values are stored using fixed point numbers. Furthermore, to implement this, I had to limit the amount of bytes used per number data to 4 bytes. So integers can go from -65536 to 65535, and non-integers can only go from -32768 to 32767. Since lua prior to v5.3 treats all numbers as float, I separate float from integer by checking them against C's FLOAT_EPSILON value.
  4. There is protection against circular tables! If you're trying to serialize a circular table, it will throw an ugly error at you. You dirty dirty circular table user. Were you trying to make my code loop infinitely?
  5. You can forget about your metatables. I completely ignore them. If you use __index shenanigans it will get completely messed up results on you.
  6. It's efficient enough. Probably.

MIT LICENSE

Feel free to use it for your program or whatever. Issues are welcome.

lua-serializer's People

Contributors

aki-cat avatar

Stargazers

 avatar

Watchers

 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.