public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            return(Task.Factory.StartNew(() =>
            {
                StreamWriter writer = new StreamWriter(writeStream);
                JsonWriter jsonwriter = new JsonTextWriter(writer);

                if (type == typeof(Profile))
                {
                    Profile entry = (Profile)value;
                    FhirSerializer.SerializeResource(entry, jsonwriter);
                }
                else if (type == typeof(ResourceEntry))
                {
                    ResourceEntry entry = (ResourceEntry)value;
                    FhirSerializer.SerializeResource(entry.Resource, jsonwriter);
                }
                else if (type == typeof(Bundle))
                {
                    FhirSerializer.SerializeBundle((Bundle)value, jsonwriter);
                }

                writer.Flush();
            }));
        }
Example #2
0
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (StreamWriter streamwriter = new StreamWriter(writeStream))
                    using (JsonWriter writer = new JsonTextWriter(streamwriter))
                    {
                        bool summary = requestMessage.RequestSummary();

                        if (type == typeof(OperationOutcome))
                        {
                            Resource resource = (Resource)value;
                            FhirSerializer.SerializeResource(resource, writer);
                        }
                        else if (typeof(Resource).IsAssignableFrom(type))
                        {
                            Resource resource = (Resource)value;
                            FhirSerializer.SerializeResource(resource, writer);
                        }
                        else if (typeof(FhirResponse).IsAssignableFrom(type))
                        {
                            FhirResponse response = (value as FhirResponse);
                            if (response.HasBody)
                            {
                                FhirSerializer.SerializeResource(response.Resource, writer, summary);
                            }
                        }
                    }
            }));
        }
Example #3
0
        public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            StreamWriter writer     = new StreamWriter(writeStream);
            JsonWriter   jsonwriter = SerializationUtil.CreateJsonTextWriter(writer); // This will use the BetterJsonWriter which handles precision correctly

            if (type == typeof(OperationOutcome))
            {
                Resource resource = (Resource)value;
                FhirSerializer.SerializeResource(resource, jsonwriter);
            }
            else if (typeof(Resource).IsAssignableFrom(type))
            {
                if (value != null)
                {
                    Resource    r  = value as Resource;
                    SummaryType st = SummaryType.False;
                    if (r.HasAnnotation <SummaryType>())
                    {
                        st = r.Annotation <SummaryType>();
                    }
                    FhirSerializer.SerializeResource(r, jsonwriter, st);
                }
            }
            writer.Flush();
            return(System.Threading.Tasks.Task.FromResult(false));
            // return System.Threading.Tasks.Task.CompletedTask;
        }
Example #4
0
        public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            XmlWriter writer = new XmlTextWriter(writeStream, Encoding.UTF8);

            if (type == typeof(OperationOutcome))
            {
                Resource resource = (Resource)value;
                FhirSerializer.SerializeResource(resource, writer);
            }
            else if (typeof(Resource).IsAssignableFrom(type))
            {
                if (value != null)
                {
                    Resource    r  = value as Resource;
                    SummaryType st = SummaryType.False;
                    if (r.HasAnnotation <SummaryType>())
                    {
                        st = r.Annotation <SummaryType>();
                    }
                    FhirSerializer.SerializeResource(r, writer, st);
                }
            }

            writer.Flush();

            return(System.Threading.Tasks.Task.FromResult(false));
            // return System.Threading.Tasks.Task.CompletedTask;
        }
        private void testSingleJsonResource(string jsonFile)
        {
            Support.ErrorList errors = new Support.ErrorList();
            Model.Resource    singleResult;

            using (JsonTextReader jr = new JsonTextReader(new System.IO.StreamReader(jsonFile)))
            {
                Debug.WriteLine("  Reading from json...");
                singleResult = FhirParser.ParseResource(jr, errors);
                jr.Close();
            }

            if (errors.Count > 0)
            {
                Debug.WriteLine("=== Json Parse errors ===" + Environment.NewLine + errors.ToString());
                _roundTripFailed = true;
            }
            else
            {
                string xmlFile = Path.ChangeExtension(jsonFile, ".xml");

                using (XmlWriter xw = new XmlTextWriter(new System.IO.StreamWriter(xmlFile)))
                {
                    Debug.WriteLine("  Writing xml...");
                    FhirSerializer.SerializeResource(singleResult, xw);
                }
            }
        }
Example #6
0
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            return(Task.Factory.StartNew(() =>
            {
                StreamWriter writer = new StreamWriter(writeStream);
                JsonWriter jsonwriter = new JsonTextWriter(writer);
                if (type == typeof(OperationOutcome))
                {
                    Resource resource = (Resource)value;
                    FhirSerializer.SerializeResource(resource, jsonwriter);
                }
                else if (type == typeof(ResourceEntry))
                {
                    ResourceEntry entry = (ResourceEntry)value;
                    FhirSerializer.SerializeResource(entry.Resource, jsonwriter);
                    content.Headers.SetFhirTags(entry.Tags);
                }
                else if (type == typeof(Bundle))
                {
                    FhirSerializer.SerializeBundle((Bundle)value, jsonwriter);
                }
                else if (type == typeof(TagList))
                {
                    FhirSerializer.SerializeTagList((TagList)value, jsonwriter);
                }

                writer.Flush();
            }));
        }
        private void testSingleResource(string file, string baseFilename)
        {
            Model.Resource    singleResult;
            Support.ErrorList errors = new Support.ErrorList();

            using (XmlReader xr = createReader(file))
            {
                Debug.WriteLine("  Reading Xml...");
                singleResult = FhirParser.ParseResource(xr, errors);
                xr.Close();
            }

            if (errors.Count > 0)
            {
                Debug.WriteLine("=== Xml Parse errors ===" + Environment.NewLine + errors.ToString());
                _roundTripFailed = true;
            }
            else
            {
                string jsonFile = baseFilename + "-roundtrip.json";

                using (JsonTextWriter w = new JsonTextWriter(new System.IO.StreamWriter(jsonFile)))
                {
                    Debug.WriteLine("  Writing json...");
                    FhirSerializer.SerializeResource(singleResult, w);
                }

                testSingleJsonResource(jsonFile);
            }
        }
Example #8
0
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            return(Task.Factory.StartNew(() =>
            {
                XmlWriter writer = new XmlTextWriter(writeStream, new UTF8Encoding(false));
                SummaryType summary = requestMessage.RequestSummary();

                if (type == typeof(OperationOutcome))
                {
                    Resource resource = (Resource)value;
                    FhirSerializer.SerializeResource(resource, writer, summary);
                }
                else if (typeof(Resource).IsAssignableFrom(type))
                {
                    Resource resource = (Resource)value;
                    FhirSerializer.SerializeResource(resource, writer, summary);
                }
                else if (type == typeof(FhirResponse))
                {
                    FhirResponse response = (value as FhirResponse);
                    if (response.HasBody)
                    {
                        FhirSerializer.SerializeResource(response.Resource, writer, summary);
                    }
                }

                writer.Flush();
            }));
        }
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            return(Task.Factory.StartNew(() =>
            {
                XmlWriter writer = new XmlTextWriter(writeStream, Encoding.UTF8);
                // todo: klopt het dat Bundle wel en <?xml ...> header heeft een Resource niet?
                // follow up: is now ticket in FhirApi. Check later.

                if (type == typeof(Profile))
                {
                    Profile resource = (Profile)value;
                    FhirSerializer.SerializeResource(resource, writer);
                }
                else if (type == typeof(ResourceEntry))
                {
                    ResourceEntry entry = (ResourceEntry)value;
                    FhirSerializer.SerializeResource(entry.Resource, writer);
                }
                else if (type == typeof(Bundle))
                {
                    FhirSerializer.SerializeBundle((Bundle)value, writer);
                }

                writer.Flush();
            }));
        }
 protected override void Serialize(Type type, Resource value, TextWriter writer, Encoding encoding)
 {
     using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings {
         OmitXmlDeclaration = true, Encoding = encoding
     }))
     {
         FhirSerializer.SerializeResource(value, xmlWriter);
     }
 }
        public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            if (type == typeof(HttpError))
            {
                HttpError        error     = (HttpError)value;
                OperationOutcome opOutcome = new OperationOutcome();
                List <string>    locations = new List <string>();

                if (!string.IsNullOrEmpty(error.StackTrace))
                {
                    locations.Add(error.StackTrace);
                }

                opOutcome.Issue.Add(new OperationOutcome.IssueComponent()
                {
                    Severity    = OperationOutcome.IssueSeverity.Fatal,
                    Diagnostics = error.Message,
                    Code        = OperationOutcome.IssueType.Exception,
                    Location    = locations
                });

                string logMessage = string.Format("An error with FHIR STU3 ocurred: {0}\nError Detail: {1}\n{2}\n{3}", error.Message, error.MessageDetail, error.ExceptionMessage, error.StackTrace);
                Log.For(this).Error(logMessage);

                value = opOutcome;
                type  = opOutcome.GetType();
            }

            // Return content-type was set by SetDefaultContentType(), just check if that determined that it should be XML
            bool returnXml = ContentType.XML_CONTENT_HEADERS.Contains(content.Headers.ContentType.MediaType);

            return(System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                if (type == typeof(Resource) || type.IsSubclassOf(typeof(Resource)))
                {
                    if (returnXml)
                    {
                        XmlWriter writer = new XmlTextWriter(writeStream, Encoding.UTF8);
                        FhirSerializer.SerializeResource((Resource)value, writer);
                        writer.Flush();
                    }
                    else
                    {
                        StreamWriter writer = new StreamWriter(writeStream);
                        JsonWriter jsonwriter = new JsonTextWriter(writer);
                        Resource resource = (Resource)value;
                        FhirSerializer.SerializeResource(resource, jsonwriter);
                        writer.Flush();
                    }
                }
                else
                {
                    throw new NotSupportedException(String.Format("Cannot write unsupported type {0} to body", type.Name));
                }
            }));
        }
Example #12
0
        private void SaveResourceToXmlFile(IKey key, Resource resource)
        {
            var fileName = GetResourceFileName(key);

            using (var resourceStream = File.Create(fileName))
            {
                using (var writer = XmlWriter.Create(resourceStream))
                {
                    FhirSerializer.SerializeResource(resource, writer);
                }
            }
        }
Example #13
0
        public override System.Threading.Tasks.Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Encoding           = new UTF8Encoding(false),
                OmitXmlDeclaration = true,
                Async           = true,
                CloseOutput     = true,
                Indent          = true,
                NewLineHandling = NewLineHandling.Entitize,
                IndentChars     = "  "
            };

            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Body, settings))
            {
                SummaryType st = SummaryType.False;
                if (context.ObjectType == typeof(OperationOutcome))
                {
                    // We will only honor the summary type during serialization of the outcome
                    // if the resource wasn't a stored OpOutcome we are returning
                    OperationOutcome resource = (OperationOutcome)context.Object;
                    if (string.IsNullOrEmpty(resource.Id) && resource.HasAnnotation <SummaryType>())
                    {
                        st = resource.Annotation <SummaryType>();
                    }
                    FhirSerializer.SerializeResource(resource, writer, st);
                }
                else if (typeof(Resource).IsAssignableFrom(context.ObjectType))
                {
                    if (context.Object != null)
                    {
                        Resource r = context.Object as Resource;
                        if (r.HasAnnotation <SummaryType>())
                        {
                            st = r.Annotation <SummaryType>();
                        }
                        FhirSerializer.SerializeResource(r, writer, st);
                    }
                }
                return(writer.FlushAsync());
            }
        }
        public override System.Threading.Tasks.Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            using (StreamWriter writer = new StreamWriter(context.HttpContext.Response.Body))
            {
                JsonTextWriter jsonwriter = (JsonTextWriter)SerializationUtil.CreateJsonTextWriter(writer); // This will use the BetterJsonWriter which handles precision correctly
                using (jsonwriter)
                {
                    jsonwriter.ArrayPool  = _charPool;
                    jsonwriter.Formatting = Formatting.Indented; // lets make it pretty

                    SummaryType st = SummaryType.False;
                    if (context.ObjectType == typeof(OperationOutcome))
                    {
                        // We will only honor the summary type during serialization of the outcome
                        // if the resource wasn't a stored OpOutcome we are returning
                        OperationOutcome resource = (OperationOutcome)context.Object;
                        if (string.IsNullOrEmpty(resource.Id) && resource.HasAnnotation <SummaryType>())
                        {
                            st = resource.Annotation <SummaryType>();
                        }
                        FhirSerializer.SerializeResource(resource, jsonwriter, st);
                    }
                    else if (typeof(Resource).IsAssignableFrom(context.ObjectType))
                    {
                        if (context.Object != null)
                        {
                            Resource r = context.Object as Resource;
                            if (r.HasAnnotation <SummaryType>())
                            {
                                st = r.Annotation <SummaryType>();
                            }
                            FhirSerializer.SerializeResource(r, jsonwriter, st);
                        }
                    }
                    return(writer.FlushAsync());
                }
            }
        }
Example #15
0
        static void Main(string[] args)
        {
            var client = new FhirClient("http://localhost:1398/fhir/");

            client.PreferredFormat = ResourceFormat.Json;
            Parameters inputParameters = null;

            using (var inputFile = File.Open("Examples\\guidance-operation-request-example-v2.xml", FileMode.Open))
            {
                using (var inputReader = XmlReader.Create(inputFile))
                {
                    inputParameters = FhirParser.ParseResource(inputReader) as Parameters;
                }
            }

            if (inputParameters == null)
            {
                Console.WriteLine("Could not parse input parameters.");
            }
            else
            {
                //var result = client.Create<Parameters>(inputParameters);
                var result = client.WholeSystemOperation("guidance", inputParameters);
                if (result != null)
                {
                    using (var outputFile = File.Create("Examples\\guidance-operation-result-example-v2.xml"))
                    {
                        using (var outputWriter = XmlWriter.Create(outputFile))
                        {
                            FhirSerializer.SerializeResource(result, outputWriter);
                        }
                    }
                    Console.WriteLine("Operation output written to file.");
                }
                else
                {
                    Console.WriteLine("Operation returned null.");
                }
            }
            Console.ReadLine();
        }
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
     FhirSerializer.SerializeResource(value as Resource, writer);
 }
Example #17
0
        protected override void Serialize(Type type, Resource value, TextWriter writer, Encoding encoding)
        {
            JsonWriter jsonWriter = new JsonTextWriter(writer);

            FhirSerializer.SerializeResource(value, jsonWriter);
        }