{"id":13629514,"url":"https://github.com/Terria-K/TeuJson","last_synced_at":"2025-04-17T09:34:27.908Z","repository":{"id":153744476,"uuid":"610346967","full_name":"Terria-K/TeuJson","owner":"Terria-K","description":"A Reflection-less and Lightweight Json Library using source generator.","archived":false,"fork":false,"pushed_at":"2024-02-02T09:00:24.000Z","size":138,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-08-01T22:43:49.985Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","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/Terria-K.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-03-06T15:36:11.000Z","updated_at":"2023-05-27T00:55:19.000Z","dependencies_parsed_at":"2023-09-03T09:00:33.799Z","dependency_job_id":null,"html_url":"https://github.com/Terria-K/TeuJson","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/Terria-K%2FTeuJson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Terria-K%2FTeuJson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Terria-K%2FTeuJson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Terria-K%2FTeuJson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Terria-K","download_url":"https://codeload.github.com/Terria-K/TeuJson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751197,"owners_count":17196588,"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":[],"created_at":"2024-08-01T22:01:12.513Z","updated_at":"2024-11-08T20:31:07.778Z","avatar_url":"https://github.com/Terria-K.png","language":"C#","funding_links":[],"categories":["Contributors Welcome for those","Source Generators"],"sub_categories":["1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category","Serialization"],"readme":"# TeuJson\nA Reflection-less and Lightweight Json Library using source generator.\n\n[![Nuget](https://img.shields.io/nuget/v/TeuJson?style=for-the-badge)](https://www.nuget.org/packages/TeuJson/)\n\n### Installation\nInstall these two required packages.\n\n```console\ndotnet add package TeuJson --version 3.1.2\ndotnet add package TeuJson.Generator --version 3.1.2\n```\n\n## Features\n\n- Serializers and deserializers.\n- 2 formatting options (Pretty, and Minimal).\n- Custom converters defined as functions.\n- Allows trailing commas.\n- Read and write as binary.\n\n# Usage\n\n## Creating a class with a Serializable.\n\n```C#\n// Unlike most libraries, TeuJson uses interfaces instead of an attribute on a type.\n// This is much cleaner way to specify the type if it can serialized or deserialized.\npublic partial class Person : IDeserialize, ISerialize\n{\n    [Name(\"name\")]\n    public string Name { get; set; }\n    public int Age { get; set; }\n    [Ignore]\n    public string Location { get; set; }\n    [TeuObject]\n    public string City;\n}\n\n// Then use it like this:\nvar person = new Person { \n  Name = \"John Anthony\",\n  Age = 32,\n  Location = \"North Pole\",\n  City = \"Santa's City\"\n};\nvar serialized = JsonConvert.Serialize(person);\nJsonTextWriter.WriteToFile(\"person.json\", person);\n\nvar johnJson = JsonTextReader.FromFile(\"person.json\");\nvar john = JsonConvert.Deserialize\u003cPerson\u003e(johnJson);\n```\n\nThe output of the file will be:\n\n```json\n{\n  \"name\": \"John Anthony\",\n  \"Age\": 32,\n  \"City\": \"Santa's City\"\n}\n```\n\n###  What it generates?\nIt generates the code like what you've expected, there might be a special cases with classes since they can have null values. All of Serializable classes will use a fully qualified name to instantiate themselves. \n\nThe reason why the methods are `virtual` is because the class might have a derived class which can also be a serializable to override those methods. If you don't want this, mark the `class` as `sealed`. Structs do not have inheritance, so it won't have a `virtual` method.\n\n```c#\n// Source Generated code\nusing TeuJson;\n\npartial class Person\n{\n    public virtual void Deserialize(JsonObject @__obj)\n    {\n        Name = @__obj[\"name\"];\n        Age = @__obj[\"Age\"];\n        City = @__obj[\"City\"];\n    }\n}\n\npartial class Rect4\n{\n    public virtual JsonObject Serialize()\n    {\n        var __builder = new JsonObject();\n        __builder[\"name\"] = Name;\n        __builder[\"Age\"] = Age;\n        __builder[\"City\"] = City;\n        return __builder;\n    }\n}\n```\n\n## Custom Converters\n\nYou can create your own converters by defining a function inside of a static class. You will not necessarily needed if you have an access to that specific class or struct.\n\nThe way to declare it is differs from NET 6 and NET 7.\n```C#\n// Creating the converter\n/* MyMathConverter.cs */\nnamespace Maths;\n\n/* NET 6.0 and below */\n/** public class MyMathConverter **/\npublic class MyMathConverter \n{\n// Converters are named sensitive, it must follow the naming convetion in order to work.\n// Writer = JsonValue ToJson(this \u003cT\u003e value);\n// Reader = \u003cT\u003e To\u003cT\u003e(this JsonValue value);\n\n    // Extensions are possible in NET 6, but not in NET 7 due to its limitation.\n    public static JsonValue ToJson(Vector2 value) \n    {\n        // Json object is similar to Dictionary.\n        return new JsonObject \n        {\n            [\"x\"] = value.X,\n            [\"y\"] = value.Y\n        };\n    }\n\n    public static Vector2 ToVector2(JsonValue value) \n    {\n        // check if the json value is object\n        if (value.IsObject) \n        {\n            int x = value[\"x\"];\n            int y = value[\"y\"];\n            return new Vector2(x, y);\n        }\n        return Vector2.Zero;\n    }\n}\n```\n\n```C#\npublic sealed partial class Player : IDeserialize\n{\n    [Name(\"name\")]\n    public string Name { get; set; }\n    /* NET 6 and below: Name must be fully qualified*/\n    /** Name must be fully qualified **/\n    /** [Custom(\"Maths.MyMathConverter\")] **/\n    [Custom\u003cMyMathConverter\u003e()]\n    [Name(\"position\")]\n    public Vector2 Position { get; set;}\n}\n```\n\n# License\n\nMIT License ([Read License](LICENSE.txt)).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTerria-K%2FTeuJson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTerria-K%2FTeuJson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTerria-K%2FTeuJson/lists"}