Wiki source code of API methods
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | (% class="lead" %) |
| |
4.1 | 2 | Questions related to API methods: |
| |
1.1 | 3 | |
| |
4.1 | 4 | * {{showhide showmessage="Why do some API methods return the $type property in their responses?" hidemessage="Why do some API methods return the $type property in their responses?"}}|(% style="border-color:white" %)((( |
| |
1.1 | 5 | (% class="box infomessage" %) |
| 6 | ((( | ||
| |
5.1 | 7 | **Note:** It is reccomended to read about{{html}}<a href="https://docs.loymax.net/xwiki/bin/view/Main/General_information/Used_technologies/">the technology stack</a>{{/html}} used by the Loymax System. |
| |
1.1 | 8 | ))) |
| 9 | |||
| 10 | ((( | ||
| |
5.1 | 11 | Objects used in the **.Net** framework are converted from a data structure to a sequence of bytes or a string. This format makes it easier to transfer and store data. |
| |
1.1 | 12 | |
| 13 | (% class="box" %) | ||
| 14 | ((( | ||
| |
5.1 | 15 | The process of converting an object state into a stream of bytes in order to store them in the PC memory, database or file is called **serialization**. The main purpose of serialization is to save the state of an object so that it can be restored if necessary. |
| 16 | The reverse conversion of bytes into a data structure is called **deserialization**. | ||
| |
1.1 | 17 | ))) |
| 18 | |||
| |
5.1 | 19 | The Loymax System uses the **Newtonsoft.Json.JsonSerializer** for serialization/deserialization. |
| |
1.1 | 20 | ))) |
| |
5.1 | 21 | )))|(% style="border-color:white" %){{lightbox image="Serialization.png"/}} |
| |
1.1 | 22 | |
| |
5.1 | 23 | Let's consider the serialization/deserialization process using an example of how the method works for {{html}}<a href="https://docs.loymax.net/xwiki/bin/view/Main/Integration/Ways_to_use_API/API_methods/Methods_of_public_api/Brands/#H41F43E43B44344743543D43843544143F43844143A43043144043543D43443E432">to get a list of brands</a>{{/html}} |
| |
1.1 | 24 | |
| 25 | ((( | ||
| 26 | |||
| 27 | ))) | ||
| 28 | |||
| 29 | |(% style="border-color:white" %)((( | ||
| |
5.1 | 30 | All brands in the System are created using the same template (model), i.e. they are derived from the same data type. |
| |
1.1 | 31 | |
| 32 | The algorithm for obtaining information about brands through API methods works as follows: | ||
| 33 | |||
| |
5.1 | 34 | 1. The customer sends a request for a list of brands (##GET: /v1.2/brands###). |
| 35 | 1. The server processes the request, generates and sends an SQL query to the database to get the list of brands. | ||
| 36 | 1. The database responds to the server's request by returning a list of brands that are coverted from a byte stream back to a **.Net** object. | ||
| 37 | 1. The server returns a list of brands to the customer in JSON format. The **Newtonsoft.Json.JsonSerializer **converts the **.Net** object into JSON format. | ||
| |
1.1 | 38 | )))|(% style="border-color:white" %)[[image:BrandBase.png]] |
| 39 | |||
| |
5.1 | 40 | For example, **.NET** contains **Brand1 ** and **BrandBase** types (**Brand1 ** is inherited from **BrandBase**). |
| |
1.1 | 41 | |
| 42 | (% class="box" %) | ||
| 43 | ((( | ||
| 44 | ##public class BrandBase | ||
| 45 | { | ||
| 46 | public string Name { get; set; } | ||
| 47 | public string Description { get; set; } | ||
| 48 | } | ||
| 49 | public class Brand1 : BrandBase | ||
| 50 | { | ||
| 51 | public string Code { get; set; } | ||
| 52 | }## | ||
| 53 | ))) | ||
| 54 | |||
| |
5.1 | 55 | **Newtonsoft JsonConvert** adds the **$type ** property to JSON schema for types during serialization and uses it during deserialization. |
| |
1.1 | 56 | |
| |
5.1 | 57 | Thus, serialization of the **Brand1** class instance will create a JSON object with the above mentioned **$type** property. |
| |
1.1 | 58 | |
| 59 | (% class="box infomessage" %) | ||
| 60 | ((( | ||
| |
5.1 | 61 | **Brand1** is an object created using the **BrandBase** type. Since the serializer expects to get only the object of the **BrandBase** type, and **Brand1** is recognized as a separate type that is inherited from the common **BrandBase** type, it assigns the ##$type## property to it, which specifies the model according to which the object was created, in order to create an object of the original type during deserialization. |
| |
1.1 | 62 | ))) |
| 63 | |||
| |
5.1 | 64 | The response to a request to get a list of brands will contain the following information about **Brand1**: |
| |
1.1 | 65 | |
| 66 | (% class="box" %) | ||
| 67 | ((( | ||
| 68 | ##{## | ||
| 69 | |||
| 70 | ## "$type": "Loymax.Api.Models.BrandInfo.Brand1, Loymax.Api", | ||
| 71 | "name": "Brand1", | ||
| 72 | "description": "New Brand", | ||
| 73 | "code": "7878b563333841d79a5e5d14fde29cbe" | ||
| 74 | }## | ||
| 75 | ))){{/showhide}} |