{"id":15693637,"url":"https://github.com/neg4n/obj-serialize","last_synced_at":"2025-05-08T04:53:27.867Z","repository":{"id":119380622,"uuid":"552404839","full_name":"neg4n/obj-serialize","owner":"neg4n","description":"🐆 Simple utility for serializing objects. Lightweight alternative to 'superjson'. Super useful in Next.js Pages Router","archived":false,"fork":false,"pushed_at":"2024-02-08T05:51:53.000Z","size":194,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-08T04:53:20.034Z","etag":null,"topics":["data-serialization","json","next","nextjs","nextjs-plugin","object-serialization","serialization","util","utility-library"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/obj-serialize/","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/neg4n.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-10-16T14:18:57.000Z","updated_at":"2024-07-16T06:32:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"6ea91fc4-0602-44f5-85cd-c02c7f1815bb","html_url":"https://github.com/neg4n/obj-serialize","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"500e0d4c01ef6fd4c824ecf771189eda88ff2150"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neg4n%2Fobj-serialize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neg4n%2Fobj-serialize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neg4n%2Fobj-serialize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neg4n%2Fobj-serialize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neg4n","download_url":"https://codeload.github.com/neg4n/obj-serialize/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253002848,"owners_count":21838638,"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":["data-serialization","json","next","nextjs","nextjs-plugin","object-serialization","serialization","util","utility-library"],"created_at":"2024-10-03T18:46:50.623Z","updated_at":"2025-05-08T04:53:27.844Z","avatar_url":"https://github.com/neg4n.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# obj-serialize\n\n`obj-serialize` is a library containing utility functions and building blocks in to serialize objects to be passed around to another context, between applications or between APIs.\n\n## Explanation and example\n\n_but what does the library description mean...?_\n\n### Example situation\n\nLet's take a [Next.js][next] for an example and let's assume you have some kind of service that queries your database and returns some data about dogs.\n\n```js\n// services/get-dogs.js\nconst dogs = [\n  {\n    name: 'fafik',\n    size: 'small',\n    birth: new Date('1995-12-17T03:24:00'),\n  },\n  {\n    name: 'pimpek',\n    size: 'big',\n    birth: new Date('1995-12-17T03:24:00'),\n  },\n]\n\nexport function getDogs(size) {\n  // In real world scenario, this probably will be a call to the database\n  return dogs.filter(({ size: dogSize }) =\u003e dogSize === size)\n}\n```\n\nThen you want to execute this service and pass the data to your frontend application via [`getServerSideProps`](https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props)\n\n```js\n// pages/index.js\nimport { getDogs } from 'services/get-dogs'\n\nexport async function getServerSideProps() {\n  const smallDogs = getDogs('small')\n  return {\n    props: {\n      smallDogs,\n    },\n  }\n}\n\nexport default function Home({ smallDogs }) {\n  return \u003cdiv\u003ehello {smallDogs[0].name}\u003c/div\u003e\n}\n```\n\n### The problem\n\n[Next.js][next] won’t serialize `Date` object that is present in the `smallDogs` variable. **It can only serialize JSON serializable data types**.\n\nThe error when opening the home page would look like this:\n\n\u003e [!CAUTION]\n\u003e Error: Error serializing `.smallDogs[0].birth` returned from `getServerSideProps` in “/“.  \n\u003e Reason: `object` (“[object Date]”) cannot be serialized as JSON. Please only return JSON serializable data types.\n\n### The solution\n\nHere comes the `obj-serialize` library. You can just do\n\n```js\nimport { nextServerSideSerialize } from 'obj-serialize'\n```\n\nand use it somewhere in your code in order to make any object viable for [Next.js][next] to pass around!\n\n#### Full code snippet\n\n```js\n// pages/index.js\nimport { nextServerSideSerialize } from 'obj-serialize'\nimport { getDogs } from '../services/get-dogs'\n\nexport async function getServerSideProps() {\n  const smallDogs = getDogs('small')\n\n  return {\n    props: {\n      smallDogs: smallDogs.map((dog) =\u003e nextServerSideSerialize(dog)),\n    },\n  }\n}\n\nexport default function Home({ smallDogs }) {\n  return \u003cdiv\u003esiema {smallDogs[0].name}\u003c/div\u003e\n}\n```\n\nThis will work flawlessly ✅\n\n## Customising the serialization\n\nApart from providing out-of-the-box working utility for serialisation that takes place in [Next.js][next] applications, the `obj-serialize` also provides option to create **your own serializers**.\n\n### Building your function\n\nAll you have to do is to import base building block of the library (`serialize` function) and use it as you want.\n\n```js\nimport { serialize } from 'obj-serialize'\n```\n\nThe function accepts data to be serialized as a first parameter and serialization rules as the second parameter. The rules parameter is nothing else but function that is used to _“walk”_ through the object, be executed for each occurrence and eventually convert unserialized data into proper one by returning it.\n\n\u003e [!NOTE]\n\u003e ℹ️ There is a special value called `SkipSerialization`. It is a unique token that is intended to be used when serialisation traverse does not meet any condition in your serialisation rules and you just need to skip the process for particular case. It has to be this token and not `null` or `undefined` since these two can also have impact on desired data after the serialization.\n\n### Example custom serializer\n\nLet’s assume that you want to convert all `Date` objects not `toISOString()` _(as nextServerSideSerialize does)_ but rather `toLocaleString()`.\n\n```js\nimport { serialize, SkipSerialization } from 'obj-serialize'\n\nexport function customSerialize(data) {\n  return serialize(data, (unserializedData) =\u003e {\n    if (unserializedData instanceof Date) {\n      return unserializedData.toLocaleString()\n    } else {\n      return SkipSerialization\n    }\n  })\n}\n```\n\nand thats all! Now you can use your own serializer in the same way as presented [here](#full-code-snippet)\n\n# Acknowledgements and license\n\nThe project is licensed under the MIT License. All contributions are welcome\n\n[next]: https://nextjs.org/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneg4n%2Fobj-serialize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneg4n%2Fobj-serialize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneg4n%2Fobj-serialize/lists"}