{"id":24511726,"url":"https://github.com/devnax/json-piece","last_synced_at":"2025-08-07T20:10:30.199Z","repository":{"id":46067082,"uuid":"502555469","full_name":"devnax/json-piece","owner":"devnax","description":"json-piece is a lightweight JavaScript package that provides two main functions: toString and parse","archived":false,"fork":false,"pushed_at":"2025-07-04T17:33:58.000Z","size":156,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-04T18:41:37.308Z","etag":null,"topics":["json-stringify"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/json-piece","language":"TypeScript","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/devnax.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-06-12T08:06:43.000Z","updated_at":"2025-07-04T17:34:02.000Z","dependencies_parsed_at":"2025-01-22T00:41:07.524Z","dependency_job_id":"b3326f8f-0969-4565-bb89-a6f57a18b6d7","html_url":"https://github.com/devnax/json-piece","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/devnax/json-piece","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Fjson-piece","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Fjson-piece/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Fjson-piece/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Fjson-piece/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devnax","download_url":"https://codeload.github.com/devnax/json-piece/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Fjson-piece/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269317236,"owners_count":24396846,"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","status":"online","status_checked_at":"2025-08-07T02:00:09.698Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["json-stringify"],"created_at":"2025-01-22T00:41:04.740Z","updated_at":"2025-08-07T20:10:30.156Z","avatar_url":"https://github.com/devnax.png","language":"TypeScript","readme":"# `json-piece` Documentation\n\n## Overview\n\n`json-piece` is a lightweight JavaScript package that provides two main functions: `toString` and `parse`. It allows you to easily convert JSON objects or arrays to a string representation, and vice versa. This is useful for encoding and decoding complex JSON structures while maintaining their integrity and ensuring they can be easily transmitted or stored.\n\n- **`toString`**: Converts a JSON object or array into a string.\n- **`parse`**: Converts a string representation back into the original JSON object or array.\n\n## Installation\n\nTo install the `json-piece` package, you can use npm:\n\n```bash\nnpm install json-piece\n```\n\nAlternatively, you can use yarn:\n\n```bash\nyarn add json-piece\n```\n\n## Functions\n\n### `toString`\n\nThe `toString` function takes a JSON object or array and converts it into a string representation. Nested objects and arrays are properly handled, ensuring the structure remains intact.\n\n#### Usage\n\n```javascript\nimport { toString } from 'json-piece';\n\nconst obj = {\n  a: 1,\n  b: [2, 3],\n  c: { d: 4, e: 5 }\n};\n\nconst jsonString = toString(obj);\nconsole.log(jsonString); // Encoded string representation of the object\n```\n\n#### Parameters\n\n- `data`: The JSON object or array to convert into a string.\n\n#### Returns\n\n- A string representation of the given JSON object or array. The string is encoded with `encodeURI` to make it URL-safe.\n\n### `parse`\n\nThe `parse` function converts a string representation generated by `toString` back into its original JSON object or array form. This function ensures the nested structures are properly reconstructed.\n\n#### Usage\n\n```javascript\nimport { parse } from 'json-piece';\n\nconst jsonString = '[a\u0026b\u0026c]/0{a=1\u0026b=2|1[a\u0026b\u0026c]}';\n\nconst parsedObj = parse(jsonString);\nconsole.log(parsedObj); // Reconstructed original object\n```\n\n#### Parameters\n\n- `data`: The string representation of a JSON object or array, typically generated by the `toString` function.\n\n#### Returns\n\n- The original JSON object or array.\n\n## How It Works\n\nThe package works by serializing each object or array into a structured format, where nested objects and arrays are assigned an index and represented as references. These references are stored in a `pieces` array and are later used during parsing to reconstruct the original structure.\n\n1. **`toString`**:\n   - Iterates over the data, identifies whether an item is an object, array, or primitive value, and processes it accordingly.\n   - Nested objects and arrays are serialized with unique references, which are stored in the `pieces` array.\n   - The final string is a combination of the root structure and the serialized references.\n\n2. **`parse`**:\n   - The string is split into root and pieces.\n   - The references are resolved using the `pieces` array, and the original objects and arrays are reconstructed.\n\n## Example\n\n### Example 1: Object to String\n\n```javascript\nconst myObject = {\n  name: \"Alice\",\n  age: 30,\n  address: {\n    street: \"123 Main St\",\n    city: \"Wonderland\"\n  }\n};\n\nconst stringified = toString(myObject);\nconsole.log(stringified);\n// Output: Encoded string representation of the object\n```\n\n### Example 2: String to Object\n\n```javascript\nconst stringified = '[a\u0026b\u0026c]/0{a=1\u0026b=2|1[a\u0026b\u0026c]}';\n\nconst parsedObject = parse(stringified);\nconsole.log(parsedObject);\n// Output: The original object structure\n```\n\n## License\n\nThis package is licensed under the MIT License. See [LICENSE](https://github.com/devnax/json-piece/blob/main/LICENSE) for more details.\n\n---\n\n## 🤝 Contributing\n\nContributions are welcome! Please check out the [contribution guidelines](https://github.com/devnax/json-piece).\n\n---\n\n## 📄 License\n\nThis project is licensed under the [MIT License](https://opensource.org/licenses/MIT).\n\n---\n\n## 📞 Support\n\nFor help or suggestions, feel free to open an issue on [GitHub](https://github.com/devnax/json-piece/issues) or contact us via [devnaxrul@gmail.com](mailto:devnaxrul@gmail.com).","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevnax%2Fjson-piece","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevnax%2Fjson-piece","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevnax%2Fjson-piece/lists"}