/// <summary> /// Writes the metadata document as the message body. /// </summary> /// <remarks>It is the responsibility of this method to flush the output before the method returns.</remarks> internal override void WriteMetadataDocument() { this.AssertSynchronous(); IEnumerable <EdmError> errors; CsdlJsonWriterSettings writerSettings = new CsdlJsonWriterSettings { IsIeee754Compatible = MessageWriterSettings.IsIeee754Compatible }; if (!CsdlWriter.TryWriteCsdl(this.Model, this.jsonWriter, writerSettings, out errors)) { Debug.Assert(errors != null, "errors != null"); StringBuilder builder = new StringBuilder(); foreach (EdmError error in errors) { builder.AppendLine(error.ToString()); } throw new ODataException(Strings.ODataMetadataOutputContext_ErrorWritingMetadata(builder.ToString())); } this.Flush(); }
public static string SerializeAsJson(this IEdmModel model, bool indented = true, bool isIeee754Compatible = false) { using (MemoryStream memStream = new MemoryStream()) { JsonWriterOptions options = new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, Indented = indented, SkipValidation = false }; using (Utf8JsonWriter jsonWriter = new Utf8JsonWriter(memStream, options)) { CsdlJsonWriterSettings settings = new CsdlJsonWriterSettings(); settings.IsIeee754Compatible = isIeee754Compatible; IEnumerable <EdmError> errors; bool ok = CsdlWriter.TryWriteCsdl(model, jsonWriter, settings, out errors); jsonWriter.Flush(); Assert.True(ok); } memStream.Seek(0, SeekOrigin.Begin); return(new StreamReader(memStream).ReadToEnd()); } }