📘 C# Blog Series – Blog 45 **“C# JSON Handling — System.Text.Json vs Newtonsoft.Json

📘 C# Blog Series – Blog 45

**“C# JSON Handling — System.Text.Json vs Newtonsoft.Json







(Real-Life Example: GPS Packet JSON Conversion, API Request/Response, Device Models)”**

आज की backend दुनिया में JSON = Most Important Data Format है।
GPS Tracking, Web APIs, Mobile Apps, IoT—
हर system JSON पर ही चलता है।

C# में JSON handling करने के लिए दो main libraries हैं:

System.Text.Json (Fastest + Built-in)

Newtonsoft.Json (Older but Feature-rich)

आज हम दोनों को detail में सीखेंगे,
और GPS server के real-world examples भी देखेंगे।


🟦 1. JSON क्या है? (Simple Definition)

JSON =
Human-readable data format
जिससे server ↔ app data exchange करते हैं।

Example GPS JSON:

{ "imei": "861128068064267", "lat": 26.85, "lon": 80.94, "speed": 45 }

🟢 2. System.Text.Json — Modern High-Speed JSON

Using directives:

using System.Text.Json;

Serialize (Object → JSON)

string json = JsonSerializer.Serialize(device);

Deserialize (JSON → Object)

GPSDevice dev = JsonSerializer.Deserialize<GPSDevice>(json);

✔ Fastest
✔ Built-in
✔ Low memory usage
❌ Missing some advanced features


3. Newtonsoft.Json — सबसे Popular Library

using Newtonsoft.Json;

Serialize

string json = JsonConvert.SerializeObject(device);

Deserialize

var dev = JsonConvert.DeserializeObject<GPSDevice>(json);

✔ Very flexible
✔ Tons of features
✔ Best for complex JSON
❌ Slower than System.Text.Json


🌟 4. GPS Device Model Example

class GPSDevice { public string IMEI { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public int Battery { get; set; } }

🟠 5. Real GPS Packet JSON Conversion

Your packet:

IMEI=8611280;LAT=26.85;LON=80.94;BAT=90;

Convert to object:

var dev = new GPSDevice { IMEI = "8611280", Latitude = 26.85, Longitude = 80.94, Battery = 90 };

Now convert to JSON:

string json = JsonSerializer.Serialize(dev);

Output:

{"IMEI":"8611280","Latitude":26.85,"Longitude":80.94,"Battery":90}

Perfect for WebSocket or API.


🔵 6. PascalCase → camelCase Conversion (API standard)

API responses mostly camelCase में होने चाहिए:

Use:

var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

Now:

{"imei":"8611280","latitude":26.85}

🟣 7. Ignore NULL Values

options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;

JSON output clean हो जाता है।


🔥 8. Newtonsoft Features — Why still popular?

✔ Custom converters आसान

✔ Dynamic JSON parsing आसान

✔ Case-insensitive by default

✔ Complex nested JSON handle easily

Example: Dynamic JSON

var obj = JObject.Parse(json); string imei = obj["imei"].ToString();

System.Text.Json में इतना simple नहीं है।


🧠 9. Real-Life GPS Use Case: Unknown Packet → JSON Dump

var dump = new { Raw = packet, Time = DateTime.Now, Source = "G17" }; string json = JsonConvert.SerializeObject(dump, Formatting.Indented); File.AppendAllText("unknown.json", json + ",\n");

Great debugging tool.


📌 10. JSON Property Rename (Important for API)

System.Text.Json:

public class GPSDevice { [JsonPropertyName("lat")] public double Latitude { get; set; } }

Newtonsoft:

[JsonProperty("lat")] public double Latitude { get; set; }

🧪 11. Deserialize with Missing Fields (Newtonsoft Only)

Packet:

{"imei":"8611","lat":26.8}

Newtonsoft safe handle करेगा।
System.Text.Json fail कर सकता है unless configured.


⭐ Summary: कौन सा बेहतर है?

FeatureSystem.Text.JsonNewtonsoft
Speed⚡ FastestSlower
Feature-rich❌ Medium✔ Yes
Custom convertersHardEasy
Dynamic JSONHardVery easy
Best forHigh performance APIsComplex JSON systems
GPS server✔ Best✔ Good for complex parsing

🔚 Conclusion

JSON Handling आपके system को बनाता है:

✔ App ↔ Server communication structured
✔ Fast & reliable
✔ Parsing easy
✔ Debugging smooth
✔ GPS packets to WebSocket conversion simple
✔ API responses professional

System.Text.Json → सबसे fast
Newtonsoft.Json → सबसे flexible

GPS server में दोनों का combo best रहता है।

Post a Comment

0 Comments

Translate

Close Menu