{"id":37469137,"url":"https://github.com/simulation-tree/serialization","last_synced_at":"2026-01-16T07:13:31.206Z","repository":{"id":292693322,"uuid":"806620496","full_name":"simulation-tree/serialization","owner":"simulation-tree","description":"Contains common serialization formats","archived":false,"fork":false,"pushed_at":"2025-07-19T14:46:34.000Z","size":156,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-19T18:36:35.519Z","etag":null,"topics":["csharp","dotnet","json","json5","nativeaot","xml"],"latest_commit_sha":null,"homepage":"","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/simulation-tree.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,"zenodo":null}},"created_at":"2024-05-27T14:48:39.000Z","updated_at":"2025-06-28T15:06:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"24c22761-4f65-49dc-b7e9-ba4e0a848cca","html_url":"https://github.com/simulation-tree/serialization","commit_stats":null,"previous_names":["simulation-tree/serialization"],"tags_count":45,"template":false,"template_full_name":null,"purl":"pkg:github/simulation-tree/serialization","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simulation-tree%2Fserialization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simulation-tree%2Fserialization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simulation-tree%2Fserialization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simulation-tree%2Fserialization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simulation-tree","download_url":"https://codeload.github.com/simulation-tree/serialization/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simulation-tree%2Fserialization/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28478046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T06:30:42.265Z","status":"ssl_error","status_checked_at":"2026-01-16T06:30:16.248Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","dotnet","json","json5","nativeaot","xml"],"created_at":"2026-01-16T07:13:31.130Z","updated_at":"2026-01-16T07:13:31.199Z","avatar_url":"https://github.com/simulation-tree.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Archive notice\n\nThis repository is archived, in favor of unique repositories per serialization\nformat.\n\n# Serialization\n\n[![Test](https://github.com/simulation-tree/serialization/actions/workflows/test.yml/badge.svg)](https://github.com/simulation-tree/serialization/actions/workflows/test.yml)\n\nNative library for working with common human readable formats using readers and writers.\nWith high-level types available for representing objects.\n\n### Supported formats\n\n- JSON\n- JSON 5 (named after ECMAScript 5)\n- XML\n\n### JSON reader and writer\n\nThe reader and writers are low-level concepts used to traverse and write data:\n```cs\nusing JSONWriter writer = new();\nwriter.WriteStartObject();\nwriter.WriteName(\"name\");\nwriter.WriteText(\"John Doe\");\nwriter.WriteEndObject();\n\nusing JSONReader reader = new(writer.GetBytes());\nreader.ReadStartObject();\nreader.ReadToken();\nSpan\u003cchar\u003e nameBuffer = stackalloc char[32];\nint nameLength = reader.ReadText(nameBuffer);\nreader.ReadEndObject();\nAssert.That(nameBuffer[..nameLength].ToString(), Is.EqualTo(\"John Doe\"));\n```\n\n### Generic JSON object\n\nThis is the high-level type that represents a JSON object without the need\nfor interacting with the reader/writer types:\n```cs\nJSONObject fruit = new();\nfruit.Add(\"name\", \"cherry\");\nfruit.Add(\"color\", \"red\");\n\nJSONArray inventory = new();\ninventory.Add(\"apples\");\ninventory.Add(\"oranges\");\ninventory.Add(fruit);\n\nusing JSONObject jsonObject = new();\njsonObject.Add(\"name\", \"John Doe\");\njsonObject.Add(\"age\", 42);\njsonObject.Add(\"alive\", true);\njsonObject.Add(\"inventory\", inventory);\n\njsonObject[\"age\"].Number++;\n\nusing Text jsonText = new();\nSerializationSettings settings = SerializationSettings.PrettyPrint;\njsonObject.ToString(jsonText, settings);\nConsole.WriteLine(jsonText);\n```\n\nOutput:\n```json\n{\n    \"name\": \"John Doe\",\n    \"age\": 43,\n    \"alive\": true,\n    \"inventory\": [\n        \"apples\",\n        \"oranges\",\n        {\n            \"name\": \"cherry\",\n            \"color\": \"red\"\n        }\n    ]\n}\n```\n\n### JSON 5 support\n\nThe reading mechanism supports both old and new JSON formats. But for the writer,\nsome settings need to be adjusted:\n```cs\nSerializationSettings settings = new();\nsettings.flags |= SerializationFlags.QuotelessNames;\nsettings.flags |= SerializationFlags.SingleQuotedText;\n```\n\nThe shorthand for these settings is `SerializationSettings.JSON5` and `SerializationSettings.JSON5PrettyPrint`.\n\n### JSON to C# and back\n\nThe readers and writers have API for serializing/deserializing `IJSONObject` values:\n```cs\npublic struct Player : IJSONObject, IDisposable\n{\n    public int hp;\n    public bool alive;\n\n    private Text name;\n\n    public readonly Span\u003cchar\u003e Name\n    {\n        get =\u003e name.AsSpan();\n        set =\u003e name.CopyFrom(value);\n    }\n\n    public Player(int hp, bool alive, ReadOnlySpan\u003cchar\u003e name)\n    {\n        this.hp = hp;\n        this.alive = alive;\n        this.name = new(name);\n    }\n\n    public void Dispose()\n    {\n        name.Dispose();\n    }\n\n    void IJSONObject.Read(ref JSONReader reader)\n    {\n        //read hp\n        reader.ReadToken();\n        hp = (int)reader.ReadNumber(out _);\n\n        //read alive\n        reader.ReadToken();\n        alive = reader.ReadBoolean(out _);\n\n        //read name\n        reader.ReadToken();\n        Span\u003cchar\u003e nameBuffer = stackalloc char[32];\n        int nameLength = reader.ReadText(nameBuffer);\n        name = new(nameBuffer.Slice(0, nameLength));\n    }\n\n    void IJSONObject.Write(JSONWriter writer)\n    {\n        writer.WriteProperty(nameof(hp), hp);\n        writer.WriteProperty(nameof(alive), alive);\n        writer.WriteProperty(nameof(name), name.AsSpan());\n    }\n}\n\nbyte[] jsonBytes = File.ReadAllBytes(\"player.json\");\nusing ByteReader reader = new(jsonBytes);\nJSONReader jsonReader = new(reader);\nusing Player player = jsonReader.ReadObject\u003cPlayer\u003e();\nReadOnlySpan\u003cchar\u003e name = player.Name;\n```\n\n### XML\n\nXML is supported through the `XMLNode` type, which can be created from either a byte or a char array.\nEach node has a name, content, and a list of children. Attributes can be read using the indexer.\n```cs\nbyte[] xmlData = File.ReadAllBytes(\"solution.csproj\");\nusing XMLNode project = new(xmlData);\nXMLAttribute sdk = project[\"Sdk\"];\nsdk.Value = \"Simulation.NET.Sdk\";\nproject.TryGetFirst(\"PropertyGroup\", out XMLNode propertyGroup);\nproject.TryGetFirst(\"TargetFramework\", out XMLNode tfm);\ntfm.Content = \"net9.0\";\nFile.WriteAllText(\"solution.csproj\", project.ToString());\n```\n\n### Contributing and design\n\nAlthough the name of the library is `serialization`, it's not to solve serialization itself.\nBut instead, for providing implementations of common and easy to read/edit formats very efficiently.\n\nAnd despite \"common\" being difficult to define, contributions to this are welcome.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimulation-tree%2Fserialization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimulation-tree%2Fserialization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimulation-tree%2Fserialization/lists"}