{"id":26296395,"url":"https://github.com/charliedigital/js2c","last_synced_at":"2025-09-11T11:50:50.391Z","repository":{"id":205869076,"uuid":"715375810","full_name":"CharlieDigital/js2c","owner":"CharlieDigital","description":"A .NET package using source generators to automatically create classes from a snippet of JSON.","archived":false,"fork":false,"pushed_at":"2023-11-07T03:22:13.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-15T18:49:42.089Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/js2c.generator/","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/CharlieDigital.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}},"created_at":"2023-11-07T02:32:56.000Z","updated_at":"2023-11-07T03:18:28.000Z","dependencies_parsed_at":"2023-11-07T03:15:50.710Z","dependency_job_id":"b36d2dbd-647e-4254-a97e-09e8c89fcbdf","html_url":"https://github.com/CharlieDigital/js2c","commit_stats":null,"previous_names":["charliedigital/js2c"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/CharlieDigital/js2c","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CharlieDigital%2Fjs2c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CharlieDigital%2Fjs2c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CharlieDigital%2Fjs2c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CharlieDigital%2Fjs2c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CharlieDigital","download_url":"https://codeload.github.com/CharlieDigital/js2c/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CharlieDigital%2Fjs2c/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261193310,"owners_count":23122931,"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":"2025-03-15T04:18:15.458Z","updated_at":"2025-06-23T01:05:37.928Z","avatar_url":"https://github.com/CharlieDigital.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON to Class (js2c)\n\nJSON to class (*js2c*) is a small library using .NET Source Generators to automatically create classes based on snippets of JSON.\n\nThis project was inspired by parsing JSON+LD from various websites which all use slightly different formats and different structures.\n\nHowever, it can be used with any JSON snippet.\n\nTo use:\n\n```\ndotnet add package js2c.generator\n```\n\n## Motivation\n\nIf you have a snippet of JSON and you'd like to quickly create a strongly typed class from it, js2c let's you do that quickly by simply decorating your class with an attribute and supplying a JSON snippet as a template:\n\n```cs\n// 1️⃣ A partial class that is otherwise empty\n[JsonSource(PersonJson)]\npublic partial class Person {\n  // A JSON snippet which defines a \"template\"\n  internal const string PersonJson = @\"{\n    \"\"firstName\"\" : \"\"\"\",\n    \"\"lastName\"\" : \"\"\"\",\n    \"\"age\"\": 0\n  }\";\n}\n\n// 2️⃣ The generated class:\npublic partial class Person {\n  public static Person? Parse(string json) {\n    return JsonSerializer.Deserialize\u003cPerson\u003e(json);\n  }\n  [JsonPropertyName(\"firstName\")] public string FirstName { get; set;} = \"\";\n  [JsonPropertyName(\"lastName\")] public string LastName { get; set;} = \"\";\n  [JsonPropertyName(\"age\")] public decimal Age { get; set; }\n}\n\n// 3️⃣ Which you can use like this\nvar person = new Person {\n  FirstName = \"Stanley\",\n  LastName = \"Banks\",\n  Age = 30m\n}\n\n// Or like this:\nvar adam = Person.Parse(\"{ \\\"firstName\\\": \\\"Adam\\\", \\\"lastName\\\": \\\"Ng\\\" }\");\n```\n\njs2c simplifies defining classes from JSON by allowing you to simply paste a template into your source.\n\nScenarios where you might find this useful:\n\n- Web scraping `__NEXT_DATA__` or other JSON chunks\n- Ad-hoc strongly typed API DTO classes from small snippets of JSON\n- Processing JSON event data in Lambda\n- Many more!\n\n## Basic Types\n\n- JSON Strings are mapped to `string`\n- JSON Booleans are mapped to `bool`\n- JSON Numbers are mapped to `decimal`\n- JSON Arrays are mapped to `Array\u003cT\u003e`\n- JSON Objects are mapped to `class`\n\n## Complex Types\n\njs2c handles complex types like arrays and objects (with limitations for now; feel free to contribute!):\n\n### Arrays\n\nHere is an example of using it with arrays of values:\n\n```cs\n// JSON with string array\ninternal const string PersonJson = @\"{\n  \"\"firstName\"\" : \"\"\"\",\n  \"\"lastName\"\" : \"\"\"\",\n  \"\"emails\"\": [\"\"\"\", \"\"\"\"]\n}\";\n\n// Partial class\n[JsonSource(PersonJson)]\npublic partial class Person {\n\n}\n\n// Used like this:\nvar randy = new Person3 {\n  FirstName = \"Randy\",\n  LastName = \"Lee\",\n  Emails = new [] {\n    \"randy@example.com\",\n    \"randy.lee@example.com \"\n  }\n};\n```\n\n### Objects\n\njs2c handles nested objects and will generate classes using a special attribute like `@type`:\n\n```cs\n// JSON template\ninternal const string PersonJson = @\"{\n  \"\"firstName\"\" : \"\"\"\",\n  \"\"lastName\"\" : \"\"\"\",\n  \"\"emails\"\": [\"\"\"\", \"\"\"\"],\n  \"\"address\"\": {\n    \"\"@type\"\": \"\"Address\"\",\n    \"\"street\"\": \"\"\"\",\n    \"\"city\"\": \"\"\"\",\n    \"\"state\"\": \"\"\"\"\n  }\n}\";\n\n// Partial class\n[JsonSource(PersonJson)]\npublic partial class Person {\n\n}\n\n// Generated class can use arrays and objects.\nvar patty = new Person {\n  FirstName = \"Patty\",\n  LastName = \"Lundgren\",\n  Emails = new [] { \"patty@example.com\", \"patty.Lundgren@example.com \"},\n  Address = new()  {\n    Street = \"123 Acme Ln\",\n    City = \"New York City\",\n    State = \"NY\"\n  }\n};\n```\n\n## Development\n\n### Unit Tests\n\nThe best way to work with this is to build unit tests for your use cases as you go along.\n\nFrom the command line:\n\n```shell\ncd tests\ndotnet test\n```\n\n### A Note on Caching\n\nYou may run into issues with caching while developing your own generators.  To work around this, you can use the `build-server shutdown` command to effectively restart the build server:\n\n```shell\ncd tests\n\n# Reset the build server to clear cache.\ndotnet build-server shutdown\ndotnet run\n\n# Or combine:\ndotnet build-server shutdown \u0026\u0026 dotnet run\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharliedigital%2Fjs2c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharliedigital%2Fjs2c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharliedigital%2Fjs2c/lists"}