public async System.Threading.Tasks.Task <string> Put <T>(string uri, T data) { using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient()) { System.Net.Http.Headers.MediaTypeHeaderValue mediaType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings(); JsonNetFormatter jsonFormatter = new JsonNetFormatter(jsonSerializerSettings); System.Net.Http.ObjectContent content = new System.Net.Http.ObjectContent <T>(data , new System.Net.Http.Formatting.JsonMediaTypeFormatter() ); System.Net.Http.HttpResponseMessage response = await httpClient.PutAsync(uri, content); response.EnsureSuccessStatusCode(); return(response.Content.ReadAsStringAsync().Result); /* * var requestMessage = new System.Net.Http.HttpRequestMessage * (data, mediaType, new System.Net.Http.Formatting.MediaTypeFormatter[] { jsonFormatter }); * * // var result = httpClient.PutAsync("_endpoint", requestMessage.Content).Result; * // return result.Content.ReadAsStringAsync().Result; */ } }
public string Json_Serialize(Type type, object content) { // set up JsonNetFormatter formatter = new JsonNetFormatter(); HttpRequestMessage request = this.GetRequest(content, formatter, type, "application/json"); // verify return this.GetSerialized(content, request, formatter); }
public string Jsonp_Deserialize(string content) { // set up JsonNetFormatter formatter = new JsonNetFormatter(); HttpRequestMessage request = this.GetRequest(content, formatter, "application/javascript"); // verify return (string)this.GetDeserialized(typeof(string), content, request, formatter); }
public string Json_Serialize(Type type, object content) { // set up JsonNetFormatter formatter = new JsonNetFormatter(); HttpRequestMessage request = this.GetRequest(content, formatter, type, "application/json"); // verify return(this.GetSerialized(content, request, formatter)); }
public void Setup() { _formatter = new JsonNetFormatter(); _graph = ConstructComplexGraph(); var ms = new MemoryStream(); _formatter.Serialize(ms, _graph); ms.Position = 0; _target = (MyModel)_formatter.Deserialize(ms); }
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType) { var formatter = new JsonNetFormatter(_jsonSerializerSettings) { JsonpCallbackFunction = GetJsonCallbackFunction(request) }; return(formatter); }
public void SaveAll <T>(List <T> types) where T : Entity { if (types == null) { throw new ArgumentNullException("types"); } string path = GetPath <T>(); using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)) { //IFormatter formatter = new BinaryFormatter(); IFormatter formatter = new JsonNetFormatter(); formatter.Serialize(stream, types); } }
public async Task <IActionResult> Index([FromServices] IConfiguration configuration) { ConnectToStorageContainer(configuration); var model = new FailedEmailsViewModel() { StorageAccountName = _cloudStorageAccount.BlobEndpoint.Authority }; BlobContinuationToken continuationToken = null; var results = new List <IListBlobItem>(); do { var response = await _cloudBlobContainer.ListBlobsSegmentedAsync(continuationToken); continuationToken = response.ContinuationToken; results.AddRange(response.Results); }while (continuationToken != null); model.TotalEmails = results.Count; var serialisationFormatter = new JsonNetFormatter(); const int limit = 50; var count = 0; foreach (CloudBlockBlob blob in results) { if (count >= limit) { break; } var serialised = await blob.DownloadTextAsync(); var deserialised = await Deserialise(serialisationFormatter, blob); model.FailedEmails.Add(new FailedEmailViewModel() { BlobName = blob.Name, DateCreated = blob.Properties.Created.Value, FailedEmail = deserialised }); count++; } return(View(model)); }
public List <T> GetAll <T>() where T : Entity { string path = GetPath <T>(); List <T> types = new List <T>(); try { using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { //IFormatter formatter = new BinaryFormatter(); IFormatter formatter = new JsonNetFormatter(); types = (List <T>)formatter.Deserialize(stream); } } catch (Exception e) { Console.WriteLine("TODO: Log the exception!"); Console.WriteLine(e.Message); } return(types); }
/// <summary /> public static void Register(HttpConfiguration config) { #region Validations if (config == null) { throw new ArgumentNullException(nameof(config)); } #endregion /* * */ var cs = ZincConfiguration.Current.Rest; if (cs == null) { return; } if (cs.Enabled == false) { return; } /* * */ Dictionary <string, string> ext = new Dictionary <string, string>(); foreach (var h in cs.Extensions) { ext.Add(h.Name, h.Type); } /* * MessageHandlers */ if (cs.Handlers != null) { foreach (var e in cs.Handlers.Elements()) { if (ext.ContainsKey(e.Name.LocalName) == false) { throw new ZincConfigurationException(ER.Rest_Configuration_MissingHandler, e.Name.LocalName); } string type = ext[e.Name.LocalName]; DelegatingHandler handler = Platinum.Activator.Create <DelegatingHandler>(type); config.MessageHandlers.Add(handler); } } /* * Exception handling: * - Filter: Exceptions returned from the WebApi are 'reworked'. * - Services: Global exception handling. */ config.Filters.Add(new HandleExceptionFilter()); config.Services.Replace(typeof(System.Web.Http.ExceptionHandling.IExceptionHandler), new ExceptionHandler()); /* * Clear all of the formatters, and *only* add JSON. */ config.Formatters.Clear(); JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Include; settings.Converters.Add(new StringEnumConverter() { AllowIntegerValues = true }); var formatter = new JsonNetFormatter(settings); formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/javascript")); config.Formatters.Add(formatter); /* * Web API routes */ config.MapHttpAttributeRoutes(); }
public string Jsonp_Serialize_WithCustomCallbackMethod(string callbackMethod, int content) { // set up JsonNetFormatter formatter = new JsonNetFormatter(); HttpRequestMessage request = this.GetRequest(content, formatter, typeof(int), "application/javascript"); request.RequestUri = new Uri("http://example.org?callback=" + callbackMethod); formatter = (JsonNetFormatter)formatter.GetPerRequestFormatterInstance(typeof(int), request, new MediaTypeHeaderValue("application/javascript")); // verify return this.GetSerialized(content, request, formatter); }
public JsonNetFormatterTests() { _jsonNetFormatter = new JsonNetFormatter(); }