jsonValue2ucs()

This procedure converts JSON value to string. All escaped charactes are changed to binary representation in UCS format. Following conversion is applied:

Conversion

  • / –> /
  • \\ –> \
  • " –> "
  • \t –> 0x0009
  • \f –> 0x000B
  • \r –> 0x000C
  • \n –> 0x000D
  • \b –> 0x0008
  • \r\n –> 0x000A
  • \uXXXX –> 0xXXXX where XXXX is hex number

NOTE:Enough output memory must be allocated by caller.

ParameterTypeDescription
inText_ptrPOINTERMandatoryPointer to variable which contains character value in JSON coding (text which needs to be converted)
inText_size10U 0MandatoryLength of text in bytes
outText_ptrPOINTERMandatoryPointer to output text produced by procedure
outText_size10U 0MandatoryInput/output parameter. As an input the maximum size of allocated memory for output must be provided (in bytes). As output, procedure returns curent length of UCS2 text generated by conversion (In bytes).

NOTE: Procedure operates on UCS2 input and output strings!

  • Procedure returns *ON if everything is OK.
  • Procedure returns *OFF in case of errors in conversion

Example:

D lString         S            200C   VARYING
D lStringTo       S          16384C
D lStringToSize   S             10U 0    

    lString   = %UCS2('Sample text to change with \n');
    lStringTo = *BLANKS;
    lStringToSize = %Size(lStringTo);
    if ucs2JsonValue( %addr(lString) + 2
                     :%len(lString) * 2
                     :%addr(lStringTo)
                     :lStringToSize);

      lString = lStringTo;
      %len(lString) = %DIV(lStringToSize : 2);

    endif;  

In the sample code above the value which must be converted was put into VARYING variable. This type of veriable contains length of text stored as first two bytes. That is why when variable pointer is passed to jsonValue2ucs() procedure the address is calculated as %addr(lString) + 2. Then then length in bytes of input text is calculated as %len(lString) * 2 as lString variable is defined as UCS2. The output text can be longer because of conversion, that is why the lStringTo was defined as 16384C variable. To move result back into lString variable two lines of code are required. First just put everything from lStringTo into lString. The second one sets length lString to correct value. Function %div() divides size of output by two to convert lengt in bytes to length in characters.