{"id":18521782,"url":"https://github.com/simphotonics/serialize_enum","last_synced_at":"2026-04-27T20:31:26.707Z","repository":{"id":237735921,"uuid":"795146722","full_name":"simphotonics/serialize_enum","owner":"simphotonics","description":"Serialize Dart enums without recourse to source code generation. ","archived":false,"fork":false,"pushed_at":"2025-06-12T20:13:29.000Z","size":51,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-12T21:30:37.655Z","etag":null,"topics":["dart","enum","json","serialization"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/serialize_enum","language":"Dart","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/simphotonics.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2024-05-02T17:13:35.000Z","updated_at":"2025-06-12T20:13:33.000Z","dependencies_parsed_at":"2024-11-06T17:45:55.068Z","dependency_job_id":null,"html_url":"https://github.com/simphotonics/serialize_enum","commit_stats":null,"previous_names":["simphotonics/serializable_enum","simphotonics/serialize_enum"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/simphotonics/serialize_enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fserialize_enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fserialize_enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fserialize_enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fserialize_enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simphotonics","download_url":"https://codeload.github.com/simphotonics/serialize_enum/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fserialize_enum/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32354564,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"ssl_error","status_checked_at":"2026-04-27T20:07:00.910Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":["dart","enum","json","serialization"],"created_at":"2024-11-06T17:27:36.323Z","updated_at":"2026-04-27T20:31:26.701Z","avatar_url":"https://github.com/simphotonics.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Serializing Dart Enums without Source Code Generation\n[![Dart](https://github.com/simphotonics/serialize_enum/actions/workflows/dart.yml/badge.svg)](https://github.com/simphotonics/serialize_enum/actions/workflows/dart.yml)\n\n\n## Introduction\n\nPersisting objects in Dart and Flutter typically consists in:\n1. Transforming the object into a map of type `Map\u003cString, Object?\u003e`\nusing a method called `toJson()`.\n2. Converting the map into a `String`\nusing the function [`jsonEncode`][jsonEncode].\n3. Storing the resulting Sting in a file or database.\n\nTo revive the object, the following step are usually involved:\n1. The stored string is retrieved from the database.\n2. The String is converted back into an object of type `Map\u003cString, Object?\u003e`\n   using the function [`jsonDecode`][jsonDecode].\n3. A clone of the original object is created using\n   a factory constructor usually named `.fromJson`.\n\nWriting the method `toJson()` and the factory constructor `.fromJson`\nfor a *large* number of data classes can be tedious and error prone\nand this task is often acomplished by annotating the\ndata classes and building the source code using packages like:\n[`json_serializable`][json_serializable].\n\n## Motivation\n\nDart enumerations often represent settings or options\nthat need to be persisted after exiting an app or program.\nFor small projects, however, source code generation might add too much\ncomplexity.\n\nThe package [`serialize_enum`][serialize_enum]\nprovides mixins to serialize Dart enums *without* source code\ngeneration. All that is needed is a `with` statement when declaring the\nenum and defining a `factory` constructor that calls a static method provided by\nthe mixin.\n\n## Usage\n\nInclude [`serialize_enum`][serialize_enum] as a `dev_dependency`\nin your `pubspec.yaml` file.\n\nThe example below shows the enum `AlphabeticOrder`. The generic mixin\n[SerializeByName][SerializeByName] provides the method `toJson`.\nThe typedef `Json` is pre-defined and\nrepresents the type `Map\u003cString, Object?\u003e`.\nNote that the enum factory constructor\ncalls the static method `SerializeByName.fromJson` provided by the mixin:\n\n```Dart\nimport 'package:serialize_enum/serialize_enum.dart';\n\nenum AlphabeticOrder with SerializeByName\u003cAlphabeticOrder\u003e {\n  asc,\n  desc;\n\n  /// Reads a json map and returns the corresponding\n  /// instance of `AlphabeticOrder`.\n  factory AlphabeticOrder.fromJson(Json json) =\u003e\n      SerializeByName.fromJson(json: json, values: values);\n}\n```\n\nThe generic type parameter of [SerializeByName][SerializeByName]\nmust be specified. It is used to generate the json map\n*key* under which the enum *name* is stored.\n\n```Dart\n// Code shown above goes here ...\nvoid main() {\n  const order = AlphabeticOrder.asc;\n\n  print('Json map:');\n  print(order.toJson());\n\n  print('\\nRevived enum:');\n  print(AlphabeticOrder.fromJson(order.toJson()));\n}\n```\nRunning the program produces the following console output:\n```Console\n$ dart alphabetic_order_example.dart\nJson map:\n{alphabeticOrder: asc}\n\nRevived enum:\nAlphabeticOrder.asc\n```\n\n## Further Serialization Options\n\n1. To serialize the enum instance by storing its *index* instead of its *name*\nuse the mixin [`SerializeByIndex`][SerializeByIndex].\n\n2. In order to use a *custom* key when serializing an enumeration, [`SerializeByIndex`][SerializeByIndex] or [`SerializeByName`][SerializeByName] may be implemented as an interface\n*instead* of being mixed in:\n\n   ```Dart\n\n   import 'package:serialize_enum/serialize_enum.dart';\n\n   enum AlphabeticOrder implements SerializableByName {\n     asc,\n     desc;\n\n     /// Key used to serialize the enum.\n     static const key = 'customKey';\n\n     @override\n     Json toJson() =\u003e {key: name};\n\n     /// instance of `AlphabeticOrder`.\n     factory AlphabeticOrder.fromJson(Json json) =\u003e\n         SerializableByNameCustomKey.fromJson(\n           json: json,\n           values: values,\n           key: key,\n         );\n   }\n   ```\n   Note that the factory constructor above calls the\n   static method `SerializeByNameCustomKey`.\n\n## Benchmark\n\nFor benchmark scores please visist the folder [`benchmark`][benchmark].\n\n## Features and bugs\n\nPlease file feature requests and bugs at the [issue tracker][tracker].\n\n[tracker]: https://github.com/simphotonics/serialize_enum/issues\n\n[benchmark]: https://github.com/simphotonics/serialize_enum/tree/main/benchmark\n\n[jsonEncode]: https://api.dart.dev/dart-convert/jsonEncode.html\n\n[jsonDecode]: https://api.dart.dev/dart-convert/jsonDecode.html\n\n[json_serializable]: https://pub.dev/packages/json_serializable\n\n[serialize_enum]: https://pub.dev/packages/serialize_enum\n\n[SerializableByIndex]: https://pub.dev/documentation/serialize_enum/latest/serialize_enum/SerializableByIndex-class.html\n\n[SerializableByName]: https://pub.dev/documentation/serialize_enum/latest/serialize_enum/SerializableByName-class.html\n\n[SerializeByIndex]: https://pub.dev/documentation/serialize_enum/latest/serialize_enum/SerializeByIndex-mixin.html\n\n[SerializeByName]: https://pub.dev/documentation/serialize_enum/latest/serialize_enum/SerializeByName-mixin.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimphotonics%2Fserialize_enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimphotonics%2Fserialize_enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimphotonics%2Fserialize_enum/lists"}