Skip to content

ProductDto

Class public
A data transfer object for product information, annotated with System.Text.Json serialization attributes to control wire format.
Namespace SampleApi
Assembly SampleApi
Package v1.0.0.0
public class ProductDto
ProductDto demonstrates the most common JSON customization patterns supported by System.Text.Json:
  • JsonPropertyNameAttribute to map CLR property names to snake_case or camelCase JSON keys.
  • JsonIgnoreAttribute to hide internal-only properties from serialized output.
  • JsonRequiredAttribute to enforce that a property must be present during deserialization.
Properties marked with JsonRequiredAttribute will cause JsonSerializer to throw a JsonException if the corresponding key is missing from the input JSON.
DateTimeOffset
DateTimeOffset ProductDto.AddedAt

Gets or sets the UTC date and time when the product was first added to the catalogue. Maps to the JSON key "added_at".

Category Section titled Category nullable
string?
string? ProductDto.Category

Gets or sets the product category used for catalogue organization. Maps to the JSON key "category".

Description Section titled Description nullable
string?
string? ProductDto.Description

Gets or sets the long-form product description, supporting plain text only. Maps to the JSON key "description".

When null, the product has no description and UIs should display a placeholder or omit the section entirely.
Guid ProductDto.Id

Gets or sets the unique product identifier. This is required during deserialization and maps to the JSON key "id".

InternalTrackingCode Section titled InternalTrackingCode nullable
string?
string? ProductDto.InternalTrackingCode

An internal tracking code used by the warehouse management system. This property is excluded from JSON serialization via JsonIgnoreAttribute and is never exposed through the API.

bool ProductDto.IsAvailable

Gets or sets whether the product is currently available for purchase. Maps to the JSON key "available".

When false, the product should not appear in storefront listings but may still be visible in the admin catalogue.
string ProductDto.Name

Gets or sets the human-readable product name displayed in catalogues and search results. Maps to the JSON key "name".

decimal ProductDto.Price

Gets or sets the unit price in the store's base currency. Maps to the JSON key "price".

Prices are represented as Decimal to preserve precision in financial calculations. Negative values are not permitted.
string?
string? ProductDto.Sku

Gets or sets the stock keeping unit code for inventory tracking. Maps to the JSON key "sku".

SKU codes follow the format XX-#### where XX is a two-letter category prefix and #### is a numeric sequence.
List<string>
List<string> ProductDto.Tags

Gets or sets the list of searchable tags associated with this product. Maps to the JSON key "tags".

Tags enable faceted search and filtering. They should be lowercase, hyphen-separated strings (e.g., "usb-c", "wireless").

Serializing a product to JSON:

var product = new ProductDto
{
Id = Guid.NewGuid(),
Name = "Wireless Mouse",
Description = "Ergonomic wireless mouse with USB-C receiver",
Price = 29.99m,
Sku = "WM-1001",
Category = "Peripherals",
Tags = ["wireless", "ergonomic", "usb-c"],
IsAvailable = true
};
string json = JsonSerializer.Serialize(product);
// {"id":"...","name":"Wireless Mouse","description":"...","price":29.99,...}

Deserializing from JSON (required properties enforced):

string json = """
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "Keyboard",
"price": 59.99
}
""";
var product = JsonSerializer.Deserialize<ProductDto>(json);