ucs2JsonValue()

This procedure converts string to JSON value. All the forbiden charactes are changed either by adding escape character or by changing them to string alloved by JSON:

Conversion

  • / –> /
  • \ –> \\
  • " –> "
  • 0x09 –> \t
  • 0x0B –> \f
  • 0x0C –> \r
  • 0x0D –> \n
  • 0x08 –> \b
  • 0x0A –> \r\n
  • spec char bellow 0x1F–> \uXXXX where XXXX is hex number

NOTE:Enough output memory must be allocated by caller.

ParameterTypeDescription
inText_ptrPOINTERMandatoryPointer to variable which contains character value in UCS2 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 \');
    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. Thet is why when variable pointer is passed to ucs2jsonValue() 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.