Serialize and deserialize data.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

json.test.ts 2.6 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', async () => {
  6. const result = await serialize('Hello, World!', { type: 'application/json' });
  7. expect(result).toBe('"Hello, World!"');
  8. });
  9. it('should serialize a number', async () => {
  10. const result = await serialize(123, { type: 'application/json' });
  11. expect(result).toBe('123');
  12. });
  13. it('should serialize a boolean', async () => {
  14. const result = await serialize(true, { type: 'application/json' });
  15. expect(result).toBe('true');
  16. });
  17. it('should serialize an array', async () => {
  18. const result = await serialize([1, 2, 3], { type: 'application/json' });
  19. expect(result).toBe('[1,2,3]');
  20. });
  21. it('should serialize an object', async () => {
  22. const result = await 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', async () => {
  26. const result = await 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', async () => {
  32. const result = await deserialize<string>('"Hello, World!"', { type: 'application/json' });
  33. expect(result).toBe('Hello, World!');
  34. });
  35. it('should deserialize a number', async () => {
  36. const result = await deserialize<number>('123', { type: 'application/json' });
  37. expect(result).toBe(123);
  38. });
  39. it('should deserialize a boolean', async () => {
  40. const result = await deserialize<boolean>('true', { type: 'application/json' });
  41. expect(result).toBe(true);
  42. });
  43. it('should deserialize an array', async () => {
  44. const result = await deserialize<number[]>('[1,2,3]', { type: 'application/json' });
  45. expect(result).toEqual([1, 2, 3]);
  46. });
  47. it('should deserialize an object', async () => {
  48. const result = await 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', async () => {
  52. const result = await 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. });