npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

obj-serialize

v2.1.0

Published

Simple utility to serialize objects to be passed around to another context. Useful in Next.js Pages Router projects.

Downloads

26

Readme

obj-serialize

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.

Explanation and example

but what does the library description mean...?

Example situation

Let's take a Next.js for an example and let's assume you have some kind of service that queries your database and returns some data about dogs.

// services/get-dogs.js
const dogs = [
  {
    name: 'fafik',
    size: 'small',
    birth: new Date('1995-12-17T03:24:00'),
  },
  {
    name: 'pimpek',
    size: 'big',
    birth: new Date('1995-12-17T03:24:00'),
  },
]

export function getDogs(size) {
  // In real world scenario, this probably will be a call to the database
  return dogs.filter(({ size: dogSize }) => dogSize === size)
}

Then you want to execute this service and pass the data to your frontend application via getServerSideProps

// pages/index.js
import { getDogs } from 'services/get-dogs'

export async function getServerSideProps() {
  const smallDogs = getDogs('small')
  return {
    props: {
      smallDogs,
    },
  }
}

export default function Home({ smallDogs }) {
  return <div>hello {smallDogs[0].name}</div>
}

The problem

Next.js won’t serialize Date object that is present in the smallDogs variable. It can only serialize JSON serializable data types.

The error when opening the home page would look like this:

[!CAUTION] Error: Error serializing .smallDogs[0].birth returned from getServerSideProps in “/“.
Reason: object (“[object Date]”) cannot be serialized as JSON. Please only return JSON serializable data types.

The solution

Here comes the obj-serialize library. You can just do

import { nextServerSideSerialize } from 'obj-serialize'

and use it somewhere in your code in order to make any object viable for Next.js to pass around!

Full code snippet

// pages/index.js
import { nextServerSideSerialize } from 'obj-serialize'
import { getDogs } from '../services/get-dogs'

export async function getServerSideProps() {
  const smallDogs = getDogs('small')

  return {
    props: {
      smallDogs: smallDogs.map((dog) => nextServerSideSerialize(dog)),
    },
  }
}

export default function Home({ smallDogs }) {
  return <div>siema {smallDogs[0].name}</div>
}

This will work flawlessly ✅

Customising the serialization

Apart from providing out-of-the-box working utility for serialisation that takes place in Next.js applications, the obj-serialize also provides option to create your own serializers.

Building your function

All you have to do is to import base building block of the library (serialize function) and use it as you want.

import { serialize } from 'obj-serialize'

The 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.

[!NOTE] ℹ️ 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.

Example custom serializer

Let’s assume that you want to convert all Date objects not toISOString() (as nextServerSideSerialize does) but rather toLocaleString().

import { serialize, SkipSerialization } from 'obj-serialize'

export function customSerialize(data) {
  return serialize(data, (unserializedData) => {
    if (unserializedData instanceof Date) {
      return unserializedData.toLocaleString()
    } else {
      return SkipSerialization
    }
  })
}

and thats all! Now you can use your own serializer in the same way as presented here

Acknowledgements and license

The project is licensed under the MIT License. All contributions are welcome