-
Notifications
You must be signed in to change notification settings - Fork 3
/
JSON.Mod
83 lines (71 loc) · 1.96 KB
/
JSON.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
MODULE JSON;
IMPORT Chars;
CONST
NullType* = 0;
BooleanType* = 1;
NumberType* = 2;
StringType* = 3;
ArrayType* = 4;
ObjectType* = 5;
TYPE
(* Self services as an interface to JSON types,
all JSON types extend it. *)
Self* = RECORD
type: INTEGER
END;
Boolean* = POINTER TO BooleanDesc;
BooleanDesc = RECORD (Self)
value: BOOLEAN
END;
Number* = POINTER TO NumberDesc;
NumberDesc* = RECORD (Self)
value : REAL;
NaN : BOOLEAN
END;
String* = POINTER TO StringDesc;
StringDesc* = RECORD (Self)
value : CHAR;
next : String
END;
List* = POINTER TO ListDesc;
ListDesc* = RECORD (Self)
value : Self;
next: List
END;
Object* = POINTER TO ObjectDesc;
ObjectDesc* = RECORD (Self)
key : String;
value: Self;
next : Object
END;
(* Base methods available to all JSON types via Self type definition *)
PROCEDURE IsNull*(o : Self) : BOOLEAN;
BEGIN
RETURN (o.type = NullType)
END IsNull;
(* IsType compares a JSON object agaist a type constant. Returns TRUE
if they match or FALSE otherwise *)
PROCEDURE IsType*(o : Self; checkType : INTEGER) : BOOLEAN;
RETURN (o.type = checkType)
END IsType;
(* TypeName exams a type setting the value of the typeName array of char *)
PROCEDURE TypeName*(o : Self; VAR typeName : ARRAY OF CHAR);
BEGIN
Chars.Clear(typeName);
IF o.type = NullType THEN
typeName := "null";
ELSIF o.type = BooleanType THEN
typeName := "boolean";
ELSIF o.type = NumberType THEN
typeName := "number";
ELSIF o.type = StringType THEN
typeName := "string";
ELSIF o.type = ArrayType THEN
typeName := "array";
ELSIF o.type = ObjectType THEN
typeName := "object";
ELSE
typeName := "undefined";
END;
END TypeName;
END JSON.