/// <inheritdoc /> public bool TrySerialize(object content, IRequestBodyWriter writer) { string xml = null; XmlSerializer serializer = new XmlSerializer(content.GetType()); using (StringWriter stringWriter = new StringWriter()) { using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter)) { serializer.Serialize(xmlWriter, content); xml = stringWriter.ToString(); } } if (String.IsNullOrWhiteSpace(xml)) { return(false); } //At this point we have hopefully valid XML and we should write it with the content type writer.Write(xml, AssociatedContentType.First()); return(true); }
/// <inheritdoc /> public bool TrySerialize(object content, IRequestBodyWriter writer) { if (content == null) { throw new ArgumentNullException(nameof(content)); } writer.Write(content.ToString(), AssociatedContentType.First()); return(true); }
/// <inheritdoc /> public bool TrySerialize(object content, IRequestBodyWriter writer) { string json = JsonConvert.SerializeObject(content); if (String.IsNullOrWhiteSpace(json)) { return(false); } //At this point we have hopefully valid JSON and we should write it with the content type writer.Write(json, AssociatedContentType.First()); return(true); }
/// <inheritdoc /> public bool TrySerialize(object content, IRequestBodyWriter writer) { if (content == null) { return(false); } using (MemoryStream ms = new MemoryStream()) { ProtoBuf.Serializer.Serialize(ms, content); writer.Write(ms.ToArray(), AssociatedContentType.First()); } return(true); }
/// <inheritdoc /> public bool TrySerialize(object content, IRequestBodyWriter writer) { if (content == null) { throw new ArgumentNullException(nameof(content)); } //If we don't have a mediator collection prepared for the type we should create one. if (!ModelTypeToReflectionMediatorCollection.ContainsKey(content.GetType())) { ModelTypeToReflectionMediatorCollection[content.GetType()] = BuildUrlEncodedMemberCollection(content); return(TrySerialize(content, writer)); } UrlEncodedMemberKeyValueCollectionAdapter urlEncocedCollection = new UrlEncodedMemberKeyValueCollectionAdapter(ModelTypeToReflectionMediatorCollection[content.GetType()], content); writer.Write(new FormUrlEncodedContent(urlEncocedCollection).ReadAsStringAsync().Result, AssociatedContentType.First()); return(true); }