{"id":19599736,"url":"https://github.com/starwing/lua-mp","last_synced_at":"2025-04-27T16:32:08.566Z","repository":{"id":137297231,"uuid":"261276200","full_name":"starwing/lua-mp","owner":"starwing","description":"yet another msgpack implement for Lua","archived":false,"fork":false,"pushed_at":"2024-07-27T08:21:37.000Z","size":54,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T01:32:09.439Z","etag":null,"topics":["lua","msgpack"],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starwing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-04T19:26:17.000Z","updated_at":"2024-11-04T06:30:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"a9c83da9-79ba-418d-bde1-80cffdcc653d","html_url":"https://github.com/starwing/lua-mp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flua-mp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flua-mp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flua-mp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flua-mp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starwing","download_url":"https://codeload.github.com/starwing/lua-mp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251171449,"owners_count":21547112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","msgpack"],"created_at":"2024-11-11T09:12:17.236Z","updated_at":"2025-04-27T16:32:08.318Z","avatar_url":"https://github.com/starwing.png","language":"Lua","readme":"# lua-mp - Yet another msgpack implement for Lua\n\n[![Build Status](https://travis-ci.org/starwing/lua-mp.svg?branch=master)](https://travis-ci.org/starwing/lua-mp)[![Coverage Status](https://coveralls.io/repos/github/starwing/lua-mp/badge.svg?branch=master)](https://coveralls.io/github/starwing/lua-mp?branch=master)\n\n\n\nLua-mp is a simple C module for Lua to manipulate msgpack encoding/decoding.\n\n## Install\n\nThe simplest way to install it is using Luarocks:\n\n```shell\nluarocks install --server=https://luarocks.org/dev lua-mp\n```\n\nOr, just compile the single C file:\n\n```shell\n# Linux\ngcc -o mp.so -O3 -fPIC -shared mp.c\n# macOS\ngcc -o mp.so -O3 -fPIC -shared -undefined dynamic_lookup mp.c\n# Windows\ncl /Fe mp.dll /LD /MT /O2 /DLUA_BUILD_AS_DLL /Ipath/to/lua/include mp.c path/to/lua/lib\n```\n\n# Example\n\n```lua\nlocal mp = require \"mp\"\nlocal serpent = require \"serpent\" -- just for print table\n\nlocal bytes = mp.encode {\n  name = \"alice\",\n  phone_list = { \"1311234\", \"1310024\" }\n}\nprint(mp.tohex(bytes))\n\nlocal t = mp.decode(bytes)\nprint(serpent.block(t))\n```\n\n ## Document\n\n| Routine                                   | Returns          | Description                                                  |\n| ----------------------------------------- | ---------------- | ------------------------------------------------------------ |\n| `decode(string[, i[, j[, ext_handler]]])` | `object`,  `pos` | decode a msgpack string into object, returns the decoded object and unread position |\n| `encode(...)`                             | `string`         | Accept any values and create a msgpack encoding string       |\n| `null`                                    | `-`              | the `null` object can be used in array/map                   |\n| `array(...)`                              | ...              | mark all it's arguments to array                             |\n| `map(...)`                                | ...              | mark all it's arguments to map                               |\n| `meta(type, ...)`                         | ...              | mark all it's arguments to `type`                            |\n| `tohex(string)`                           | `string`         | convert a binary string into hexadigit mode for human reading |\n| `fromhex(string)`                         | `string`         | convert hexadigit back into binary string                    |\n\n### Decoding mapping\n\n| Msgpack Type                        | Lua Type                            |\n| ----------------------------------- | ----------------------------------- |\n| `null`                              | `nil`                               |\n| `true`, `false`                     | `boolean`                           |\n| `int`, `uint`, `float32`, `float64` | `number`                            |\n| `bin`, `string`                     | `string`                            |\n| `ext`                               | `{ type = number, value = string }` |\n\n`decode()` accepts a string, and optional two pos `i` and `j` (based on 1), and a optionl `env_handler` function  for arguments. if there is `ext` type in `msgpack` string, the ext_handler will be called with `type` and `value` and returns a object for use. if the handler returns nothing, the default type/value table will be used.\n\nIt returns the decoded object, and if there is any remain bytes in string, returns the current reading position for next reading. So you can iterate all object in a string using a loop:\n\n```lua\nlocal pos = 1\nlocal values = {}\nwhile pos do\n  values[#values+1], pos = mp.decode(data, pos)\nend\n```\n\n### Encoding Mapping\n\n| Lua Type   | Msgpack Type                                                 |\n| ---------- | ------------------------------------------------------------ |\n| `nil`      | `null`                                                       |\n| `boolean`  | `true`, `false`                                              |\n| `number`   | `int`, `float32`, `float64`                                  |\n| `string`   | `string`                                                     |\n| `function` | A [handler function](#handler-function) returns the actually values to encode |\n| `table`    | `array` or `map` or any other (using `meta` types)           |\n\n### Meta types\n\nmeta type is a table, that has a metatable, which has a field `\"msgpack.type\"` with below values:\n\n| `\"msgpack.type\"` | `\"Description\"` |\n| ---------------- | --------------- |\n| `\"null\"` | encode a `null` |\n| `\"True\"`, `\"False\"` | encode a `true` or `false` |\n| `\"int\"`, `\"uint\"`, `\"float\"`, `\"double\"` | reads the `value` field of table, and encode a number value |\n| `\"string\"`, `\"binary\"` | same as above, but encoding a `string` or `binary` value |\n| `\"value\"` | same as above, but encode using the Lua type of value field |\n| `\"handler\"` | reads the `pack` field as a function, call with table itself, and returns the actually values to encode |\n| `\"array\"` | treat table as a array |\n| `\"map\"` | treat table as a map |\n| `\"extension\"` | reads the `type` and `value` fields in table and encode a `ext` value |\n\nmeta value can get by `mp.meta()` or `mp.array()`, `mp.map()` routines:\n\n```lua\n-- get a meta value of uint:\nlocal uint = mp.meta(\"uint\", 123)\n-- get a array:\nlocal array = mp.array { 1,2,3,4 }\n-- set all tables to map:\nmp.map(map1, map2, map3)\n```\n\n### Handler function\n\nA `handler` should first returns a `\"msgpack.type\"` string, and if needed, return the actual value to be encoded. for `\"entension\"` type, `handler` should returns both `type` and `value`:\n\n```lua\nlocal function handler_to_get_a_uint() return \"uint\", 123 end\nlocal function handler_to_get_a_ext() return \"extension\", 1, \"foo\" end\nlocal function handler_to_get_a_true() return \"True\" end\nlocal function handler_to_another_handler() return \"handler\", { pack = handler } end\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarwing%2Flua-mp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarwing%2Flua-mp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarwing%2Flua-mp/lists"}