dsr-kv
v0.2.2
Published
De-Serialize keys or values in a stringified JSON object.
Downloads
13
Maintainers
Readme
dsr-kv
De-Serialize object keys
or values
in a stringified JSON object/data structure.
Installation
To get started, run the command:
npm install dsr-kv
[!NOTE] It doesn't deserialize the whole JSON string(as
JSON.parse()
would), instead just thekeys
orvalues
(or both) of objects found in the JSON string.
Why?
While building lofo, as a step during the program's execution, I wanted to write an object
literal to a file. The obvious solution was to JSON.stringify()
the object first, then write it to the file but, the result of that approach was this issue, where Next.js would fail with an error: Unexpected object key type
, when trying to read the "stringified" object. I concluded that the solution was to "deserialize" the object key first, then write to the file. It worked!
At the time, I had a minimal implementation of the idea(that worked), this package is just a "finished" version of that implementation.
Of note, is that the there is currently no implementation for deserialization of object
values
(onlykeys
) as the use case for that is unclear for now...
Usage
import { dsr } from "dsr-kv"
const data = {
hello: "world"
}
// Get JSON string;
const jsonString = JSON.stringify(data); // Output: '{"hello":"world"}'
// Deserialize object keys found in JSON string;
dsr(jsonString) // Output: '{hello:"world"}'