(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 भी देखेंगे।
JSON =
Human-readable data format
जिससे server ↔ app data exchange करते हैं।
Example GPS JSON:
{
"imei": "861128068064267",
"lat": 26.85,
"lon": 80.94,
"speed": 45
}
Using directives:
using System.Text.Json;
string json = JsonSerializer.Serialize(device);
GPSDevice dev = JsonSerializer.Deserialize<GPSDevice>(json);
✔ Fastest
✔ Built-in
✔ Low memory usage
❌ Missing some advanced features
using Newtonsoft.Json;
string json = JsonConvert.SerializeObject(device);
var dev = JsonConvert.DeserializeObject<GPSDevice>(json);
✔ Very flexible
✔ Tons of features
✔ Best for complex JSON
❌ Slower than System.Text.Json
class GPSDevice
{
public string IMEI { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public int Battery { get; set; }
}
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.
API responses mostly camelCase में होने चाहिए:
Use:
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
Now:
{"imei":"8611280","latitude":26.85}
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
JSON output clean हो जाता है।
Example: Dynamic JSON
var obj = JObject.Parse(json);
string imei = obj["imei"].ToString();
System.Text.Json में इतना simple नहीं है।
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.
System.Text.Json:
public class GPSDevice
{
[JsonPropertyName("lat")]
public double Latitude { get; set; }
}
Newtonsoft:
[JsonProperty("lat")]
public double Latitude { get; set; }
Packet:
{"imei":"8611","lat":26.8}
Newtonsoft safe handle करेगा।
System.Text.Json fail कर सकता है unless configured.
| Feature | System.Text.Json | Newtonsoft |
|---|---|---|
| Speed | ⚡ Fastest | Slower |
| Feature-rich | ❌ Medium | ✔ Yes |
| Custom converters | Hard | Easy |
| Dynamic JSON | Hard | Very easy |
| Best for | High performance APIs | Complex JSON systems |
| GPS server | ✔ Best | ✔ Good for complex parsing |
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 रहता है।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav