Serialization of Enterprise applied types to JSON

We talked about the truth that the platform implements instruments for working with JSON in the past. Streaming writing/studying and serialization of primitive varieties and collections.

We’re glad to inform you that now we have automated working with JSON and carried out the 3rd group of instruments within the platform. They mean you can serialize 1C: Enterprise software varieties into JSON: hyperlinks, objects, recordsets and, usually, any types for which XDTO serialization is supported. After all, we additionally supplied the reverse operation – deserialization.

Why is it wanted

Initially, we suggest utilizing XDTO serialization to JSON when exchanging information between two 1C: Enterprise software options. In comparison with XML, JSON is extra compact, serialization / deserialization to / from JSON is quicker. As well as, now we have taken further measures intending to cut back the quantity of transmitted information.

Along with alternate between 1C: Enterprise purposes, this mechanism will also be replaced with exterior methods that might be prepared to accept 1C: Enterprise information varieties. For instance, XDTO serialization to JSON can prepare a customized HTTP interface for a software resolution. A service on the 1C: Enterprise platform will generate a response in reminiscence as a JSON string. After which, ship it utilizing the HTTPServiceResponse object. The serialization mechanism now we have carried out is compliant with the JSON customary. Subsequently, the exterior system mustn’t expertise any issue in deserializing.

Utilizing XDTO serialization in JSON for different duties appears unlikely to us. As a result, if an external system shouldn’t work with 1C: Enterprise software varieties, why switch them to it? And in the event, you intend to alternate solely primitive types and collections, then the ReadJSON () and WriteJSON () strategies, which we talked about earlier, are nicely suited to this process.

The way it appears to be like

The best code that serializes catalogue merchandise may appear to be this:

The consequence shall be a JSON file with the following content material:

{
	"#kind": "jcfg:CatalogObject.Контрагенты",
	"#worth": {
		"IsFolder": false,
		"Ref": "3d710076-8d04-11dc-8ca0-000d8843cd1b",
		"DeletionMark": false,
		"Mother or father": "9d5c422c-8c4c-11db-a9b0-00055d49b45e",
		"Code": "000000012",
		"Description": "Мосхлеб ОАО",
		"Регион": "9d5c422f-8c4c-11db-a9b0-00055d49b45e",
		"Индекс": "456789",
		"Страна": "Россия",
		"Город": "Москва",
		"Улица": "Петровка",
		"Дом": "12",
		"Телефон": "+7(999)234-78-64",
		"ЭлектроннаяПочта": "[email protected]",
		"Факс": "",
		"ВебСайт": "",
		"ВидЦен": "9d5c4225-8c4c-11db-a9b0-00055d49b45e",
		"ДополнительнаяИнформация": "Поставка хлеба",
		"КонтактноеЛицо": "Громышева П.Р.",
		"Широта": 55.762744,
		"Долгота": 37.618102
	}
}

You possibly can deserialize such a file with the following code:

Consequently, you’ll obtain a component of the Counterparties listing with the title Moskhleb OJSC and the code 000000012.

Serialization options

As you could have seen, serialization/deserialization is carried out utilizing the XDTO Serializer object. Subsequently, the ensuing doc construction has XML’s “generic options”, and JSON is obtained “comparable” to XML. This has a damaging impact on each the “readability” and the compactness of the file. It is a form of “cost” for the comfort obtained from the usage of XDTO. If the “purity” of the following JSON is vital to you, we suggest utilizing the instruments that we discussed earlier.

The final ideas of JSON serialization are identical to XDTO XML serialization. Particularly:

  • The info construction corresponds to the structure of an XML doc;
  • There are minor variations associated with the storage of varieties and the illustration of arrays in JSON;
  • The order and composition of the JSON properties follow the construction of the configuration object and can’t be modified.

Moreover, there’s one vital characteristic. Unlike XML, you don’t want to supply a reputation to document the beginning of a JSON object. Because of this, JSON serialization/deserialization might be executed in certainly one of two methods.

Or, when serializing, you specify a specific kind of project after which deserialize without selecting the category of the learn worth:

Alternatively, you’re serializing to JSON without a specific kind of project. However, then the type will have to be specified throughout deserialization:

One other characteristic of serialization is that we tried to optimize the scale of the following file. Subsequently, namespaces aren’t entirely written in a separate JSON property for built-in platform varieties. However, they are prefixed earlier than the kind:

In this case, the prefix jcfg denotes the namespace http://v8.1c.ru/8.1/data/enterprise/current-config… Matching prefixes to namespaces is hardcoded into the platform. Subsequently, there are no difficulties with kind definitions when exchanging between 1C: Enterprise purposes.

And when exchanging with exterior methods, the external system will want a whole correspondence desk for the proper deserialization of platform varieties. There’s such a desk, and it’s printed within the documentation.

Deserialization options

The final ideas of deserialization are also associated with using the XDTO mechanism. Particularly:

  • Solely these varieties that help XDTO serialization are deserialized;
  • The order of the JSON properties should match the construction of the configuration object.

When deserializing, you have got the flexibility to vary the obtained information. To do that, as with primitive varieties and collections, you should use the restore perform.

This functionality could also be required, for instance, to change references to associated objects when transferring information from one software resolution to a different so that after the switch, this information is already referred to the things of the brand new database. Or, for instance, to take away info from the info that’s significant solely within the supply database.

For instance, the product description is saved in HTML within the supply database. And in your database, a textual description is sufficient, or such an outline shouldn’t be wanted in any respect. As a result of you’ll have your report.

Then, having a file with the following content material:

{
	"#kind": "jcfg:CatalogObject.Товары",
	"#worth": {
		"IsFolder": false,
		"Ref": "2nd97a749-8ea9-11dc-8ca1-000d8843cd1b",
		"DeletionMark": false,
		"Mother or father": "87f1c226-9679-11db-a9b2-00055d49b45e",
		"Code": "000000030",
		"Description": "Колбаса",
		"Артикул": "Kol67",
		"Поставщик": "95f19911-d7c6-11db-a9bd-00055d49b45e",
		"ФайлКартинки": "00000000-0000-0000-0000-000000000000",
		"Вид": "Товар",
		"Штрихкод": "",
		"Описание":"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.Zero Transitional//EN">
			<html>
				<head>
					<meta title="viewport" content material="initial-scale=1.0, width=device-width"></meta>
					<meta http-equiv="Content material-Sort" content material="textual content/html; charset=utf-8"></meta>
					<meta http-equiv="X-UA-Suitable" content material="IE=EmulateIE7"></meta>
					<meta title="format-detection" content material="phone=no"></meta>
					<model kind="textual content/css">n
						physique{margin:0px;padding:8px;overflow:auto;width:100%;top:100%;}n
						p{line-height:1.15;margin:0px;}n
						ol,ul{margin-top:0px;margin-bottom:0px;}n
						img{border: none;}n
					</model>
				</head>
				<physique>n
					<p>Колбаса "Докторская" вареная высшего сорта из охлажденного мяса</p>n
					<p><span model=" font-weight: daring;">Вес - </span> 500 г.</p>n
				</physique>
			</html>"
	}
}

it would help if you used your restore perform when deserializing, through which you clear the Description property:

On account of this code, the ReferenceObject.Merchandise object shall be learned, for which the Description attribute will comprise an empty string:

Facebook
Twitter
Pinterest
WhatsApp

Related Articles

Latest Articles