Fix incorrect date serialization

Summary:
While creating some other tests, discovered that our current date serialization uses `toString()` serialization, causing the amount of milliseconds to be lost. The serialization (see below) uses less bytes as well since the human readable timezone isn't included. This change only affects serialization and is backward compatible.

```
  ✓ test serialize and deserializeObject function for non Object input (1 ms)
  ✕ test makeObjectSerializable and deserializeObject function for Date input (2 ms)
  ✓ test makeObjectSerializable and deserializeObject function for Map of Sets
  ✓ test makeObjectSerializable and deserializeObject function for Map, Dates and Set with complex nesting (1 ms)

  ● test makeObjectSerializable and deserializeObject function for Date input

    expect(received).toEqual(expected) // deep equality

    Expected: 2021-03-01T10:31:07.205Z
    Received: 2021-03-01T10:31:07.000Z
```

Reviewed By: priteshrnandgaonkar

Differential Revision: D26145941

fbshipit-source-id: dfd6607a4199ca46e2075027856138efb88a07f9
This commit is contained in:
Michel Weststrate
2021-02-01 11:40:20 -08:00
committed by Facebook GitHub Bot
parent 7cc55daf34
commit e614993558
2 changed files with 16 additions and 15 deletions

View File

@@ -7,18 +7,18 @@
* @format
*/
import {makeObjectSerializable, deserializeObject} from '../serialization.tsx';
import {makeObjectSerializable, deserializeObject} from '../serialization';
class TestObject extends Object {
constructor(title: Object, map: ?Map<any, any>, set: ?Set<any>) {
constructor(title: Object, map?: Map<any, any>, set?: Set<any>) {
super();
this.title = title;
this.map = map;
this.set = set;
}
title: Object;
map: ?Map<any, any>;
set: ?Set<any>;
map?: Map<any, any>;
set?: Set<any>;
}
test('test makeObjectSerializable function for unnested object with no Set and Map', async () => {
const obj = {key1: 'value1', key2: 'value2'};
@@ -283,12 +283,13 @@ test('test serialize and deserializeObject function for non Object input', async
});
test('test makeObjectSerializable and deserializeObject function for Date input', async () => {
const date = new Date('2019-02-15');
const expectedDate = {
__flipper_object_type__: 'Date',
data: date.toString(),
};
expect(await makeObjectSerializable(date)).toEqual(expectedDate);
const date = new Date(2021, 1, 29, 10, 31, 7, 205);
expect(await makeObjectSerializable(date)).toMatchInlineSnapshot(`
Object {
"__flipper_object_type__": "Date",
"data": "${date.toJSON()}",
}
`);
expect(deserializeObject(await makeObjectSerializable(date))).toEqual(date);
});
@@ -296,7 +297,7 @@ test('test makeObjectSerializable and deserializeObject function for Map of Sets
const map = new Map([
['k1', new Set([1, 2, 3, 4, 5, 6])],
[new Set([1, 2]), new Map([['k3', 'v3']])],
]);
] as any);
const expectedOutput = {
__flipper_object_type__: 'Map',
data: [
@@ -317,16 +318,16 @@ test('test makeObjectSerializable and deserializeObject function for Map, Dates
const map = new Map([
['k1', date1],
['k2', new Set([date2])],
]);
] as any);
const expectedOutput = {
__flipper_object_type__: 'Map',
data: [
['k1', {__flipper_object_type__: 'Date', data: date1.toString()}],
['k1', {__flipper_object_type__: 'Date', data: date1.toJSON()}],
[
'k2',
{
__flipper_object_type__: 'Set',
data: [{__flipper_object_type__: 'Date', data: date2.toString()}],
data: [{__flipper_object_type__: 'Date', data: date2.toJSON()}],
},
],
],

View File

@@ -174,7 +174,7 @@ export async function makeObjectSerializable(
} else if (element instanceof Date) {
dict.set(element, {
__flipper_object_type__: 'Date',
data: element.toString(),
data: element.toJSON(),
});
} else if (element instanceof Array) {
const {childNeedsIteration, outputArr} = processArray(