%Decode

This special method converts JSON string escape sequences to single characters and JSON numbers to DataBasic format.

All special method names and keywords are case-insensitive.

Syntax

%Decode(String, string)

%Decode(Number, number)

Syntax elements

stringAn expression that evaluates to a string.

numberAn expression that evaluates to a number.

Applicability

Internal objects only.

Comments

Characters without explicit escape sequences in JSON use \u followed by the dour-digit hexadecimal value of the character.

Examples

Example 1: String

Given:

Source = '{ "Characters" : "TEST\\\/\"\b\t\n\u000B\f\r\u00FE" }'
TestObject = %New(JSON, Source)
CRT TestObject->Characters
CRT
CRT "Decoded: "
Decoded = %Decode(String, TestObject->Characters)
Lngth = LEN(Decoded)
FOR I = 1 TO Lngth
    Ch = Decoded[I,1]
    CRT "Character ":I "R#2":" Seq. Code ":SEQ(Ch) "R#3":
    IF SEQ(Ch) >= 32 THEN CRT " Value ":Ch:
    CRT
NEXT

which produces:

TEST\\\/\"\b\t\n\u000B\f\r\u00FE

Decoded:
Character  1 Seq. Code  84 Value T
Character  2 Seq. Code  69 Value E
Character  3 Seq. Code  83 Value S
Character  4 Seq. Code  84 Value T
Character  5 Seq. Code  92 Value \
Character  6 Seq. Code  47 Value /
Character  7 Seq. Code  34 Value "
Character  8 Seq. Code   8
Character  9 Seq. Code   9
Character 10 Seq. Code  10
Character 11 Seq. Code  11
Character 12 Seq. Code  12
Character 13 Seq. Code  13
Character 14 Seq. Code 254 Value ^

Example 2: Number

Given:

Document =  ' { "Nos" : [ 1234, 12.34, 0.1234, 123e4, 123E4, -1234, 123e-4, '
Document := ' 1234, 12E+34, 1.2e-4, 12.34e1 ] } '
TestObject = %New(JSON, Document)
Lngth = TestObject->Nos->%Length() - 1
CRT "   JSON" "#13" :"Decoded"
FOR I = 0 TO Lngth
    CRT I "#2" :" ":TestObject->Nos(I) "#7" :"   ":%Decode(Number, TestObject->Nos(I))
NEXT I

which produces:

   JSON      Decoded
0  1234      1234
1  12.34     12.34
2  0.1234    0.1234
3  123e4     1230000
4  123E4     1230000
5  -1234     -1234
6  123e-4    0.0123
7  1234      1234
8  12E+34    120000000000000000000000000000000000
9  1.2e-4    0.00012
10 12.34e1   123.4

See also

%Encode, %Length, %New, CRT, LEN, SEQ, Assignment Operators