m-json.mkxl /size: 2419 b    last modification: 2024-01-16 10:22
1%D \module
2%D   [       file=m-json,
3%D        version=2022.04.11, % 2012.08.03,
4%D          title=\CONTEXT\ Modules,
5%D       subtitle=Json,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\startmodule [json]
15
16\registerctxluafile{util-jsn}{autosuffix}
17\registerctxluafile{util-tbs}{}
18
19% \def\u#1#2#3#4{\cldcontext{utf.char(0x#1#2#3#4)}}
20
21\startluacode
22
23    local tablestore = utilities.tablestore
24    local field      = tablestore.field
25
26    interfaces.implement {
27        name      = "loadtable",
28        actions   = tablestore.load,
29        arguments = { "optional", "optional" },
30        public    = true,
31        protected = true,
32    }
33
34    interfaces.implement {
35        name      = "tablefield",
36        actions   = function(namespace,name)
37            local data = field(namespace,name)
38            if data then
39                context(data)
40            end
41        end,
42        arguments = "2 arguments",
43        public    = true,
44        protected = true,
45    }
46
47    interfaces.implement {
48        name      = "tablefielddefault",
49        actions   = function(namespace,name,default)
50            local data = field(namespace,name,default)
51            if data then
52                context(data)
53            end
54        end,
55        arguments = "3 arguments",
56        public    = true,
57    }
58
59    local replace   = lpeg.replacer("@","%%")
60    local lpegmatch = lpeg.match
61
62    interfaces.implement {
63        name      = "tableformatted",
64        actions   = function(namespace,name,fmt)
65            local data = field(namespace,name)
66            if data then
67                context(lpegmatch(replace,fmt),data)
68            end
69        end,
70        arguments = "3 arguments",
71        public    = true,
72    }
73
74    interfaces.implement {
75        name      = "tablelength",
76        actions   = { tablestore.length, context },
77        arguments = "2 arguments",
78        public    = true,
79    }
80
81    utilities.json.field  = tablestore.field
82    utilities.json.loaded = tablestore.loaded
83    utilities.json.length = tablestore.length
84
85\stopluacode
86
87\let\loadjsonfile     \loadtable
88\let\jsonfield        \tablefield
89\let\jsonfielddefault \tablefielddefault
90\let\jsonlength       \tablelength
91
92\stopmodule
93