swift의 타입들과 js의 타입의 호환성 때문에 변환 과정이 필요하다.
Encodable일 경우 편하게 jsonencoder를 만들어서 바로 인코딩 해주면 된다.
(아니라면 한번 작업을 거쳐야함)
// json encoder 생성 및 설정 let jsonEncoder = JSONEncoder() jsonEncoder.dateEncodingStrategy = .iso8601 // <-- 필요!! // json으로 인코딩 let jsonData = try jsonEncoder.encode(report) // json 인코딩 결과를 문자열로 변환하여 RN으로 전달 if let jsonString = String(data: jsonData, encoding: .utf8) { resolve(jsonString) }
그런데 날짜값이 예상하지 못한 형태로 반환되는 것을 알 수 있다.
export const owen_real_data: Report = { missingDataRatio: 1, timezone: 'Asia/Seoul', session: { startTime: 724846457, // Foundation의 Date 객체가 encoding 된 결과다. id: '20231221101417_csl9j', state: 'COMPLETE', endTime: 724847777, // Foundation의 Date 객체가 encoding 된 결과다. sleepStages: [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ], createdTimezone: 'Asia/Seoul', }, peculiarities: ['NO_BREATHING_STABILITY', 'TOO_MANY_DEFECTS_IN_SLEEP_STAGES'], };
export const sameReport: Report = { missingDataRatio: 1, timezone: 'Asia/Seoul', session: { startTime: '2023-12-21T10:14:17Z', id: '20231221101417_csl9j', state: 'COMPLETE', endTime: '2023-12-21T10:36:17Z', sleepStages: [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ], createdTimezone: 'Asia/Seoul', }, peculiarities: ['NO_BREATHING_STABILITY', 'TOO_MANY_DEFECTS_IN_SLEEP_STAGES'], }