Exemple #1
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());
                }
            }
        }