private R4Model.Resource ParseJson(JsonReader reader)
 {
     try
     {
         var FhirJsonParser = new R4Serialization.FhirJsonParser();
         return(FhirJsonParser.Parse <R4Model.Resource>(reader));
     }
     catch (Exception oExec)
     {
         throw new Exception("Error parsing Stu3 json to FHIR Resource, See inner exception info more info", oExec);
     }
 }
示例#2
0
        public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (encoding.EncodingName != Encoding.UTF8.EncodingName)
            {
                throw new FhirFatalException(System.Net.HttpStatusCode.BadRequest, "FHIR supports UTF-8 encoding exclusively, not " + encoding.WebName);
            }


            var request = context.HttpContext.Request;

            // TODO: Brian: Would like to know what the issue is here? Will this be resolved by the Async update to the core?
            if (!request.Body.CanSeek)
            {
                // To avoid blocking on the stream, we asynchronously read everything
                // into a buffer, and then seek back to the beginning.
                request.EnableBuffering();
                Debug.Assert(request.Body.CanSeek);

                // no timeout configuration on this? or does that happen at another layer?
                await request.Body.DrainAsync(CancellationToken.None);

                request.Body.Seek(0L, SeekOrigin.Begin);
            }

            using (var streamReader = context.ReaderFactory(request.Body, encoding))
                using (var jsonReader = new JsonTextReader(streamReader))
                {
                    // need to configure these properties as is done in
                    // HL7.Fhir.SerializationUtil.JsonReaderFromJsonText()
                    jsonReader.DateParseHandling  = DateParseHandling.None;
                    jsonReader.FloatParseHandling = FloatParseHandling.Decimal;

                    try
                    {
                        if (FhirMajorVersion == Common.Enums.FhirVersion.Stu3)
                        {
                            var resource = new R4Serialization.FhirJsonParser().Parse <R4Model.Resource>(jsonReader);
                            return(InputFormatterResult.Success(resource));
                        }
                        else if (FhirMajorVersion == Common.Enums.FhirVersion.R4)
                        {
                            var resource = new Stu3Serialization.FhirJsonParser().Parse <Stu3Model.Resource>(jsonReader);
                            return(InputFormatterResult.Success(resource));
                        }
                        else
                        {
                            throw new FhirFatalException(System.Net.HttpStatusCode.BadRequest, "Unable to resolve which major version of FHIR is in use.");
                        }
                    }
                    catch (FormatException exception)
                    {
                        throw new FhirFatalException(System.Net.HttpStatusCode.BadRequest, "Body parsing failed: " + exception.Message);
                    }
                }
        }