{"id":15008100,"url":"https://github.com/davidsheh/luaserialization","last_synced_at":"2025-10-30T12:31:29.402Z","repository":{"id":256172995,"uuid":"854460682","full_name":"DavidSheh/LuaSerialization","owner":"DavidSheh","description":"一种易于使用的轻量级的序列化工具，用于将 C# 对象序列化成 Lua 代码。  An easy-to-use, lightweight serialization tool for converting C# objects into Lua code.","archived":false,"fork":false,"pushed_at":"2024-09-10T03:50:37.000Z","size":9,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-02-02T08:32:15.437Z","etag":null,"topics":["csharp","game-development","lua","serialization","unity","unity3d"],"latest_commit_sha":null,"homepage":"https://codebug.top/post/lua-serialization/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DavidSheh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-09-09T08:03:37.000Z","updated_at":"2024-09-11T00:51:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"423d9496-54a4-461d-96d4-403c69b69cce","html_url":"https://github.com/DavidSheh/LuaSerialization","commit_stats":null,"previous_names":["davidsheh/luaserialization"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidSheh%2FLuaSerialization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidSheh%2FLuaSerialization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidSheh%2FLuaSerialization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidSheh%2FLuaSerialization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DavidSheh","download_url":"https://codeload.github.com/DavidSheh/LuaSerialization/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238968295,"owners_count":19560586,"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":["csharp","game-development","lua","serialization","unity","unity3d"],"created_at":"2024-09-24T19:15:09.235Z","updated_at":"2025-10-30T12:31:24.128Z","avatar_url":"https://github.com/DavidSheh.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003cp\u003e\u003cstrong\u003e\u003ca href=\"README.md\"\u003eEnglish\u003c/a\u003e | \u003ca href=\"https://davidsheh.github.io/post/lua-serialization/\"\u003e简体中文\u003c/a\u003e\u003c/strong\u003e\u003c/p\u003e\n\u003c/div\u003e\n\n---\n\n# Lua Serialization\n[Lua Serialization](https://github.com/DavidSheh/LuaSerialization) is a lightweight serialization tool for converting C# objects into Lua code. It helps you save instance objects in C# as configuration files in Lua code format, making them easy to use in a Lua environment. This is particularly useful for game development workflows involving Unity3D + Lua.\n\n## Features\n1. Supports serializing C# objects into Lua code.\n2. Supports exporting various data types, including primitive types (int, float, double, bool, string, enum, array), as well as complex types like List and Dictionary.\n3. Supports ignoring specific fields during export.\n4. Supports preprocessing before serialization, such as initializing certain fields before export.\n5. Supports exporting native Lua code (like Lua functions and custom enums).\n6. Customizable export formats.\n\n## Instructions\n\n### Basic Usage\n\nData class definition:\n```csharp\npublic class People\n{\n    public int id;\n    public string name;\n    public int age;\n    public float weight;\n    public List\u003cint\u003e luckyNumbers;\n    public Dictionary\u003cint, string\u003e luckyNumberMap;\n    [IgnoreLua] public string description;\n}\n```\n\nSerialization:\n```csharp\nPeople people = new People()\n{\n    id = 10001,\n    name = \"Lua\",\n    age = 18,\n    weight = 60.5f,\n    luckyNumbers = new List\u003cint\u003e() { 2, 5, 6, 8, 9 },\n    luckyNumberMap = new Dictionary\u003cint, string\u003e()\n    {\n        { 2, \"Good things come in pairs\" },\n        { 5, \"Five blessings arrive\" },\n        { 6, \"Smooth sailing\" },\n        { 8, \"Wealth from all directions\" },\n        { 9, \"Longevity\" },\n    },\n    description = \"Field that is marked as not serialized.\",\n};\n\nstring strLua = LuaSerializer.Serialize(people);\nConsole.WriteLine(strLua);\n```\n\nOutput:\n```lua\n{\n    [\"id\"]=10001,\n    [\"name\"]=\"Lua\",\n    [\"age\"]=18,\n    [\"weight\"]=60.5,\n    [\"luckyNumbers\"]={\n        2,\n        5,\n        6,\n        8,\n        9\n    },\n    [\"luckyNumberMap\"]={\n        [2]=\"Good things come in pairs\",\n        [5]=\"Five blessings arrive\",\n        [6]=\"Smooth sailing\",\n        [8]=\"Wealth from all directions\",\n        [9]=\"Longevity\"\n    },\n}\n```\n\n### Advanced Usage\n#### 1. Preprocessing Before Serialization\nImplement the `IBeforeLuaSerialization` interface and add preprocessing logic in the `OnBeforeLuaSerialize()` method.\n\nData class definition:\n```csharp\npublic class People : IBeforeLuaSerialization\n{\n    public int id;\n    public string name;\n    public int age;\n    public float weight;\n    private string brief;\n\n    public void OnBeforeLuaSerialize()\n    {\n        brief = $\"id: {id}, name: {name}, age: {age}, weight: {weight}\";\n    }\n}\n```\n\nSerialization:\n```csharp\nPeople people = new People()\n{\n    id = 10002,\n    name = \"Lucky\",\n    age = 18,\n    weight = 60.5f,\n};\nstring strLua = LuaSerializer.Serialize(people);\nConsole.WriteLine(strLua);\n```\n\nOutput:\n```lua\n{\n    [\"id\"]=10002,\n    [\"name\"]=\"Lucky\",\n    [\"age\"]=18,\n    [\"weight\"]=60.5,\n    [\"brief\"]=\"id: 10002, name: Lucky, age: 18, weight: 60.5\"\n}\n```\n\n#### 2. Exporting Native Lua Code\nFor fields that need to export native Lua code, simply prefix the string with `@`.\n\nData class definition:\n```csharp\npublic class People\n{\n    public int id;\n    public string name;\n    public string gender;\n    public string description;\n}\n```\n\nSerialization:\n```csharp\nPeople people = new People()\n{\n    id = 10003,\n    name = \"Lucky\",\n    gender = \"@GenderType.Male\",\n    description = \"@function() print('I am a student.') end\",\n};\nstring strLua = LuaSerializer.Serialize(people);\nConsole.WriteLine(strLua);\n```\n\nOutput:\n```lua\n{\n    [\"id\"]=10003,\n    [\"name\"]=\"Lucky\",\n    [\"gender\"]=GenderType.Male,\n    [\"description\"]=function() print('I am a student.') end\n}\n```\n\n#### 3. Custom Export Format\nImplement the `ILuaSerializable` interface and define custom export logic in the `SerializeToLua()` method.\n\nData class definition:\n```csharp\npublic class People\n{\n    public int id;\n    public string name;\n    public int age;\n    public Child[] children;\n}\n\npublic class Child : ILuaSerializable\n{\n    public string name;\n    public int age;\n    \n    public object SerializeToLua()\n    {\n        return $\"[name = '{name}', age = {age}]\";\n    }\n}\n```\n\nSerialization:\n```csharp\nPeople people = new People()\n{\n    id = 10004,\n    name = \"Lucy\",\n    age = 32,\n    children = new Child[]{\n        new Child(){\n            name = \"Jack\",\n            age = 1\n        },\n        new Child(){\n            name = \"Nancy\",\n            age = 2\n        }\n    }\n};\nstring strLua = LuaSerializer.Serialize(people);\nConsole.WriteLine(strLua);\n```\n\nOutput:\n```lua\n{\n    [\"id\"]=10004,\n    [\"name\"]=\"Lucy\",\n    [\"age\"]=32,\n    [\"children\"]={\n        \"[name = 'Jack', age = 1]\",\n        \"[name = 'Nancy', age = 2]\"\n    }\n}\n```\n\n## Repository\nA lightweight C# to Lua serialization tool with no third-party dependencies [Lua Serialization](https://github.com/DavidSheh/LuaSerialization): https://github.com/DavidSheh/LuaSerialization.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidsheh%2Fluaserialization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidsheh%2Fluaserialization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidsheh%2Fluaserialization/lists"}