Serialize and deserialize data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json.test.ts 2.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { describe, expect, it } from 'vitest';
  2. import { deserialize, serialize } from '../../src/types/application/json';
  3. describe('application/json', () => {
  4. describe('serialize', () => {
  5. it('should serialize a string', () => {
  6. const result = serialize('Hello, World!', { type: 'application/json' });
  7. expect(result).toBe('"Hello, World!"');
  8. });
  9. it('should serialize a number', () => {
  10. const result = serialize(123, { type: 'application/json' });
  11. expect(result).toBe('123');
  12. });
  13. it('should serialize a boolean', () => {
  14. const result = serialize(true, { type: 'application/json' });
  15. expect(result).toBe('true');
  16. });
  17. it('should serialize an array', () => {
  18. const result = serialize([1, 2, 3], { type: 'application/json' });
  19. expect(result).toBe('[1,2,3]');
  20. });
  21. it('should serialize an object', () => {
  22. const result = serialize({ a: 1, b: 2, c: 3 }, { type: 'application/json' });
  23. expect(result).toBe('{"a":1,"b":2,"c":3}');
  24. });
  25. it('should serialize an object with indent', () => {
  26. const result = serialize({ a: 1, b: 2, c: 3 }, { type: 'application/json', indent: 2 });
  27. expect(result).toBe('{\n "a": 1,\n "b": 2,\n "c": 3\n}');
  28. });
  29. });
  30. describe('deserialize', () => {
  31. it('should deserialize a string', () => {
  32. const result = deserialize<string>('"Hello, World!"', { type: 'application/json' });
  33. expect(result).toBe('Hello, World!');
  34. });
  35. it('should deserialize a number', () => {
  36. const result = deserialize<number>('123', { type: 'application/json' });
  37. expect(result).toBe(123);
  38. });
  39. it('should deserialize a boolean', () => {
  40. const result = deserialize<boolean>('true', { type: 'application/json' });
  41. expect(result).toBe(true);
  42. });
  43. it('should deserialize an array', () => {
  44. const result = deserialize<number[]>('[1,2,3]', { type: 'application/json' });
  45. expect(result).toEqual([1, 2, 3]);
  46. });
  47. it('should deserialize an object', () => {
  48. const result = deserialize<{ a: number, b: number, c: number }>('{"a":1,"b":2,"c":3}', { type: 'application/json' });
  49. expect(result).toEqual({ a: 1, b: 2, c: 3 });
  50. });
  51. it('should deserialize an object with indent', () => {
  52. const result = deserialize<{ a: number, b: number, c: number }>(
  53. '{\n "a": 1,\n "b": 2,\n "c": 3\n}',
  54. { type: 'application/json' },
  55. );
  56. expect(result).toEqual({ a: 1, b: 2, c: 3 });
  57. });
  58. });
  59. });