internal static string ConvertToUriComplexLiteral(ODataComplexValue complexValue, IEdmModel model, ODataVersion version) { ExceptionUtils.CheckArgumentNotNull<ODataComplexValue>(complexValue, "complexValue"); ExceptionUtils.CheckArgumentNotNull<IEdmModel>(model, "model"); StringBuilder sb = new StringBuilder(); using (TextWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture)) { JsonWriter jsonWriter = new JsonWriter(writer, false); bool writingResponse = false; ODataMessageWriterSettings messageWriterSettings = new ODataMessageWriterSettings { Version = new ODataVersion?(version) }; using (ODataJsonOutputContext context = ODataJsonOutputContext.Create(ODataFormat.VerboseJson, jsonWriter, messageWriterSettings, writingResponse, model, null)) { ODataJsonPropertyAndValueSerializer serializer = new ODataJsonPropertyAndValueSerializer(context); serializer.WriteComplexValue(complexValue, null, true, serializer.CreateDuplicatePropertyNamesChecker(), null); } } 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()); }