|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { describe, expect, it } from 'vitest';
- import { deserialize, serialize } from '../../src/types/application/json';
-
- describe('application/json', () => {
- describe('serialize', () => {
- it('should serialize a string', async () => {
- const result = await serialize('Hello, World!', { type: 'application/json' });
- expect(result).toBe('"Hello, World!"');
- });
-
- it('should serialize a number', async () => {
- const result = await serialize(123, { type: 'application/json' });
- expect(result).toBe('123');
- });
-
- it('should serialize a boolean', async () => {
- const result = await serialize(true, { type: 'application/json' });
- expect(result).toBe('true');
- });
-
- it('should serialize an array', async () => {
- const result = await serialize([1, 2, 3], { type: 'application/json' });
- expect(result).toBe('[1,2,3]');
- });
-
- it('should serialize an object', async () => {
- const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/json' });
- expect(result).toBe('{"a":1,"b":2,"c":3}');
- });
-
- it('should serialize an object with indent', async () => {
- const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/json', indent: 2 });
- expect(result).toBe('{\n "a": 1,\n "b": 2,\n "c": 3\n}');
- });
- });
-
- describe('deserialize', () => {
- it('should deserialize a string', async () => {
- const result = await deserialize<string>('"Hello, World!"', { type: 'application/json' });
- expect(result).toBe('Hello, World!');
- });
-
- it('should deserialize a number', async () => {
- const result = await deserialize<number>('123', { type: 'application/json' });
- expect(result).toBe(123);
- });
-
- it('should deserialize a boolean', async () => {
- const result = await deserialize<boolean>('true', { type: 'application/json' });
- expect(result).toBe(true);
- });
-
- it('should deserialize an array', async () => {
- const result = await deserialize<number[]>('[1,2,3]', { type: 'application/json' });
- expect(result).toEqual([1, 2, 3]);
- });
-
- it('should deserialize an object', async () => {
- const result = await deserialize<{ a: number, b: number, c: number }>('{"a":1,"b":2,"c":3}', { type: 'application/json' });
- expect(result).toEqual({ a: 1, b: 2, c: 3 });
- });
-
- it('should deserialize an object with indent', async () => {
- const result = await deserialize<{ a: number, b: number, c: number }>(
- '{\n "a": 1,\n "b": 2,\n "c": 3\n}',
- { type: 'application/json' },
- );
- expect(result).toEqual({ a: 1, b: 2, c: 3 });
- });
- });
- });
|