JSON serialization is undoubtedly one of the most important performance improvements within the ASP.
Inside the ASP, we can insert a custom mediatypeformatter (media formatter),
Plainly, it is based on HTTP Content-type Application/json
To determine which media formatter to use
Implementation, remember to introduce the Jil package
public class Jilformatter:mediatypeformatter {private readonly Options _jiloptions; Private MethodInfo _method; Public Jilformatter () {//Time format to serialize _jiloptions = new Options (Dateformat:datetimeformat. ISO8601); Media type Supportedmediatypes.add (new Mediatypeheadervalue ("Application/json")); Added utf8encoding encoded Supportedencodings.add (new UTF8Encoding (Encodershouldemitutf8identifier:false, THROWONINV Alidbytes:true)); Added unicodeencoding encoded Supportedencodings.add (new UnicodeEncoding (Bigendian:false, byteordermark:true, throw Oninvalidbytes:true)); }//To determine whether the deserialization type public override bool Canreadtype (type type) {if (type = = NULL) {throw new ArgumentNullException ("type"); } return true; }//Determine if serialization type public override bool Canwritetype (TYpe type) {if (type = = null) {throw new ArgumentNullException ("type" ); } return true; }//Asynchronously deserializes an object of the specified type. public override task<object> Readfromstreamasync (type type, Stream readstream, System.Net.Http.HttpContent Content, Iformatterlogger Formatterlogger) {return Task.fromresult (Deserializefromstream (type, read Stream)); } Private Object Deserializefromstream (type type, Stream readstream) {try { using (var reader = new StreamReader (Readstream)) {return JSON. Deserialize (reader, type, _jiloptions); }} catch {return null; }}//Asynchronously serializes an object of the specified type. public override Task Writetostreamasync (type type, object value, Stream Writestream, SysTem. Net.Http.HttpContent content, TransportContext transportcontext) {var streamWriter = new Streamwrit ER (writestream); Json. Serialize (value, StreamWriter, _jiloptions); StreamWriter.Flush (); Return Task.fromresult (Writestream); } }
Adding to the media formatter collection
public static void Register (httpconfiguration config) { //Web API Configuration and service //Web API Route CONFIG. Maphttpattributeroutes (); Config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{id}", defaults:new {id = Routeparameter.optional} );
Globalconfiguration.configuration.formatters[0] = new Jilformatter (); } }
Control class
public class Homecontroller:apicontroller {public static list<market> = new List<market > (); Public HomeController () {} static HomeController () {Market S.add (New Market {Id = 1, Name = "1", Datetimenow = DateTime.Now}); Markets.add (New Market {Id = 2, Name = "1", Datetimenow = DateTime.Now}); Markets.add (New Market {Id = 3, Name = "1", Datetimenow = DateTime.Now}); Markets.add (New Market {Id = 4, Name = "1", Datetimenow = DateTime.Now}); Markets.add (New Market {Id = 5, Name = "1", Datetimenow = DateTime.Now}); } public ienumerable<market> Get () {return to; Public Httpresponsemessage Post ([frombody]market value) {Markets.add (value); return new Httpresponsemessage (Httpstatuscode.ok); } public HttpresponsemesSage Put (int id, [frombody]market value) {var market = markets.where (x = = X.id = = ID). FirstOrDefault (); if (market = = NULL) return new Httpresponsemessage (Httpstatuscode.notfound); Market. Name = value. Name; return new Httpresponsemessage (Httpstatuscode.ok); } public httpresponsemessage Delete (int id) {var result = Markets.remove (Markets.where (x =&G T X.id = = Id). FirstOrDefault ()); return result? New Httpresponsemessage (httpstatuscode.accepted): New Httpresponsemessage (Httpstatuscode.notfound); } }
We visit through postman
We're in post an entity
Let's get a little bit.
You can already see the entity that was just post.
Source: http://pan.baidu.com/s/1i3o6KD3
ASP. NET MVC Vnext series http://www.cnblogs.com/liek/p/4634294.html
It's my biggest motivation to get your affirmation!
Category: ASP. Vnext
Jil Serialization JSON