diff --git a/src/utils/Processing.ts b/src/utils/Processing.ts index 8ce2c5de..8a28fd9a 100644 --- a/src/utils/Processing.ts +++ b/src/utils/Processing.ts @@ -295,7 +295,11 @@ export function readJSONorXML(file: string): FileImport { return importedFile; } else if (file.endsWith('.xml')) { const buffer = fs.readFileSync(file); - const importedFile: FileImport = { content: FHIRConverter.xmlToObj(buffer.toString()) }; + // xmlToObj keeps decimal values as strings, so convert to JSON text first. + // xmlToJson writes decimals as JSON numbers, which gives the same result as reading + // the equivalent JSON file. + const json = FHIRConverter.xmlToJson(buffer.toString()); + const importedFile: FileImport = { content: json != null ? JSON.parse(json) : undefined }; if (buffer.length > LARGE_FILE_BUFFER_LENGTH) { importedFile.large = true; } diff --git a/test/utils/Processing.test.ts b/test/utils/Processing.test.ts index 3a04ecca..8aab7725 100644 --- a/test/utils/Processing.test.ts +++ b/test/utils/Processing.test.ts @@ -566,6 +566,17 @@ describe('Processing', () => { ); }); + it('should read decimal values in xml files as numbers', () => { + const XMLFilePath = path.join(__dirname, 'fixtures', 'xml-decimals', 'location.xml'); + const JSONFilePath = path.join(__dirname, 'fixtures', 'xml-decimals', 'location.json'); + + const xmlFileImport = readJSONorXML(XMLFilePath); + + expect(xmlFileImport.content.position.longitude).toBe(-97.7047386); + expect(xmlFileImport.content.position.latitude).toBe(30.4159542); + expect(xmlFileImport.content).toEqual(fs.readJSONSync(JSONFilePath)); + }); + it('should not log an error when JSON files begin with a Byte Order Mark', () => { const JSONFilePath = path.join(__dirname, 'fixtures', 'with-BOM', 'badBOM.json'); diff --git a/test/utils/fixtures/xml-decimals/location.json b/test/utils/fixtures/xml-decimals/location.json new file mode 100644 index 00000000..a2e25b37 --- /dev/null +++ b/test/utils/fixtures/xml-decimals/location.json @@ -0,0 +1,9 @@ +{ + "resourceType": "Location", + "id": "orgloc", + "name": "Org Location", + "position": { + "longitude": -97.7047386, + "latitude": 30.4159542 + } +} diff --git a/test/utils/fixtures/xml-decimals/location.xml b/test/utils/fixtures/xml-decimals/location.xml new file mode 100644 index 00000000..9ebe5a13 --- /dev/null +++ b/test/utils/fixtures/xml-decimals/location.xml @@ -0,0 +1,9 @@ + + + + + + + + +