Namespace: Newtonsoft.Json.Formatting JSON ফরম্যাট করার অপশন দেয়।
- None: কোনো স্পেশাল ফরম্যাটিং নেই। এটি ডিফল্ট।
- Indented: চাইল্ড অবজেক্টগুলো ইন্ডেন্ট করে (নেস্টেড ফরম্যাট)।
JsonTextWriter.IndentationএবংIndentCharসেটিং অনুযায়ী।
উদাহরণ ১: Indented JSON
static void Main(string[] args)
{
Product product = new Product
{
Name = "Apple",
Expiry = new DateTime(2008, 12, 28),
Price = 3.9900M,
Sizes = new[] { "Small", "Medium", "Large" }
};
string json = JsonConvert.SerializeObject(product, Formatting.Indented);
Console.WriteLine(json);
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
Console.ReadLine();
}
class Product
{
public String[] Sizes { get; set; }
public decimal Price { get; set; }
public DateTime Expiry { get; set; }
public string Name { get; set; }
}
আউটপুট (Indented)
{
"Sizes": [
"Small",
"Medium",
"Large"
],
"Price": 3.9900,
"Expiry": "2008-12-28T00:00:00",
"Name": "Apple"
}
উদাহরণ ২: None (Compact) JSON
static class Program
{
static void Main(string[] args)
{
Product product = new Product
{
Name = "Apple",
Expiry = new DateTime(2008, 12, 28),
Price = 3.9900M,
Sizes = new[] { "Small", "Medium", "Large" }
};
string json = JsonConvert.SerializeObject(product, Formatting.None);
Console.WriteLine(json);
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
Console.ReadLine();
}
}
class Product
{
public String[] Sizes { get; set; }
public decimal Price { get; set; }
public DateTime Expiry { get; set; }
public string Name { get; set; }
}
আউটপুট (None)
{"Sizes":["Small","Medium","Large"],"Price":3.9900,"Expiry":"2008-12-28T00:00:00","Name":"Apple"}
টিপ: ডেভেলপমেন্ট/ডিবাগের সময় Formatting.Indented ব্যবহার করুন, প্রোডাকশনে ব্যান্ডউইডথ বাঁচাতে Formatting.None।