I
I
ivanich2742019-08-28 15:25:16
PHP
ivanich274, 2019-08-28 15:25:16

What is this data format and how to parse it using PHP?

The format is similar to serialized JSON, but it doesn't look like it. This format is associated with 1C.
I need to parse it using PHP.

26.08.2019 8:02:12--26.08.2019 8:02:12 Начало пакета _________

{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{14,{{"S","type"},{"S","order_change"}},{{"S","agent"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{5,{{"S","id"},{"S","42318"}},{{"S","id_1c"},{"S","ПР10"}},{{"S","id_crm"},{"S","00-00164917"}},{{"S","name"},{"S","Иванова Тест"}},{{"S","contact"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{3,{{"S","email"},{"S","[email protected]"}},{{"S","phone"},{"S","790000000"}},{{"S","contact_person"},{"S",""}}}}}}}},{{"S","id"},{"S","264762"}},{{"S","id_1c"},{"S","ЗА00-000000133586"}},{{"S","id_crm"},{"S","ЗА00-000000133586"}},{{"S","date"},{"S","2019-08-26"}},{{"S","time"},{"S","7:56:54"}},{{"S","amount"},{"N",215}},{{"S","delivery_date"},{"S","2019-08-26"}},{{"S","delivery_interval"},{"S","1-21-22"}},{{"S","items"},{"#",51e7a0d2-530b-11d4-b98a-008048da3034,{1,{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{9,{{"S","id_1c"},{"S","11014"}},{{"S","price"},{"N",215}},{{"S","quantity"},{"N",1}},{{"S","vat_rate"},{"N",0}},{{"S","name"},{"S","Вода 19л Классика"}},{{"S","discount_price"},{"S","0.00"}},{{"S","measure_code"},{"S","868"}},{{"S","measure_name"},{"S","бут"}},{{"S","type"},{"S","Товар"}}}}}}},{{"S","properties_values"},{"#",51e7a0d2-530b-11d4-b98a-008048da3034,{6,{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Отменен"}},{{"S","value"},{"B",0}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","ПометкаУдаления"}},{{"S","value"},{"B",0}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Финальный статус"}},{{"S","value"},{"B",0}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Статус заказа"}},{{"S","value"},{"S","[N] Принят"}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Статус заказа ИД"}},{{"S","value"},{"S","N"}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Дата изменения статуса"}},{{"S","value"},{"D",20190826075719}}}}}}},{{"S","storage"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","id_1c"},{"S","2"}},{{"S","name"},{"S","Благодатская"}}}}},{{"S","forwarder_group"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{0}}}}}

 ________Конец пакета__________

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kovalsky, 2019-08-29
@ivanich274

Hmm, how interesting. Perhaps the first thing to do is to define the terminology. There will be a lot of tautologies in my description, but if approached from the point of view of JS terminology, then everything will be clear:
1. An element is an independent substring, if necessary, separated from another comma, and enclosed in quotes if it does not describe an object or array,
2. An element starting with a curly bracket and ending with it can be called an object if the first element is the number of values ​​in the form of a number, or an array if, in addition to the first condition, all its child elements also describe objects (no matter how strange it may sound). Also, the second element in the array is some obscure fixed-length key (36), which is the same in the vast majority of cases, but sometimes still different. Intrigue.
3. Each value is described by two elements: the first describes the type, the second - a specific value (sorry for the tautology). There are 5 types: '#' - array, 'S' - string, 'N' - number, 'B' - boolean, 'D' - date, unix timestamp.
4. Properties of objects are described as follows: the first object describes the key of the property, and the second - its value,

initial_string = `{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{14,{{"S","type"},{"S","order_change"}},{{"S","agent"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{5,{{"S","id"},{"S","42318"}},{{"S","id_1c"},{"S","ПР10"}},{{"S","id_crm"},{"S","00-00164917"}},{{"S","name"},{"S","Иванова Тест"}},{{"S","contact"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{3,{{"S","email"},{"S","[email protected]"}},{{"S","phone"},{"S","790000000"}},{{"S","contact_person"},{"S",""}}}}}}}},{{"S","id"},{"S","264762"}},{{"S","id_1c"},{"S","ЗА00-000000133586"}},{{"S","id_crm"},{"S","ЗА00-000000133586"}},{{"S","date"},{"S","2019-08-26"}},{{"S","time"},{"S","7:56:54"}},{{"S","amount"},{"N",215}},{{"S","delivery_date"},{"S","2019-08-26"}},{{"S","delivery_interval"},{"S","1-21-22"}},{{"S","items"},{"#",51e7a0d2-530b-11d4-b98a-008048da3034,{1,{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{9,{{"S","id_1c"},{"S","11014"}},{{"S","price"},{"N",215}},{{"S","quantity"},{"N",1}},{{"S","vat_rate"},{"N",0}},{{"S","name"},{"S","Вода 19л Классика"}},{{"S","discount_price"},{"S","0.00"}},{{"S","measure_code"},{"S","868"}},{{"S","measure_name"},{"S","бут"}},{{"S","type"},{"S","Товар"}}}}}}},{{"S","properties_values"},{"#",51e7a0d2-530b-11d4-b98a-008048da3034,{6,{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Отменен"}},{{"S","value"},{"B",0}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","ПометкаУдаления"}},{{"S","value"},{"B",0}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Финальный статус"}},{{"S","value"},{"B",0}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Статус заказа"}},{{"S","value"},{"S","[N] Принят"}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Статус заказа ИД"}},{{"S","value"},{"S","N"}}}},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","name"},{"S","Дата изменения статуса"}},{{"S","value"},{"D",20190826075719}}}}}}},{{"S","storage"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{2,{{"S","id_1c"},{"S","2"}},{{"S","name"},{"S","Благодатская"}}}}},{{"S","forwarder_group"},{"#",4238019d-7e49-4fc9-91db-b6b951d5cf8e,{0}}}}}`;

// для удобства меняем все скобки на квадратные и оборачиваем ключи кавычками
processed_string = initial_string
  .replace(/{/g, '[').replace(/}/g, ']')
  .replace(/[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/gi, '"$&"');

main_sequence = eval(processed_string);

const parseThisFreakinOdinAssObject = (function() {
  const registered_types = [ '#', 'S', 'N', 'B', 'D' ];

  function processValue(type, value) {
    switch (type) {
      case 'S': {
        value = value.toString();
        break;
      }
      case 'N':
      case 'D': {
        value = Number(value);
        break;
      }
      case 'B': {
        value = Boolean(value);
        break;
      }
      case '#': {
        if (value) {
          const	declared_length = value[0],
              actual_length = value.length - 1;

          if (declared_length !== actual_length) {
            throw new Error(`The array actual length ${ actual_length } differs from declared length ${ declared_length }!`);
          }

          value = value.slice(1);
        } else {
          value = [];
        }
      }
    }

    return value;
  }

  return function parse(sequence) {
    const type = sequence[0];

    if (!registered_types.includes(type)) {
      throw new Error(`Type '${ type }' is not registered!'`);
    }

    // есл значение - массив, то пропускаем волшебный ключ
    let value = processValue(type, type === '#' ? sequence[2] : sequence[1]);

    if (value instanceof Array) {
      let temp_value;

      // если дочерний элемент также массив, то значит текущий элемент не объект, а массив
      if (value[0] && value[0][0] === '#') {
        temp_value = [];

        value.forEach(entry => {
          temp_value.push( parse(entry) );
        });
      } else {
        temp_value = {};
        value.forEach(entry => {
          temp_value[ parse(entry[0]) ] = parse(entry[1]);
        });
      }

      value = temp_value;
    }

    return value;
  }
})();

parseThisFreakinOdinAssObject(main_sequence);

{
    "type": "order_change",
    "agent": {
        "id": "42318",
        "id_1c": "ПР10",
        "id_crm": "00-00164917",
        "name": "Иванова Тест",
        "contact": {
            "email": "[email protected]",
            "phone": "790000000",
            "contact_person": ""
        }
    },
    "id": "264762",
    "id_1c": "ЗА00-000000133586",
    "id_crm": "ЗА00-000000133586",
    "date": "2019-08-26",
    "time": "7:56:54",
    "amount": 215,
    "delivery_date": "2019-08-26",
    "delivery_interval": "1-21-22",
    "items": [
        {
            "id_1c": "11014",
            "price": 215,
            "quantity": 1,
            "vat_rate": 0,
            "name": "Вода 19л Классика",
            "discount_price": "0.00",
            "measure_code": "868",
            "measure_name": "бут",
            "type": "Товар"
        }
    ],
    "properties_values": [
        {
            "name": "Отменен",
            "value": false
        },
        {
            "name": "ПометкаУдаления",
            "value": false
        },
        {
            "name": "Финальный статус",
            "value": false
        },
        {
            "name": "Статус заказа",
            "value": "[N] Принят"
        },
        {
            "name": "Статус заказа ИД",
            "value": "N"
        },
        {
            "name": "Дата изменения статуса",
            "value": 20190826075719
        }
    ],
    "storage": {
        "id_1c": "2",
        "name": "Благодатская"
    },
    "forwarder_group": {}
}

D
Denis Melnikov, 2019-08-28
@Mi11er

This is 1st shnyaga.
You aside
ValueFromFile (ValueFromFile)
Syntax: ValueFromFile
(<FileName>)
Parameters:
<FileName> (required)
Type: String.
File name. Cannot be empty. If it includes a path to an existing directory, then the value will be retrieved from the file found in that directory, otherwise the program's executable directory will be used.
Return value:
Type: Arbitrary.
Description:
Retrieves a value from a file previously stored in it using the ValueToFile function. The return type is determined by the type of the value stored in the file.
Availability:
Server, thick client, external connection.

D
Dimonchik, 2019-08-28
@dimonchik2013

UID like 4238019d-7e49-4fc9-91db-b6b951d5cf8e turn into a string (quotes) and try as JSON

S
Sergey, 2019-08-28
@begemot_sun

> using PHP means, it means that you need to execute a 1C script that will parse
this, comrades)
And after parsing, you need to build a grammar, generate a parser and parse it calmly and elementarily.
Google parser generator, lexer.
YACC LEX in PHP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question