/// <summary> /// Creates an instance of the output context for the specified format. /// </summary> /// <param name="format">The format to create the context for.</param> /// <param name="message">The message to use.</param> /// <param name="encoding">The encoding to use.</param> /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param> /// <param name="writingResponse">true if writing a response message; otherwise false.</param> /// <param name="model">The model to use.</param> /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param> /// <returns>Task which represents the pending create operation.</returns> internal static Task <ODataOutputContext> CreateOutputContextAsync( ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, IEdmModel model, IODataUrlResolver urlResolver) { DebugUtils.CheckNoExternalCallers(); if (format == ODataFormat.Atom) { return(ODataAtomOutputContext.CreateAsync( format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } if (format == ODataFormat.VerboseJson) { return(ODataJsonOutputContext.CreateAsync( format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } //// Metadata output is only supported in synchronous operation. if (format == ODataFormat.Batch || format == ODataFormat.RawValue) { return(ODataRawOutputContext.CreateAsync( format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataOutputContext_CreateOutputContext_UnrecognizedFormat)); }
internal static Task <ODataOutputContext> CreateOutputContextAsync(ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, IEdmModel model, IODataUrlResolver urlResolver) { if (format == ODataFormat.Atom) { return(ODataAtomOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } if (format == ODataFormat.VerboseJson) { return(ODataJsonOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } if ((format != ODataFormat.Batch) && (format != ODataFormat.RawValue)) { throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataOutputContext_CreateOutputContext_UnrecognizedFormat)); } return(ODataRawOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); }
internal static string ConvertToUriCollectionLiteral(ODataCollectionValue collectionValue, IEdmModel model, ODataVersion version) { ExceptionUtils.CheckArgumentNotNull<ODataCollectionValue>(collectionValue, "collectionValue"); ExceptionUtils.CheckArgumentNotNull<IEdmModel>(model, "model"); ODataVersionChecker.CheckCollectionValue(version); StringBuilder sb = new StringBuilder(); using (TextWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture)) { JsonWriter jsonWriter = new JsonWriter(writer, false); ODataMessageWriterSettings messageWriterSettings = new ODataMessageWriterSettings { Version = new ODataVersion?(version) }; using (ODataJsonOutputContext context = ODataJsonOutputContext.Create(ODataFormat.VerboseJson, jsonWriter, messageWriterSettings, false, model, null)) { new ODataJsonPropertyAndValueSerializer(context).WriteCollectionValue(collectionValue, null, false); } } return sb.ToString(); }
/// <summary> /// Converts a <see cref="ODataComplexValue"/> to a string for use in a Url. /// </summary> /// <param name="complexValue">Instance to convert.</param> /// <param name="model">Model to be used for validation. User model is optional. The EdmLib core model is expected as a minimum.</param> /// <param name="version">Version to be compliant with.</param> /// <returns>A string representation of <paramref name="complexValue"/> to be added to a Url.</returns> internal static string ConvertToUriComplexLiteral(ODataComplexValue complexValue, IEdmModel model, ODataVersion version) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(complexValue, "complexValue"); ExceptionUtils.CheckArgumentNotNull(model, "model"); StringBuilder builder = new StringBuilder(); using (TextWriter textWriter = new StringWriter(builder, CultureInfo.InvariantCulture)) { JsonWriter jsonWriter = new JsonWriter(textWriter, false); bool writingResponse = false; ODataMessageWriterSettings messageWriterSettings = new ODataMessageWriterSettings() { Version = version }; // Calling dispose since it's the right thing to do, but when created from JsonWriter // the output context Dispose will not actually dispose anything, it will just cleanup itself. using (ODataJsonOutputContext jsonOutputContext = ODataJsonOutputContext.Create( ODataFormat.VerboseJson, jsonWriter, messageWriterSettings, writingResponse, model, null /*urlResolver*/)) { ODataJsonPropertyAndValueSerializer jsonPropertyAndValueSerializer = new ODataJsonPropertyAndValueSerializer(jsonOutputContext); jsonPropertyAndValueSerializer.WriteComplexValue( complexValue, null, /*propertyTypeReference - this call will fill this in and verify if possible*/ true, /*isOpenPropertyType - this determines if the TypeName will be written*/ jsonPropertyAndValueSerializer.CreateDuplicatePropertyNamesChecker(), null /*collectionValidator*/); jsonPropertyAndValueSerializer.AssertRecursionDepthIsZero(); } } return(builder.ToString()); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonOutputContext">The output context to write to.</param> internal ODataJsonCollectionSerializer(ODataJsonOutputContext jsonOutputContext) : base(jsonOutputContext) { DebugUtils.CheckNoExternalCallers(); }