class, record या struct से बने C# models पेस्ट करें और System.Text.Json व Newtonsoft.Json के नियमों के अनुसार एक उदाहरण JSON document पाएं। ASP.NET Core की default camelCase naming policy या लिखे गए property नामों में से चुनें; [JsonPropertyName] या [JsonProperty] से सेट किए गए नाम हमेशा प्राथमिकता पाते हैं।
इसे Web API request/response models से frontend या बाहरी partner को sample देने, Swagger (Swashbuckle) examples भरने, या HttpClient integration tests के लिए bodies तैयार करने में इस्तेमाल करें।
using, namespace, constructors और methods रह सकते हैं।नामकरण नीति चुनें: सामान्य Web API projects के लिए camelCase (ASP.NET Core डिफ़ॉल्ट), या अगर आपने PropertyNamingPolicy = null सेट किया है या Newtonsoft defaults पर निर्भर हैं तो प्रॉपर्टी नाम जैसे हैं।रूट टाइप को स्वचालित पर रखने पर, वह पहला type root बनता है जिसे कोई दूसरा model reference नहीं करता; ज़रूरत हो तो सूची से बदलें।.http फ़ाइल या xUnit request body में कॉपी करें। आपके चुने गए options URL में सेव रहते हैं।public class InventoryItem
{
public Guid Id { get; set; }
public string SKU { get; set; } = "";
[JsonPropertyName("stock_qty")]
public int StockQuantity { get; set; }
public StockState State { get; set; } = StockState.InStock;
public DateTimeOffset UpdatedAt { get; set; }
public List<string> Barcodes { get; set; } = new();
[JsonIgnore]
public string WarehouseNote { get; set; } = "";
private int _version;
}
public enum StockState { InStock, LowStock, OutOfStock }{
"id": "00000000-0000-0000-0000-000000000000",
"sku": "",
"stock_qty": 0,
"state": 0,
"updatedAt": "1970-01-01T00:00:00+00:00",
"barcodes": [
""
]
}camelCase policy में SKU जैसा पूरा कैपिटल नाम sku बन जाता है, जो System.Text.Json के JsonNamingPolicy.CamelCase से मेल खाता है, जबकि [JsonPropertyName("stock_qty")] policy चाहे जो हो अपना नाम रखता है। WarehouseNote ([JsonIgnore]) और private field _version शामिल नहीं होते। Enum State पर कोई string converter नहीं है, इसलिए वह 0 दिखाता है, जो उसके initializer StockState.InStock की numeric वैल्यू है।
[JsonPropertyName], Newtonsoft [JsonProperty], और [DataMember(Name = ...)]।public properties और fields शामिल होते हैं, साथ में [JsonInclude] लगे non-public members। static, const, [JsonIgnore] और [IgnoreDataMember] बाहर रहते हैं; Condition = WhenWritingNull जैसे conditional ignores वाले members रखे जाते हैं।record Customer(long Id, string Name)) और [property: JsonPropertyName(...)] targets समर्थित हैं।=> ...) शामिल होती हैं, जैसे System.Text.Json उन्हें serialize करेगा।= 10 जैसी explicit values समेत)। Enum या property पर [JsonConverter(typeof(JsonStringEnumConverter))] (या Newtonsoft StringEnumConverter) हो, या members पर [JsonStringEnumMemberName] / [EnumMember(Value = ...)] हो, तो नाम वाली string इस्तेमाल होती है।DateTime → 1970-01-01T00:00:00, DateTimeOffset → 1970-01-01T00:00:00+00:00, DateOnly → 1970-01-01, TimeOnly/TimeSpan → 00:00:00, Guid → पूरी तरह zero GUID।int?/Nullable<T> अंदर वाला type इस्तेमाल करते हैं, List<T>/IEnumerable<T>/arrays को एक element मिलता है, Dictionary<int, V> में "0" key होती है, और object, JsonElement व JObject null बनते हैं।= 1.5, = "KRW") sample values के रूप में इस्तेमाल होते हैं।[JsonPropertyName] और default naming को मिलाने वाले DTOs में Swagger UI के बनाए examples को गलत पढ़ना आसान है। इस आउटपुट को ISchemaFilter या XML <example> comment में इस्तेमाल करें, ताकि docs और response में एक ही keys हों।
Migration के बाद key casing कैसे बदलेगी, यह पहले से देखने के लिए उसी model पर नामकरण नीति को आगे-पीछे बदलें।
जब client और server किसी model की अलग-अलग कॉपी रखते हैं, तो दोनों तरफ़ से JSON बनाकर compare करें और key नामों का mismatch जल्दी पकड़ें।
Program.cs में AddJsonOptions से globally register किया गया JsonStringEnumConverter model कोड में दिखता नहीं, इसलिए enums फिर भी संख्याओं के रूप में आते हैं। नाम देखने के लिए पेस्ट करने से पहले enum declaration पर [JsonConverter(typeof(JsonStringEnumConverter))] जोड़ें।string? जैसे nullable reference types को null नहीं, typed sample मिलता है।[JsonConverter] attributes, JsonSerializerOptions का DefaultIgnoreCondition, और snake_case policies (JsonNamingPolicy.SnakeCaseLower) लागू नहीं होते।[JsonPolymorphic]/[JsonDerivedType] से polymorphism समर्थित नहीं है; सिर्फ़ declared base type बनाया जाता है।नामकरण नीति का कौन सा विकल्प कब इस्तेमाल करूं?ASP.NET Core Web API JsonSerializerDefaults.Web इस्तेमाल करता है, जो camelCase है। बिना options के JsonSerializer.Serialize call करने वाला console app, या default Newtonsoft.Json settings, property नाम वैसे ही रखते हैं, इसलिए वहां प्रॉपर्टी नाम जैसे हैं चुनें।
public string FullName => ... जैसी computed property क्यों दिखती है?System.Text.Json और Newtonsoft.Json दोनों getter वाली public properties को serialize करते हैं, इसलिए computed properties भी responses में पहुंच जाती हैं। अगर आप उन्हें export नहीं करना चाहते तो [JsonIgnore] जोड़ें।
[JsonPropertyName] runtime पर key rename नहीं करता।Target के बिना compiler attribute को सिर्फ़ constructor parameter पर लगाता है। यह टूल दोनों रूप पहचानता है, लेकिन serialized property को भरोसेमंद तरीके से rename करने के लिए [property: JsonPropertyName("...")] लिखें।
DateTime values पर Z क्यों नहीं है?DateTimeKind.Unspecified वाला DateTime बिना offset के serialize होता है। अगर explicit UTC offset चाहिए, तो DateTimeOffset +00:00 रूप बनाता है।