Skip to content

Commit

Permalink
feat: Restoring unsupported JSON types
Browse files Browse the repository at this point in the history
  • Loading branch information
slaxxals committed Nov 18, 2021
1 parent 15a2a2f commit 22a5d93
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
78 changes: 76 additions & 2 deletions src/en/CommonModules/HTTPConnector/Ext/Module.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//
// URL: https://github.com/vbondarevsky/Connector
// e-mail: vbondarevsky@gmail.com
// Version: 2.3.1
// Version: 2.3.2
//
// Requirements: 1C:Enterprise platform version **8.3.10** and higher.

Expand Down Expand Up @@ -310,6 +310,19 @@ EndFunction
// ** JSONDateFormat - JSONDateFormat - Sets the date serialization format.
// ** PropertiesNamesWithDateValues - String, Array of Strings - JSON properties names,
// For the specified properties date restoration from string will be called.
// ** ReviverFunctionName - String - This function is called every time a property is read.
// It must have the following parameters:
// ** Property - String - Specify it only for reading JSON objects.
// ** Value - Arbitrary - A value of a serializable type.
// ** AdditionalParameters - Arbitrary
// The return value has arbitrary type, deserialized from JSON.
// ** ReviverFunctionModule - Arbitrary - Specifies the module, the procedure of which will be used
// for value restoration..
// ** ReviverFunctionAdditionalParameters - Arbitrary - Additional parameters which will be
// transferred to the value recovery function.
// ** RetriverPropertiesNames - Array - Array of JSON property names which recovery function
// will be called for.
// ** MaximumNesting - Number - The maximum nested level of a JSON object.
// * JSONWriterSettings - JSONWriterSettings - Defines a set of parameters used for JSON writing..
// * Data - String, BinaryData - arnitrary data to send in a request.
// - Structure, Map - form fields to send in a request:
Expand Down Expand Up @@ -452,6 +465,19 @@ EndFunction
// * JSONDateFormat - JSONDateFormat - Specifies a deserialization format of dates of the JSON objects.
// * PropertiesNamesWithDateValues - Array, String - JSON properties names,
// For the specified properties date restoration from string will be called.
// ** ReviverFunctionName - String - This function is called every time a property is read.
// It must have the following parameters:
// ** Property - String - Specify it only for reading JSON objects.
// ** Value - Arbitrary - A value of a serializable type.
// ** AdditionalParameters - Arbitrary
// The return value has arbitrary type, deserialized from JSON.
// ** ReviverFunctionModule - Arbitrary - Specifies the module, the procedure of which will be used
// for value restoration..
// ** ReviverFunctionAdditionalParameters - Arbitrary - Additional parameters which will be
// transferred to the value recovery function.
// ** RetriverPropertiesNames - Array - Array of JSON property names which recovery function
// will be called for.
// ** MaximumNesting - Number - The maximum nested level of a JSON object.
//
// Returns:
// Map - host response as JSON deserialized value.
Expand Down Expand Up @@ -747,6 +773,19 @@ EndFunction
// * PropertiesNamesWithDateValues - Array, String, FixedArray - JSON properties names,
// For the specified properties date restoration from string will be called.
// * JSONDateFormat - JSONDateFormat - Specifies a deserialization format of dates of the JSON objects.
// ** ReviverFunctionName - String - This function is called every time a property is read.
// It must have the following parameters:
// ** Property - String - Specify it only for reading JSON objects.
// ** Value - Arbitrary - A value of a serializable type.
// ** AdditionalParameters - Arbitrary
// The return value has arbitrary type, deserialized from JSON.
// ** ReviverFunctionModule - Arbitrary - Specifies the module, the procedure of which will be used
// for value restoration..
// ** ReviverFunctionAdditionalParameters - Arbitrary - Additional parameters which will be
// transferred to the value recovery function.
// ** RetriverPropertiesNames - Array - Array of JSON property names which recovery function
// will be called for.
// ** MaximumNesting - Number - The maximum nested level of a JSON object.
//
// Returns:
// Arbitrary - deserialized value from JSON.
Expand All @@ -767,7 +806,12 @@ Function JsonToObject(Json, Encoding = "utf-8", ConversionParameters = Undefined
JSONReader,
JSONConversionParameters.ReadToMap,
JSONConversionParameters.PropertiesNamesWithDateValues,
JSONConversionParameters.JSONDateFormat);
JSONConversionParameters.JSONDateFormat,
JSONConversionParameters.ReviverFunctionName,
JSONConversionParameters.ReviverFunctionModule,
JSONConversionParameters.ReviverFunctionAdditionalParameters,
JSONConversionParameters.RetriverPropertiesNames,
JSONConversionParameters.MaximumNesting);
JSONReader.Close();

Return Object;
Expand Down Expand Up @@ -2275,6 +2319,31 @@ Function JsonConversion(Property, Value, AdditionalParameters, Cancel) Export

EndFunction

// Restores a value of the type, which doesn't support deserialization
//
// Parameters:
// Property - String - property name, which value has to be restored.
// Value - String - value which has to be restored.
// PropertiesTypes - Map - types of properties to be restored.
// * Key - String - property name. Equals to a parameter Property value.
// * Value - Type - source value type.
//
// Returns:
// Arbitrary - restored value.
//
Function RestoreJson(Property, Value, PropertiesTypes) Export

PropertyType = PropertiesTypes.Get(Property);
If PropertyType = Type("UUID") Then
Return New UUID(Value);
ElsIf PropertyType = Type("BinaryData") Then
Return GetBinaryDataFromBase64String(Value);
Else
Return Value;
EndIf;

EndFunction

#EndRegion

#Region AWS4Authentication
Expand Down Expand Up @@ -2685,6 +2754,11 @@ Function JSONConversionParametersByDefault()
ConversionParametersByDefault.Insert("ConvertionFunctionName", Undefined);
ConversionParametersByDefault.Insert("ConvertionFunctionModule", Undefined);
ConversionParametersByDefault.Insert("ConvertionFunctionAdditionalParameters", Undefined);
ConversionParametersByDefault.Insert("ReviverFunctionName", Undefined);
ConversionParametersByDefault.Insert("ReviverFunctionModule", Undefined);
ConversionParametersByDefault.Insert("ReviverFunctionAdditionalParameters", Undefined);
ConversionParametersByDefault.Insert("RetriverPropertiesNames", Undefined);
ConversionParametersByDefault.Insert("MaximumNesting", 500);

Return ConversionParametersByDefault;

Expand Down
31 changes: 31 additions & 0 deletions src/en/DataProcessors/Tests/Ext/ObjectModule.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Function TestsList() Export
Tests.Add("Test_CorrectExceptionInMethodAsJson");
Tests.Add("Test_VerifyConversionToJsonNotSerializedValues");
Tests.Add("Test_VerifyConversionJSONDateWritingVariant");
Tests.Add("Test_VerifyOfRestoringUnsupportedValuesTypes");

Return Tests;

Expand Down Expand Up @@ -616,6 +617,36 @@ Procedure Test_VerifyConversionToJsonNotSerializedValues() Export

EndProcedure

Procedure Test_VerifyOfRestoringUnsupportedValuesTypes() Export

UUID = New UUID("be4ee795-7f5e-4d1a-be43-a6d6902f5cfd");
BinaryData = GetBinaryDataFromString("test", "utf-8", False);

Json = New Structure;
Json.Insert("UUID", String(UUID));
Json.Insert("BinaryData", GetBase64StringFromBinaryData(BinaryData));
Json.Insert("OtherData", 1);

JSONParameters = New Structure;
JSONParameters.Insert("ReviverFunctionModule", HTTPConnector);
JSONParameters.Insert("ReviverFunctionName", "RestoreJson");
PropertiesTypes = New Map;
PropertiesTypes.Insert("UUID", Type("UUID"));
PropertiesTypes.Insert("BinaryData", Type("BinaryData"));
JSONParameters.Insert("ReviverFunctionAdditionalParameters", PropertiesTypes);
JSONParameters.Insert("RetriverPropertiesNames", StrSplit("UUID,BinaryData", ","));

Result = HTTPConnector.PostJson(
"https://httpbin.org/post",
Json,
New Structure("JSONConversionParameters", JSONParameters));

AssertEquals(Result["json"]["UUID"], UUID);
AssertEquals(Result["json"]["BinaryData"], BinaryData);
AssertEquals(Result["json"]["OtherData"], 1);

EndProcedure

Procedure Test_PostAndRedirect() Export

Response = HTTPConnector.Get("https://httpbingo.org/redirect-to?url=https%3A%2F%2Fya.ru&status_code=301");
Expand Down

0 comments on commit 22a5d93

Please sign in to comment.