Wiki source code of Методы API
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | (% class="root-extension__layer-composer" %) | ||
| 2 | ((( | ||
| 3 | |||
| 4 | ))) | ||
| 5 | |||
| 6 | (% class="lead" %) | ||
| 7 | Questions related to the work of API methods: | ||
| 8 | |||
| 9 | * {{showhide showmessage="Why do some API methods return the $type property?" hidemessage="Why do some API methods return the $type property?"}}|(% style="border-color:white" %)((( | ||
| 10 | (% class="box infomessage" %) | ||
| 11 | ((( | ||
| 12 | **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. | ||
| 13 | ))) | ||
| 14 | |||
| 15 | ((( | ||
| 16 | Objects used in the **.Net** framework are translated from a data structure to a sequence of bytes or a string. In this format it is convenient to transfer and store data. | ||
| 17 | |||
| 18 | (% class="box" %) | ||
| 19 | ((( | ||
| 20 | The process of translating an object state into a stream of bytes in order to store them in PC memory, a database or a file is called **serialization**. The main purpose of serialization is to save the state of an object so that it is possible to restore it if necessary. | ||
| 21 | The reverse translation of bytes into a data structure is called **deserialization**. | ||
| 22 | ))) | ||
| 23 | |||
| 24 | The Loymax system uses the **Newtonsoft.Json.JsonSerializer** for serialization/deserialization. | ||
| 25 | ))) | ||
| 26 | )))|(% style="border-color:white" %){{lightbox image="Group 1 (1).png"/}} | ||
| 27 | |||
| 28 | Let's take the serialization/deserialization process as 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">receiving a list of brands</a>{{/html}} | ||
| 29 | |||
| 30 | ((( | ||
| 31 | |||
| 32 | ))) | ||
| 33 | |||
| 34 | |(% style="border-color:white" %)((( | ||
| 35 | All brands in the System are created using the same template (model), that is, they are inherited from the same data type. | ||
| 36 | |||
| 37 | The algorithm for obtaining information about brands through API methods works as follows: | ||
| 38 | |||
| 39 | 1. The client sends a request for a list of brands (##GET: /v1.2/brands###). | ||
| 40 | 1. The server processes the request, generates and sends an SQL-query to the database to get a list of brands. | ||
| 41 | 1. The database, in response to a server request, returns to the server a list of brands that are translated from the byte stream back into a **.Net** object. | ||
| 42 | 1. The server returns to the client a list of brands in JSON format. The **Newtonsoft.Json.JsonSerializer **converts the **.Net** object into JSON format. | ||
| 43 | )))|(% style="border-color:white" %)[[image:BrandBase.png]] | ||
| 44 | |||
| 45 | For example, in **.NET** there are types **Brand1 ** and **BrandBase **(**Brand1 ** inherits from **BrandBase**). | ||
| 46 | |||
| 47 | (% class="box" %) | ||
| 48 | ((( | ||
| 49 | ##public class BrandBase | ||
| 50 | { | ||
| 51 | public string Name { get; set; } | ||
| 52 | public string Description { get; set; } | ||
| 53 | } | ||
| 54 | public class Brand1 : BrandBase | ||
| 55 | { | ||
| 56 | public string Code { get; set; } | ||
| 57 | }## | ||
| 58 | ))) | ||
| 59 | |||
| 60 | **Newtonsoft JsonConvert** adds **$type ** property to JSON schema for types during serialization and uses it during deserialization. | ||
| 61 | |||
| 62 | Thus, serializing an instance of the **Brand1 class ** will create a JSON object with the aforementioned **$type** property. | ||
| 63 | |||
| 64 | (% class="box infomessage" %) | ||
| 65 | ((( | ||
| 66 | **Brand1 **is an object created using the **BrandBase** type. Since the serializer expects to get only an object of type **BrandBase**, and **Brand1 **recognizes it as a separate type that inherits from the common type **BrandBase**, it assigns it the ##$type## property, which specifies the model that the object was created with, to create an object of the source type during deserialization. | ||
| 67 | ))) | ||
| 68 | |||
| 69 | In the response to the query for a list of brands, the information about **Brand1 **will look like this | ||
| 70 | |||
| 71 | (% class="box" %) | ||
| 72 | ((( | ||
| 73 | ##{## | ||
| 74 | |||
| 75 | ## "$type": "Loymax.Api.Models.BrandInfo.Brand1, Loymax.Api", | ||
| 76 | "name": "Brand1", | ||
| 77 | "description": "New Brand", | ||
| 78 | "code": "7878b563333841d79a5e5d14fde29cbe" | ||
| 79 | }## | ||
| 80 | ))){{/showhide}} | ||
| 81 | |||
| 82 | (% class="root-extension__layer-composer" %) | ||
| 83 | ((( | ||
| 84 | |||
| 85 | ))) |