### Example JSON:API Document Structure Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Provides a sample JSON:API document demonstrating the structure for articles, including attributes, relationships, and included resources (people, comments). This JSON is used as input for deserialization examples. ```json { "data": [{ "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" }, "relationships": { "author": { "data": { "type": "people", "id": "9" } }, "comments": { "data": [ { "type": "comments", "id": "5" }, { "type": "comments", "id": "12" } ] } } }], "included": [{ "type": "people", "id": "9", "attributes": { "first-name": "Dan", "last-name": "Gebhardt", "twitter": "dgeb" } }, { "type": "comments", "id": "5", "attributes": { "body": "First!" }, "relationships": { "author": { "data": { "type": "people", "id": "2" } } } }, { "type": "comments", "id": "12", "attributes": { "body": "I like XML better" }, "relationships": { "author": { "data": { "type": "people", "id": "9" } } } }] } ``` -------------------------------- ### JSON:API Document Root with Meta and Links Example Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md An example JSON:API document demonstrating the use of root-level properties like `jsonapi` version, `links`, and `meta` data. These properties can be accessed in C# via the `DocumentRoot` helper class. ```json { "jsonapi": { "version":"1.0" }, "links": { "self": "http://example.com/articles" }, "meta": { "created": "2017-04-02T23:28:35" }, "data": [{ "id" : "1", "type": "articles", "attributes": { "title": "document root example" } }] } ``` -------------------------------- ### Configure JsonApiSerializer for ASP.NET Core MVC Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Provides an example of configuring an ASP.NET Core MVC application to use `json:api` as the default serialization format. This involves replacing the standard `JsonInputFormatter` and `JsonOutputFormatter` with instances configured with `JsonApiSerializerSettings` within the `ConfigureServices` method. ```csharp public class Startup { // ... public void ConfigureServices(IServiceCollection services) { // ... var sp = services.BuildServiceProvider(); var logger = sp.GetService(); var objectPoolProvider = sp.GetService(); services.AddMvc(opt => { var serializerSettings = new JsonApiSerializerSettings(); var jsonApiFormatter = new JsonOutputFormatter(serializerSettings, ArrayPool.Shared); opt.OutputFormatters.RemoveType(); opt.OutputFormatters.Insert(0, jsonApiFormatter); var jsonApiInputFormatter = new JsonInputFormatter(logger.CreateLogger(), serializerSettings, ArrayPool.Shared, objectPoolProvider); opt.InputFormatters.RemoveType(); opt.InputFormatters.Insert(0, jsonApiInputFormatter); }); } } ``` -------------------------------- ### Basic Serialization and Deserialization with JsonApiSerializer Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Demonstrates the fundamental usage of JsonApiSerializerSettings with JsonConvert.SerializeObject and JsonConvert.DeserializeObject for converting Plain Old CLR Objects (POCOs) to and from the json:api format. ```csharp //To serialize a POCO in json:api format string json = JsonConvert.SerializeObject(articles, new JsonApiSerializerSettings()); //To deserialize to a POCO from json:api format Article[] articles = JsonConvert.DeserializeObject(json, new JsonApiSerializerSettings()); ``` -------------------------------- ### Apply Custom Converter Globally in JsonApiSerializerSettings Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Illustrates how to apply a custom `JsonConvertor` globally to all resource objects by passing an instance of the converter directly to the `JsonApiSerializerSettings` constructor during initialization. ```csharp var settings = new JsonApiSerializerSettings(new MyTypeDeterminingResourceObjectConvertor()) Article[] articles = JsonConvert.DeserializeObject(json, settings); ``` -------------------------------- ### Access JSON:API Document Root Properties in C# Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Shows how to deserialize a JSON:API document containing root-level `jsonapi`, `links`, and `meta` properties into a `DocumentRoot` object. It then demonstrates accessing these properties for validation or further processing. ```csharp DocumentRoot articlesRoot = JsonConvert.DeserializeObject>(json, new JsonApiSerializerSettings()); Assert.Equal("1.0", articlesRoot.JsonApi.Version); Assert.Equal("http://example.com/articles", articlesRoot.Links["self"].Href); Assert.Equal("2017-04-02T23:28:35", articlesRoot.Meta["created"]); ``` -------------------------------- ### Serialize C# Objects to JSON:API Format Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Demonstrates how to construct C# `Article`, `Comment`, and `Person` objects with relationships and then serialize them into the JSON:API format using `JsonConvert.SerializeObject` and `JsonApiSerializerSettings`. ```csharp var author = new Person { Id = "9", FirstName = "Dan", LastName = "Gebhardt", Twitter = "dgeb" }; var articles = new[] { new Article { Id = "1", Title = "JSON API paints my bikeshed!", Author = author, Comments = new List { new Comment { Id = "5", Body = "First!", Author = new Person { Id = "2" } }, new Comment { Id = "12", Body = "I like XML better", Author = author } } } }; //will produce the same json:api json value string json = JsonConvert.SerializeObject(articles, new JsonApiSerializerSettings()); ``` -------------------------------- ### Deserialize JSON:API to C# Article Array Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Shows how to deserialize the provided JSON:API content into an array of `Article` objects using `JsonConvert.DeserializeObject` and `JsonApiSerializerSettings`. This demonstrates converting a JSON:API document back into a C# object model. ```csharp Article[] articles = JsonConvert.DeserializeObject(json, new JsonApiSerializerSettings()); ``` -------------------------------- ### Custom JsonApiSerializer Converter for Type Determination Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Demonstrates how to create a custom `ResourceObjectConverter` by overriding the `CreateObject` method. This allows for dynamic object instantiation during deserialization based on the `jsonapiType` field, enabling support for interfaces or abstract classes. ```csharp public class MyTypeDeterminingResourceObjectConvertor : ResourceObjectConverter { protected override object CreateObject(Type objectType, string jsonapiType, JsonSerializer serializer) { switch (jsonapiType) { case "vip-person": return new PersonVIP(); case "person": return new Person(); default: return base.CreateObject(objectType, jsonapiType, serializer); } } } ``` -------------------------------- ### Add Custom Converter to JsonApiSerializerSettings Collection Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Shows an alternative method for adding a custom `JsonConvertor` by appending it to the `Converters` collection of `JsonApiSerializerSettings`. This approach allows for more granular control, especially when overriding the `CanConvert` method. ```csharp var settings = new JsonApiSerializerSettings() settings.Converters.Add(new MyTypeDeterminingResourceObjectConvertor()) Article[] articles = JsonConvert.DeserializeObject(json, settings); ``` -------------------------------- ### Storing JSON:API Link Values with Link and Links Classes Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md The `Link` class is used to store individual link values, normalizing both string and object link formats into an object model. The `Links` class provides a dictionary-like structure to store multiple links typically found on JSON:API objects. ```APIDOC Link: Purpose: Represents a single JSON:API link, normalizing string or object formats. Properties: Href: string (The URL of the link) Meta: object (Optional, additional metadata for the link) Links: Purpose: A collection (dictionary) of Link objects, typically used to store multiple links associated with a JSON:API object (e.g., 'self', 'related'). Inherits: Dictionary ``` -------------------------------- ### Define C# POCOs for JSON:API Serialization Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Illustrates the C# class definitions (Article, Comment, Person) used as Plain Old CLR Objects (POCOs) for serialization and deserialization with JsonApiSerializer. It shows how standard Json.NET attributes like JsonProperty can be used for property mapping. ```csharp public class Article { public string Id { get; set; } public string Title { get; set; } public Person Author { get; set; } public List Comments { get; set; } } public class Comment { public string Id { get; set; } public string Body { get; set; } public Person Author { get; set; } } public class Person { public string Id { get; set; } [JsonProperty(propertyName: "first-name")] //uses standard Json.NET attributes to control serialization public string FirstName { get; set; } [JsonProperty(propertyName: "last-name")] public string LastName { get; set; } public string Twitter { get; set; } } ``` -------------------------------- ### Apply Custom Converter via JsonConvertorAttribute on Model Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md Explains how to apply a custom `JsonConvertor` directly to a model property using the `JsonConvertorAttribute`. This method is similar to how `Json.NET` handles converter application at the property level. ```csharp [JsonConvertor(typeof(MyTypeDeterminingResourceObjectConvertor))] public IPerson Author {get; set;} ``` -------------------------------- ### Representing JSON:API Relationships in C# Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md The `Relationship` class allows managing additional JSON:API relationship details such as links and meta properties. This snippet demonstrates how to define a relationship in a C# model and access its associated links after deserialization. ```json { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" }, "relationships": { "author": { "links": { "self": "http://example.com/articles/1/relationships/author", "related": "http://example.com/articles/1/author" }, "data": { "type": "people", "id": "9" } } } } } ``` ```csharp public class Article { public string Id { get; set; } public string Title { get; set; } public Relationship Author { get; set; } } ``` ```csharp Article article = JsonConvert.DeserializeObject
(json, new JsonApiSerializerSettings()); Assert.Equal("http://example.com/articles/1/relationships/author", article.Author.links["self"].Href); Assert.Equal("http://example.com/articles/1/author", article.Author.links["related"].Href); ``` -------------------------------- ### Handling JSON:API Resource Identifier Metadata in C# Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md The JSON:API specification allows defining metadata at the resource identifier level. By default, this meta is folded into the object's meta. However, `ResourceIdentifier` can be used to distinguish between resource identifier meta and resource object meta, populating the `meta` property and placing resource object details in the `Value` field. ```json { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" }, "relationships": { "author": { "meta": { "verified": true }, "data": { "type": "people", "id": "9" } } } }, "included": [{ "type": "people", "id": "9", "attributes": { "first-name": "Dan", "last-name": "Gebhardt", "twitter": "dgeb" }, "meta": { "verified" : false } }] } ``` ```csharp public class Article { public string Id { get; set; } public string Title { get; set; } public ResourceIdentifier Author { get; set; } } ``` ```csharp Article article = JsonConvert.DeserializeObject
(json, new JsonApiSerializerSettings()); Assert.Equal(true, article.Author.meta["verified"]); //resource identifier meta Assert.Equal(true, article.Author.Value.meta["verified"]); //object meta ``` -------------------------------- ### Overriding Relationship Object Detection with Custom Converters in C# Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md JsonApiSerializer by default considers any class with an 'Id' property a Resource Object. This default behavior can be customized by providing a custom `JsonConverter` (preferably extending `ResourceObjectConverter`) during `JsonApiSerializerSettings` initialization. The custom converter can override the `CanConvert(Type type)` method to define custom logic for identifying resource objects. ```csharp var settings = new JsonApiSerializerSettings(new MyOwnJsonSerializer()); Article[] articles = JsonConvert.DeserializeObject(json, settings); ``` -------------------------------- ### Customizing JSON:API Type Property in C# Models Source: https://github.com/codecutout/jsonapiserializer/blob/master/README.md By default, JsonApiSerializer uses the class name as the JSON:API type property. This behavior can be overridden by simply adding a `Type` property to your C# class and assigning a desired string value, allowing explicit control over the serialized type name. ```csharp public class Person { public string Type { get; set; } = "people"; //sets type to "people" public string Id { get; set; } [JsonProperty(propertyName: "first-name")] public string FirstName { get; set; } [JsonProperty(propertyName: "last-name")] public string LastName { get; set; } public string Twitter { get; set; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.