def main(argv): if len(argv) != 2: raise app.UsageError('Should have exactly one argument for workspace') workspace = argv[1] with open(workspace + '/ndjson/Patient.fhir.ndjson', 'r', encoding='utf-8') as f: # Read first 200 patient records. for i in range(0, 200): ndjson_line = f.readline() # For 5% of records, insert a spelling error in Patient.gender, which is # bound to a value from # https://www.hl7.org/fhir/valueset-administrative-gender.html # See: http://hl7.org/fhir/patient.html if random.random() < .05: ndjson_line = re.sub(r'"gender":"((fe)?)(male)"', '"gender":\"\\2mael\"', ndjson_line) try: patient_proto = json_format.json_fhir_string_to_proto( ndjson_line, patient_pb2.Patient) except ValueError as e: print(f'Failed parsing record {i}: {e}')
def _parse_proto_from_file(uri: str, zip_file_path: str, path: str, resource_type: str) -> Optional[_T]: """Parses a protocol buffer from the resource at the given path. Args: uri: The URI of the resource to parse. zip_file_path: The file path to the zip file containing resource definitions. path: The path within the zip file to the resource file to parse. resource_type: The type of the resource contained at `path`. Returns: The protocol buffer for the resource or `None` if it can not be found. """ with zipfile.ZipFile(zip_file_path, mode='r') as f: raw_json = f.read(path).decode('utf-8') if resource_type in _PROTO_CLASSES_FOR_TYPES: return json_format.json_fhir_string_to_proto( raw_json, _PROTO_CLASSES_FOR_TYPES[resource_type]) elif resource_type == 'Bundle': json_value = _find_resource_in_bundle( uri, json_format.load_json(raw_json)) if json_value is None: return None else: return json_format.json_fhir_object_to_proto( json_value, _PROTO_CLASSES_FOR_TYPES[json_value['resourceType']]) else: logging.warning( 'Unhandled JSON entry: %s for unexpected resource type %s.', path, resource_type) return None
def test_invalid_json(self, file_name: str, field_type: Type[message.Message]): test_suite = self.get_test_suite(file_name) if not test_suite.invalid_json: self.fail('Must have at least one invalid json example!') for invalid_json in test_suite.invalid_json: # Some of the invalid examples may cause non InvalidFhirError errors, # which is acceptable. with self.assertRaises(Exception): _ = json_format.json_fhir_string_to_proto( invalid_json, field_type)
def test_valid_pairs(self, file_name: str, field_name: str, field_type: Type[message.Message]): test_suite = self.get_test_suite(file_name) for pair in test_suite.valid_pairs: expected_proto = proto_utils.get_value_at_field( pair.proto, field_name) expected_json = pair.json_string actual_json = json_format.print_fhir_to_json_string(expected_proto) actual_proto = json_format.json_fhir_string_to_proto( actual_json, field_type, default_timezone='Australia/Sydney') self.assertEqual(expected_json, actual_json) self.assertProtoEqual(expected_proto, actual_proto)
def main(argv): if len(argv) != 2: raise app.UsageError('Should have exactly one argument for workspace') workspace = argv[1] with open(workspace + '/ndjson/Patient.fhir.ndjson', 'r', encoding='utf-8') as f: patients = [] # Read first 200 patient records. for i in range(0, 200): ndjson_line = f.readline() try: patient_proto = json_format.json_fhir_string_to_proto( ndjson_line, patient_pb2.Patient) patients.append(patient_proto) except ValueError as e: print(f'Failed parsing record {i}: {e}') for i, patient in enumerate(patients): # Insert a validation error into 5% of the records. # Patient.communication is an optional BackboneElement field, but if one # is added, it *MUST* contain a populated Patient.communication.language # field. # See: http://hl7.org/fhir/patient.html communication = patient.communication.add() communication.preferred.value = True # Add the required language field to 95% of records if random.random() < .95: language = communication.language.coding.add() language.system.value = 'http://hl7.org/fhir/valueset-all-languages.html' language.code.value = 'fr' try: resource_validation.validate_resource(patient) except fhir_errors.InvalidFhirError as e: print(f'Failed validating record {i}: {e}')
import json from pathlib import Path from google.fhir.r4 import json_format from proto.google.fhir.proto.r4.core.resources.bundle_and_contained_resource_pb2 import ( # noqa: E501 Bundle, ) LPR = Path("25dc5040-5875-8486-a49c-ed4789b7c88c.json") lpr_json = LPR.read_text() original_lpr = json.loads(lpr_json) bundle = json_format.json_fhir_string_to_proto(lpr_json, Bundle) new_lpr_json = json_format.pretty_print_fhir_to_json_string(bundle) new_lpr = json.loads(new_lpr_json) print(new_lpr_json)