Wiki source code of Методы API
Hide last authors
author | version | line-number | content |
---|---|---|---|
|
48.1 | 1 | (% class="root-extension__layer-composer" %) |
2 | ((( | ||
3 | |||
4 | ))) | ||
5 | |||
![]() |
2.1 | 6 | (% class="lead" %) |
|
48.1 | 7 | Questions related to the work of API methods: |
![]() |
6.2 | 8 | |
|
48.1 | 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" %)((( |
![]() |
28.2 | 10 | (% class="box infomessage" %) |
11 | ((( | ||
|
48.1 | 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. |
![]() |
28.2 | 13 | ))) |
![]() |
6.2 | 14 | |
![]() |
28.2 | 15 | ((( |
|
48.1 | 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. |
![]() |
7.1 | 17 | |
![]() |
28.2 | 18 | (% class="box" %) |
19 | ((( | ||
|
48.1 | 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**. | ||
![]() |
28.2 | 22 | ))) |
23 | |||
|
48.1 | 24 | The Loymax system uses the **Newtonsoft.Json.JsonSerializer** for serialization/deserialization. |
![]() |
29.1 | 25 | ))) |
![]() |
43.1 | 26 | )))|(% style="border-color:white" %){{lightbox image="Group 1 (1).png"/}} |
![]() |
28.2 | 27 | |
|
48.1 | 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}} |
![]() |
28.2 | 29 | |
![]() |
43.1 | 30 | ((( |
31 | |||
32 | ))) | ||
33 | |||
34 | |(% style="border-color:white" %)((( | ||
|
48.1 | 35 | All brands in the System are created using the same template (model), that is, they are inherited from the same data type. |
![]() |
28.2 | 36 | |
|
48.1 | 37 | The algorithm for obtaining information about brands through API methods works as follows: |
![]() |
35.1 | 38 | |
|
48.1 | 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.1 | 43 | )))|(% style="border-color:white" %)[[image:BrandBase.png]] |
![]() |
28.2 | 44 | |
|
48.1 | 45 | For example, in **.NET** there are types **Brand1 ** and **BrandBase **(**Brand1 ** inherits from **BrandBase**). |
![]() |
7.1 | 46 | |
47 | (% class="box" %) | ||
48 | ((( | ||
![]() |
43.1 | 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; } | ||
![]() |
28.3 | 57 | }## |
![]() |
43.1 | 58 | ))) |
![]() |
17.1 | 59 | |
|
48.1 | 60 | **Newtonsoft JsonConvert** adds **$type ** property to JSON schema for types during serialization and uses it during deserialization. |
![]() |
20.1 | 61 | |
|
48.1 | 62 | Thus, serializing an instance of the **Brand1 class ** will create a JSON object with the aforementioned **$type** property. |
![]() |
43.1 | 63 | |
64 | (% class="box infomessage" %) | ||
65 | ((( | ||
|
48.1 | 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. |
![]() |
43.1 | 67 | ))) |
68 | |||
|
48.1 | 69 | In the response to the query for a list of brands, the information about **Brand1 **will look like this |
![]() |
43.1 | 70 | |
71 | (% class="box" %) | ||
72 | ((( | ||
73 | ##{## | ||
74 | |||
75 | ## "$type": "Loymax.Api.Models.BrandInfo.Brand1, Loymax.Api", | ||
76 | "name": "Brand1", | ||
|
48.1 | 77 | "description": "New Brand", |
![]() |
43.1 | 78 | "code": "7878b563333841d79a5e5d14fde29cbe" |
79 | }## | ||
80 | ))){{/showhide}} | ||
|
48.1 | 81 | |
82 | (% class="root-extension__layer-composer" %) | ||
83 | ((( | ||
84 | |||
85 | ))) |