{"id":19806691,"url":"https://github.com/timnew/json_native","last_synced_at":"2026-05-14T00:33:06.985Z","repository":{"id":192801469,"uuid":"687323770","full_name":"timnew/json_native","owner":"timnew","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-06T12:19:44.000Z","size":23,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-11T06:24:46.408Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/timnew.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}},"created_at":"2023-09-05T06:01:28.000Z","updated_at":"2024-11-06T10:14:53.000Z","dependencies_parsed_at":"2025-01-11T06:24:33.248Z","dependency_job_id":"bc06ad41-21e2-471c-8c9f-ab0f9d2780c5","html_url":"https://github.com/timnew/json_native","commit_stats":{"total_commits":11,"total_committers":1,"mean_commits":11.0,"dds":0.0,"last_synced_commit":"2f9a9772e4716b709ceca42c84e57c492724cce8"},"previous_names":["timnew/json_native"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fjson_native","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fjson_native/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fjson_native/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fjson_native/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timnew","download_url":"https://codeload.github.com/timnew/json_native/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241150406,"owners_count":19918351,"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-11-12T09:08:12.780Z","updated_at":"2026-05-14T00:33:01.953Z","avatar_url":"https://github.com/timnew.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json_native\n\n[![Star this Repo](https://img.shields.io/github/stars/timnew/json_native)](https://github.com/timnew/json_native)\n[![Pub Package](https://img.shields.io/pub/v/json_native)](https://pub.dev/packages/json_native)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/timnew/json_native/test.yml)](https://github.com/timnew/json_native/actions/workflows/test.yml)\n\n## Overview\n\nNeed to work with JSON in Dart? `json_serializable`` might not always be your best bet, especially when:\n\n- The JSON structure is unclear or dynamic.\n- You only need to extract a few values, making it overkill to create a full data model.\n- You're dealing with complex types that json_serializable can't handle, like union types.\n\nIn such cases, you might revert to raw `jsonDecode``, but that comes with its own set of challenges.\n\n## The Problem: using `jsonDecode`\n\nConsider the following JSON string:\n\n```json\n{\n  \"int\": 1,\n  \"string\": \"string\",\n  \"double\": 1.1,\n  \"bool\": true,\n  \"intList\": [1, 2, 3],\n  \"stringList\": [\"a\", \"b\", \"c\"],\n  \"mixedList\": [\"a\", 1, false],\n  \"obj\": { \"key\": \"value\" },\n  \"foo\": { \"bar\": { \"baz\": true, \"string\": \"string\" } },\n  \"mixed\": [{ \"object\": { \"key\": \"value\" } }]\n}\n```\n\nParsing this JSON in Dart usually involves code like:\n\n```dart\nfinal root = jsonDecode(jsonString) as Map\u003cString, dynamic\u003e;\n\nfinal intValue = root['int'] as int;\nfinal strValue = root['string'] as String?;\nfinal doubleValue = root['double'] as double;\nfinal intList = (root['intList'] as List).cast();\nfinal stringList = (root['stringList'] as List).cast();\nfinal mixedList = root['mixedList'] as List;\nfinal obj = root['obj'] as Map\u003cString, dynamic\u003e;\nfinal baz = root['foo']['bar']['baz'] as bool;\nfinal value = root['mixed'][0]['object']['key'] as String;\n```\n\nThis approach is functional but has several drawbacks:\n\n- Code readability suffers.\n- It's error-prone.\n- Handling optional fields is tricky.\n- Debugging can be challenging.\n\n## The Solution: Using `json_native`\n\n`json_native` aims to simplify this process with more readable and less error-prone code.\n\nHere's how:\n\n```dart\nimport 'package:json_native/json_native.dart';\n\nfinal JsonObject root = jsonDecodeCast(jsonString);\n// Yes, JsonObject isn't a new type but an type alias!\nprint(root.runtimeType == Map\u003cString, dynamic\u003e);\n\n// get would cast the type for you\nfinal intValue = root.get\u003cint\u003e('int');\n\n// Yes, nullable type is also supported\nfinal strValue = root.get\u003cString?\u003e('string');\n\n// Type inference would figure out the generic param\nfinal double doubleValue = root.get('double');\n\n// Get a strong typed list\nfinal intList = root.getList\u003cint\u003e('intList');\n\n// Again, type inference would save the generic parameter.\nfinal List\u003cString\u003e stringList = root.getList('stringList');\n\n// If generic parameter is not given, it falls back to dynamic, which can be omitted.\nfinal mixedList = root.getList('mixedList');\n\n// getObj returns JsonObject.\nfinal obj = root.getObj('obj');\n\n// You can nested a series of get into a dig.\nfinal baz = root.dig\u003cbool\u003e(['foo', 'bar', 'baz']);\n\n// dig also support mixed of list and object\nfinal value = root.dig\u003cString\u003e(['mixed', 0, 'object', 'key']);\n```\n\n**Features:**\n\n- Type casting is automatically handled.\n- Supports nullable types.\n- Type inference eliminates the need for explicit generic parameters.\n- Provides strongly-typed lists.\n- Allows for nested object and list traversal with dig.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimnew%2Fjson_native","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimnew%2Fjson_native","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimnew%2Fjson_native/lists"}