newton json - a brief overview
TRANSCRIPT
-
8/11/2019 Newton JSON - A Brief Overview
1/11
Json.NET
Popular high-performanceJSON framework for .NET
-
8/11/2019 Newton JSON - A Brief Overview
2/11
Json.NETJson.NET is a popular high-performance JSON framework for .NET
Features Flexible JSON serialier for con!erting between .NET ob"ects an# JSON $%N& to JSON for manuall' rea#ing an# writing JSON (igh performance) faster than .NET*s built-in JSON serialiers +rite in#ente#) eas' to rea# JSON ,on!ert JSON to an# from $ Supports .NET /) .NET 0.1) .NET 2) Sil!erlight) +in#ows Phone an# +in#ows 3.
The JSON serialier is a goo# choice when the JSON 'ou are rea#ing or writing maps closel' to a .NETclass.
$%N& to JSON is goo# for situations where 'ou are onl' intereste# in getting !alues from JSON) 'ou #on*tha!e a class to serialie or #eserialie to) or the JSON is ra#icall' #ifferent from 'our class an# 'ou nee#to manuall' rea# an# write from 'our ob"ects.
That4s cool) JSON is lightweight an# there is no un-necessar' #ata using o!erhea# ban#wi#th like as for$. The +eb 5P% has a built-in capabilit' to serialie the return !alue as JSON format but some situationexists 6in another ser!ice or Page etho# in the co#e behin# of the +ebForm7 that the #e!eloper nee#s to
serialie #ata in JSON format.
-
8/11/2019 Newton JSON - A Brief Overview
3/11
-
8/11/2019 Newton JSON - A Brief Overview
4/11
SerialiationLets say that for example I want to send data to the browser. All I have to do isto create an ExpandoObject (my new favourite .et class!" and seriali#e it usin$%son.et &%son'onvert.eriali#eObject(").
dynamic foo * new ExpandoObject("+foo.,ar * -somethin$-+strin$ json * ewtonsoft.%son.%son'onvert.eriali#eObject(foo"+
-
8/11/2019 Newton JSON - A Brief Overview
5/11
8eserialiation%son.et offers a $reat way to deseriali#e a %O strin$ into a dynamic usin$the %Object (you can find it under the ewtonsoft.%son.Lin namespace andhere the details".
dynamic foo * %Object./arse(json0ext"+strin$ bar * foo.,ar+ 11 bar * -somethin$-
-
8/11/2019 Newton JSON - A Brief Overview
6/11
JSON erge
The most !isible new feature in this release is the abilit' to 9uickl' merge JSON using theerge metho# a##e# to JOb"ect an# Jarra'.
JOb"ect o: ; JOb"ect.Parse6 *FirstName*? *John*) *$astName*? *Smith*) *Enable#*? false) *@oles*? A *Bser* CD=7
JOb"ect o/ ; JOb"ect.Parse6 *Enable#*? true) *@oles*? A *Bser*) *5#min* CD=7o:.erge6o/) new JsonergeSettings> union arra' !alues together to a!oi# #uplicates erge5rra'(an#ling ; erge5rra'(an#ling.Bnion
D7string "son ; o:.ToString67
-
8/11/2019 Newton JSON - A Brief Overview
7/11
8epen#enc' %n"ectionThe low-le!el ,onstructor%nfo properties on JsonOb"ect,ontract) use# when creating ob"ects #uring #eserialiation) are nowobsolete an# ha!e been replace# with functions. 5lso Json.NET no longer imme#iatel' throws an exception if it tries to#eserialie an interface or abstract t'pe. %f 'ou ha!e specifie# a wa' for that t'pe to be create#) such as resol!ing it from a#epen#enc' in"ect framework) then Json.NET will happil' continue #eserialiing using that instance .
public class 5utofac,ontract@esol!er ? 8efault,ontract@esol!er> pri!ate rea#onl' %,ontainer Gcontainer public 5utofac,ontract@esol!er6%,ontainer container7 > Gcontainer ; container
D protecte# o!erri#e JsonOb"ect,ontract ,reateOb"ect,ontract6T'pe ob"ectT'pe7 > JsonOb"ect,ontract contract ; base.,reateOb"ect,ontract6ob"ectT'pe7 use 5utofac to create t'pes that ha!e been registere# with it if 6Gcontainer.%s@egistere#6ob"ectT'pe77 contract.8efault,reator ; 67 ;H Gcontainer.@esol!e6ob"ectT'pe7 return contract DD
-
8/11/2019 Newton JSON - A Brief Overview
8/11
8'namic JSON Parsing
One of the features that % think is getting e!er more important is the abilit' to serialie an# #eserialiearbitrar' JSON content #'namicall' - that is without mapping the JSON capture# #irectl' into a .NET t'peas 8ata,ontractSerialier or the Ja!aScript Serialiers #o. Sometimes it isn*t possible to map t'pes #ue tothe #ifferences in languages 6think collections) #ictionaries etc7) an# other times 'ou simpl' #on*t ha!e thestructures in place or #on*t want to create them to actuall' import the #ata.
%f this topic soun#s familiar - 'ou*re rightI % wrote about #'namic JSON parsing a few months back beforeJSON.NET was a##e# to +eb 5P% an# when +eb 5P% an# the S'stem.Net (ttp,lient libraries inclu#e# theS'stem.Json classes like JsonOb"ect an# Json5rra'. +ith the inclusion of JSON.NET in +eb 5P% theseclasses are now obsolete an# #i#n*t ship with +eb 5P% or the client libraries. % re-linke# m' original post tothis one. %n this post %*ll #iscus JToken) JOb"ect an# J5rra' which are the #'namic JSON ob"ects that makeit !er' eas' to create an# retrie!e JSON content on the fl' without un#erl'ing t'pes.
-
8/11/2019 Newton JSON - A Brief Overview
9/11
etting JSON.NET
The easiest wa' to use JSON.NET is to grab it !ia Nuet an# a## it as a reference to 'ourpro"ect. Kou can a## it to 'our pro"ect with?
PH %nstall-Package Newtonsoft.Json
From the Package anager ,onsole or b' using anage Nuet Packages in 'our pro"ect
@eferences. 5s mentione# if 'ou*re using 5SP.NET +eb 5P% or L, 2 JSON.NET will beautomaticall' a##e# to 'our pro"ect.
5lternatel' 'ou can also go to the ,o#ePlex site an# #ownloa# the latest !ersion inclu#ingsource co#e?
http?"son.co#eplex.com
-
8/11/2019 Newton JSON - A Brief Overview
10/11
5pex T.. %n#ia P!t. $t#E234 ector2567oida 2348648 (9/" I:IA
/hone; (4834"2 ax; (4834"2
-
8/11/2019 Newton JSON - A Brief Overview
11/11