Skip to content

DevBoostSolutions/jsonconverter

Repository files navigation

JsonConverter

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.

Features

  • Serialize TypeScript objects to JSON strings.
  • Deserialize JSON strings to TypeScript objects with type safety.

Installation

Npm:

npm install @devboostsolution/jsonconverter

Yarn:

yarn install @devboostsolution/jsonconverter

Usage example

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);

Development setup

Clone the repository

make install
npm test

Meta

Distributed under the XYZ license. See LICENSE for more information.

https://github.com/DevBoostSolutions/jsonconverter

Contributing

  1. Fork it (https://github.com/DevBoostSolutions/jsonconverter)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Releases

No releases published

Packages

No packages published