A TypeScript utility for serializing and deserializing JSON data.
This project provides a simple utility class JsonConverter
with static methods to serialize TypeScript objects into JSON strings and deserialize JSON strings back into TypeScript objects. It ensures type safety during deserialization by allowing you to specify the expected type.
- Serialize TypeScript objects to JSON strings.
- Deserialize JSON strings to TypeScript objects with type safety.
Npm:
npm install @devboostsolution/jsonconverter
Yarn:
yarn install @devboostsolution/jsonconverter
import { JsonConverter } from @devboostsolution/jsonconverter
export type User = {
id: number;
name: string;
email: string;
age: number;
};
export type Category = {
id: number;
name: string;
};
export type Product = {
id: number;
name: string;
price: number;
quantity: number;
category: Category;
};
export type Order = {
id: number;
user: User;
products: Product[];
};
const data: Order = {
id: 1,
user: { id: 1, name: 'John', email: '', age: 30 },
products: [
{ id: 1, name: 'Product 1', price: 100, quantity: 1, category: { id: 1, name: 'Category 1' } },
{ id: 2, name: 'Product 2', price: 200, quantity: 2, category: { id: 2, name: 'Category 2' } }
]
};
// Serialize
const dataString = JsonConverter.serialize(data);
// Deserialize
const result = JsonConverter.deserialize<Order>(dataString);
Clone the repository
make install
npm test
Distributed under the XYZ license. See LICENSE
for more information.
https://github.com/DevBoostSolutions/jsonconverter
- Fork it (https://github.com/DevBoostSolutions/jsonconverter)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request