Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/utils/Processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions test/utils/Processing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
9 changes: 9 additions & 0 deletions test/utils/fixtures/xml-decimals/location.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"resourceType": "Location",
"id": "orgloc",
"name": "Org Location",
"position": {
"longitude": -97.7047386,
"latitude": 30.4159542
}
}
9 changes: 9 additions & 0 deletions test/utils/fixtures/xml-decimals/location.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<Location xmlns="http://hl7.org/fhir">
<id value="orgloc"/>
<name value="Org Location"/>
<position>
<longitude value="-97.7047386"/>
<latitude value="30.4159542"/>
</position>
</Location>