Serializing and Deserializing JSON (2024)

Serializing and Deserializing JSON (1)

The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping the .NET object property names to the JSON property names and copies the values for you.

  • JsonConvert
  • JsonSerializer

Serializing and Deserializing JSON (2)JsonConvert

For simple scenarios where you want to convert to and from a JSON string, the SerializeObject and DeserializeObject methods on JsonConvert provide an easy-to-use wrapper over JsonSerializer.

Serializing and Deserializing JSON with JsonConvert

Copy

Product product = new Product();product.Name = "Apple";product.ExpiryDate = new DateTime(2008, 12, 28);product.Price = 3.99M;product.Sizes = new string[] { "Small", "Medium", "Large" };string output = JsonConvert.SerializeObject(product);//{// "Name": "Apple",// "ExpiryDate": "2008-12-28T00:00:00",// "Price": 3.99,// "Sizes": [// "Small",// "Medium",// "Large"// ]//}Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

SerializeObject and DeserializeObject both have overloads that take a JsonSerializerSettings object.JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.

Serializing and Deserializing JSON (3)JsonSerializer

For more control over how an object is serialized, the JsonSerializer can be used directly.The JsonSerializer is able to read and write JSON text directly to a stream via JsonTextReaderand JsonTextWriter.Other kinds of JsonWriters can also be used, such asJTokenReader/JTokenWriter,to convert your object to and from LINQ to JSON objects, orBsonReader/BsonWriter, to convert to and from BSON.

Serializing JSON to a Stream with JsonSerializer

Copy

Product product = new Product();product.ExpiryDate = new DateTime(2008, 12, 28);JsonSerializer serializer = new JsonSerializer();serializer.Converters.Add(new JavaScriptDateTimeConverter());serializer.NullValueHandling = NullValueHandling.Ignore;using (StreamWriter sw = new StreamWriter(@"c:\json.txt"))using (JsonWriter writer = new JsonTextWriter(sw)){ serializer.Serialize(writer, product); // {"ExpiryDate":new Date(1230375600000),"Price":0}}

JsonSerializer has a number of properties on it to customize how it serializes JSON. These can also be used with the methods on JsonConvert via the JsonSerializerSettings overloads.

You can read more about the available JsonSerializer settings here: Serialization Settings

Serializing and Deserializing JSON (4)See Also

Serializing and Deserializing JSON (2024)
Top Articles
Why do i keep getting sms verification code
Navigating market volatility: 10 effective ways to help new investors using demat account | Mint
Www.1Tamilmv.cafe
Washu Parking
FFXIV Immortal Flames Hunting Log Guide
Professor Qwertyson
Beautiful Scrap Wood Paper Towel Holder
When is streaming illegal? What you need to know about pirated content
Unraveling The Mystery: Does Breckie Hill Have A Boyfriend?
United Dual Complete Providers
Sotyktu Pronounce
Mid90S Common Sense Media
Bc Hyundai Tupelo Ms
8 Ways to Make a Friend Feel Special on Valentine's Day
Nebraska Furniture Tables
Belle Delphine Boobs
Carolina Aguilar Facebook
Saatva Memory Foam Hybrid mattress review 2024
Transactions (zipForm Edition) | Lone Wolf | Real Estate Forms Software
Orange Pill 44 291
Clare Briggs Guzman
Unionjobsclearinghouse
Encore Atlanta Cheer Competition
Certain Red Dye Nyt Crossword
Aliciabibs
Dmv In Anoka
Pixel Combat Unblocked
Everything You Need to Know About Ñ in Spanish | FluentU Spanish Blog
Khatrimmaza
P3P Orthrus With Dodge Slash
Despacito Justin Bieber Lyrics
Asian Grocery Williamsburg Va
Blasphemous Painting Puzzle
Rs3 Bis Perks
Vons Credit Union Routing Number
Callie Gullickson Eye Patches
Discover Things To Do In Lubbock
Nail Salon Open On Monday Near Me
All-New Webkinz FAQ | WKN: Webkinz Newz
Arigreyfr
Gregory (Five Nights at Freddy's)
Cocorahs South Dakota
Kenner And Stevens Funeral Home
Mybiglots Net Associates
Blow Dry Bar Boynton Beach
Gary Vandenheuvel Net Worth
Aznchikz
Page 5747 – Christianity Today
Is My Sister Toxic Quiz
Factorio Green Circuit Setup
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 6072

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.