tpimport — Import.a JSON formatted text buffer to UBF|VIEW|STRING|CARRAY|JSON type buffer
#include <atmi.h>
int tpimport(char *istr, long ilen, char **obuf, long *olen, long flags);
For XATMI client link with -latmiclt -latmi -lview -lnstd -lpthread -lrt -lm
For XATMI server link with -latmisrv|-latmisrvnomain|-latmisrvinteg -latmi -lview -lnstd -lpthread -lrt -lm
Function converts JSON formatted text buffer into typed message buffer.
istr is JSON formatted buffer to import. If ilen is 0, istr is assumed to point to a null-terminated string. obuf is output buffer should be allocated with tpalloc() call. If the allocated buffer size is shorter then received one, then Enduro/X will automatically realloc the buffer to new size. If olen not NULL there will be stored imported buffer length.
tpimport support embedded UBF sub-buffers: sub-UBF, sub-VIEW, sub-PTR. The VIEW and UBF data is imported within the master UBF buffer. However PTR json fields will initiated allocation of new XATMI buffer, and pointer to XATMI buffer will be installed in master UBF. As UBF may have null occurrences, the same applies to VIEWS. The empty view name indicates that this is NULL view and will generate NULL/empty occurrence in UBF. See samples bellow.
Valid flags
If TPEX_STRING flag is set then input JSON formatted buffer is in base64.
If TPEX_NOCHANGE then do not allow to change the reply buffer type. If flag is set and different buffer type is received than original, then error TPEINVAL is returned.
JSON tags in import file:
Imported JSON as:
{ "buftype":"UBF", "version":1, "data": { "T_SHORT_FLD":1765, "T_LONG_FLD":[3333111,2], "T_CHAR_FLD":"A", "T_FLOAT_FLD":1, "T_DOUBLE_FLD":[1111.220000,333,444], "T_STRING_FLD":"HELLO WORLD", "T_CARRAY_FLD":"AAECA0hFTExPIEJJTkFSWQQFAA==" } }
Will be imported to following UBF buffer:
T_SHORT_FLD 1765 T_LONG_FLD 3333111 T_LONG_FLD 2 T_CHAR_FLD A T_FLOAT_FLD 1.00000 T_DOUBLE_FLD 1111.220000 T_DOUBLE_FLD 333.000000 T_DOUBLE_FLD 444.000000 T_STRING_FLD HELLO WORLD T_CARRAY_FLD \00\01\02\03HELLO BINARY\04\05\00
For example following JSON formatted text
{ "buftype":"VIEW", "version":1, "subtype":"MYVIEW56", "data": { "MYVIEW56": { "tshort1":1, "tlong1":2, "tchar1":"A", "tfloat1":1, "tdouble1":21, "tstring1":"ABC", "tcarray1":"SEVMTE8AAAAAAA==" } } }
Will be imported into following VIEW:
VIEW MYVIEW56 #type cname fbname count flag size null short tshort1 - 1 - - - long tlong1 - 1 - - - char tchar1 - 1 - - - float tfloat1 - 1 - - - double tdouble1 - 1 - - - string tstring1 - 1 - 15 - carray tcarray1 - 1 - 10 - END
For example following JSON formatted text
{ "buftype":"STRING", "version":1, "data":"HELLO WORLD" }
Will be imported into following STRING buffer
HELLO WORLD
For example following JSON formatted text
{ "buftype":"CARRAY", "version":1, "data":"SEVMTE8gV09STEQgQ0FSUkFZ" }
Will be imported into following CARRAY buffer
0000 48 45 4c 4c 4f 20 57 4f 52 4c 44 20 43 41 52 52 HELLO WORLD CARR 0010 41 59 AY
For example following JSON formatted text
{ "buftype":"JSON", "version":1, "data": {"T_SHORT_FLD":1765, "T_LONG_FLD":[3333111,2], "T_CHAR_FLD":"A", "T_FLOAT_FLD":1, "T_DOUBLE_FLD":[1111.220000,333,444], "T_STRING_FLD":"HELLO WORLD", "T_CARRAY_FLD":"AAECA0hFTExPIEJJTkFSWQQFAA=="} }
Will be imported into following JSON buffer
{ "T_SHORT_FLD":1765, "T_LONG_FLD":[3333111,2], "T_CHAR_FLD":"A", "T_FLOAT_FLD":1, "T_DOUBLE_FLD":[1111.220000,333,444], "T_STRING_FLD":"HELLO WORLD", "T_CARRAY_FLD":"AAECA0hFTExPIEJJTkFSWQQFAA==" }
Importing data with sub-buffers this example contains:
{ "buftype":"UBF", "version":1, "data":{ "T_SHORT_FLD":55, "T_UBF_FLD":{ "T_STRING_FLD":"HELLO WORLD INNER" }, "T_UBF_2_FLD":[ { "T_STRING_FLD":"HELLO WORLD INNER 1" }, { "T_STRING_FLD":"HELLO WORLD INNER 2" }, { } ], "T_VIEW_FLD":{ "MYVIEW56":{ "tshort1":1, "tlong1":2, "tchar1":"A", "tfloat1":1, "tdouble1":21, "tstring1":"ABC", "tcarray1":"SEVMTE8AAAAAAA==" } }, "T_VIEW_2_FLD":[ { "MYVIEW56":{ "tshort1":1, "tlong1":2, "tchar1":"A", "tfloat1":1, "tdouble1":21, "tstring1":"ABC_2", "tcarray1":"SEVMTE8AAAAAAA==" } }, { "":{ } }, { "MYVIEW56":{ "tshort1":1, "tlong1":2, "tchar1":"A", "tfloat1":1, "tdouble1":21, "tstring1":"ABC_3", "tcarray1":"SEVMTE8AAAAAAA==" } } ], "T_PTR_FLD":{ "buftype":"UBF", "version":1, "data":{ "T_SHORT_FLD":1765, "T_LONG_FLD":[ 115, 2 ], "T_CHAR_FLD":"A", "T_FLOAT_FLD":1, "T_DOUBLE_FLD":[ 1111.220000, 333, 444 ], "T_STRING_FLD":"HELLO WORLD", "T_CARRAY_FLD":"AAECA0hFTExPIEJJTkFSWQQFAA==" } }, "T_PTR_2_FLD":[ { "buftype":"UBF", "version":1, "data":{ "T_SHORT_FLD":1765, "T_LONG_FLD":[ 1111, 2 ], "T_CHAR_FLD":"A", "T_FLOAT_FLD":1, "T_DOUBLE_FLD":[ 1111.220000, 333, 444 ], "T_STRING_FLD":"HELLO WORLD 22", "T_CARRAY_FLD":"AAECA0hFTExPIEJJTkFSWQQFAA==" } }, { "buftype":"UBF", "version":1, "data":{ "T_SHORT_FLD":1765, "T_LONG_FLD":[ 4444, 2 ], "T_CHAR_FLD":"A", "T_FLOAT_FLD":1, "T_DOUBLE_FLD":[ 1111.220000, 333, 444 ], "T_STRING_FLD":"HELLO WORLD 44", "T_CARRAY_FLD":"AAECA0hFTExPIEJJTkFSWQQFAA==" } } ] } }
The imported UBF would render as:
T_SHORT_FLD 55 T_PTR_FLD 0x619000003280 T_PTR_2_FLD 0x61900000be80 T_PTR_2_FLD 0x619000002d80 T_UBF_FLD T_STRING_FLD HELLO WORLD INNER T_UBF_2_FLD T_STRING_FLD HELLO WORLD INNER 1 T_UBF_2_FLD T_STRING_FLD HELLO WORLD INNER 2 T_UBF_2_FLD T_VIEW_FLD MYVIEW56 tshort1 1 tlong1 2 tchar1 A tfloat1 1.00000 tdouble1 21.000000 tstring1 ABC tcarray1 HELLO\00\00\00\00\00 T_VIEW_2_FLD MYVIEW56 tshort1 1 tlong1 2 tchar1 A tfloat1 1.00000 tdouble1 21.000000 tstring1 ABC_2 tcarray1 HELLO\00\00\00\00\00 T_VIEW_2_FLD T_VIEW_2_FLD MYVIEW56 tshort1 1 tlong1 2 tchar1 A tfloat1 1.00000 tdouble1 21.000000 tstring1 ABC_3
On success, tpacall() return call descriptor (>0); on error, -1 is returned, with tperrno set to indicate the error.
Note that tpstrerror() returns generic error message plus custom message with debug info from last function call.
TPEINVAL Invalid parameter is given to function. Either istr is NULL or flags does not allow to change the value.
TPEINVAL Invalid JSON or invalid base64 encoding, istr is NULL or obuf is NULL.
TPEOTYPE Invalid type specified to function. VIEW sub-type not found or environment is not configured.
TPESYSTEM System failure occurred during serving. See logs i.e. user log, or debugs for more info.
TPEOS System failure occurred during serving. See logs i.e. user log, or debugs for more info. In case of insufficient memory this error will be generated too.
Sample code see under: