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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
!> A small, strict JSON reader.
!>
!> Fortran ships no JSON parser, so this is hand-written. It is a complete
!> RFC 8259 reader for the shapes the API produces: objects, arrays, strings
!> with `\u` escapes and surrogate pairs, numbers, `true`, `false` and `null`.
!>
!> Values live in one growable array inside a `json_document` and are referred
!> to by integer index. That keeps the type free of recursive allocatable
!> components, and it means no reference into the document can dangle when the
!> array grows during parsing.
module cepis_json
use, intrinsic :: iso_fortran_env, only: real64, int64
use cepis_strings, only: string_buffer, from_real, from_integer, parse_real, utf8_encode
implicit none
private
character, parameter :: BACKSLASH = char(92)
integer, parameter, public :: JSON_NULL = 0
integer, parameter, public :: JSON_BOOL = 1
integer, parameter, public :: JSON_NUMBER = 2
integer, parameter, public :: JSON_STRING = 3
integer, parameter, public :: JSON_ARRAY = 4
integer, parameter, public :: JSON_OBJECT = 5
!> One `"name": value` pair of an object.
type, public :: json_member
character(len=:), allocatable :: name
integer :: node = 0
end type json_member
!> One JSON value.
type, public :: json_node
integer :: kind = JSON_NULL
character(len=:), allocatable :: text
real(real64) :: number = 0.0_real64
logical :: truth = .false.
type(json_member), allocatable :: members(:)
integer, allocatable :: items(:)
end type json_node
!> A parsed document. `root` indexes the top-level value.
type, public :: json_document
type(json_node), allocatable :: nodes(:)
integer :: used = 0
integer :: root = 0
contains
!> The kind of a node; `JSON_NULL` for an index of zero.
procedure :: kind_of
!> Index of the value of `name` inside object `node`, or zero.
procedure :: member
!> `.true.` when object `node` carries `name`.
procedure :: has
!> A node as text. Numbers and booleans are rendered; other kinds and
!> a zero index give the empty string, never a failure.
procedure :: text_of
!> A node as a floating-point value; zero when it is not a number.
procedure :: number_of
!> A node as an integer; zero when it is not a number.
procedure :: integer_of
!> `text_of` applied to a named member of an object.
procedure :: field_text
!> `number_of` applied to a named member of an object.
procedure :: field_number
!> `integer_of` applied to a named member of an object.
procedure :: field_integer
end type json_document
public :: json_parse
contains
!> Parse `text`. On failure `ok` is `.false.` and `message` says why.
subroutine json_parse(text, doc, ok, message)
character(len=*), intent(in) :: text
type(json_document), intent(out) :: doc
logical, intent(out) :: ok
character(len=:), allocatable, intent(out) :: message
integer :: pos
message = ''
pos = 1
call skip_space(text, pos)
call parse_value(doc, text, pos, doc%root, ok, message)
if (.not. ok) return
call skip_space(text, pos)
if (pos <= len(text)) then
ok = .false.
message = 'trailing characters after the JSON value'
end if
end subroutine json_parse
pure function kind_of(self, node) result(kind)
class(json_document), intent(in) :: self
integer, intent(in) :: node
integer :: kind
if (node <= 0 .or. node > self%used) then
kind = JSON_NULL
else
kind = self%nodes(node)%kind
end if
end function kind_of
pure function member(self, node, name) result(index)
class(json_document), intent(in) :: self
integer, intent(in) :: node
character(len=*), intent(in) :: name
integer :: index, i
index = 0
if (self%kind_of(node) /= JSON_OBJECT) return
if (.not. allocated(self%nodes(node)%members)) return
do i = 1, size(self%nodes(node)%members)
if (self%nodes(node)%members(i)%name == name) then
index = self%nodes(node)%members(i)%node
return
end if
end do
end function member
pure function has(self, node, name) result(yes)
class(json_document), intent(in) :: self
integer, intent(in) :: node
character(len=*), intent(in) :: name
logical :: yes
yes = self%member(node, name) /= 0
end function has
pure function text_of(self, node) result(text)
class(json_document), intent(in) :: self
integer, intent(in) :: node
character(len=:), allocatable :: text
text = ''
select case (self%kind_of(node))
case (JSON_STRING)
if (allocated(self%nodes(node)%text)) text = self%nodes(node)%text
case (JSON_NUMBER)
if (abs(self%nodes(node)%number - aint(self%nodes(node)%number)) < &
1.0e-9_real64 .and. abs(self%nodes(node)%number) < 1.0e15_real64) then
text = from_integer(int(self%nodes(node)%number, int64))
else
text = from_real(self%nodes(node)%number)
end if
case (JSON_BOOL)
if (self%nodes(node)%truth) then
text = 'true'
else
text = 'false'
end if
end select
end function text_of
pure function number_of(self, node) result(value)
class(json_document), intent(in) :: self
integer, intent(in) :: node
real(real64) :: value
value = 0.0_real64
if (self%kind_of(node) == JSON_NUMBER) value = self%nodes(node)%number
end function number_of
pure function integer_of(self, node) result(value)
class(json_document), intent(in) :: self
integer, intent(in) :: node
integer(int64) :: value
value = 0_int64
if (self%kind_of(node) == JSON_NUMBER) then
value = int(self%nodes(node)%number, int64)
end if
end function integer_of
pure function field_text(self, node, name) result(text)
class(json_document), intent(in) :: self
integer, intent(in) :: node
character(len=*), intent(in) :: name
character(len=:), allocatable :: text
text = self%text_of(self%member(node, name))
end function field_text
pure function field_number(self, node, name) result(value)
class(json_document), intent(in) :: self
integer, intent(in) :: node
character(len=*), intent(in) :: name
real(real64) :: value
value = self%number_of(self%member(node, name))
end function field_number
pure function field_integer(self, node, name) result(value)
class(json_document), intent(in) :: self
integer, intent(in) :: node
character(len=*), intent(in) :: name
integer(int64) :: value
value = self%integer_of(self%member(node, name))
end function field_integer
subroutine new_node(doc, index)
type(json_document), intent(inout) :: doc
integer, intent(out) :: index
type(json_node), allocatable :: bigger(:)
if (.not. allocated(doc%nodes)) allocate (doc%nodes(16))
if (doc%used >= size(doc%nodes)) then
allocate (bigger(2*size(doc%nodes)))
bigger(1:doc%used) = doc%nodes(1:doc%used)
call move_alloc(bigger, doc%nodes)
end if
doc%used = doc%used + 1
index = doc%used
end subroutine new_node
pure subroutine skip_space(text, pos)
character(len=*), intent(in) :: text
integer, intent(inout) :: pos
do while (pos <= len(text))
select case (text(pos:pos))
case (' ', char(9), char(10), char(13))
pos = pos + 1
case default
return
end select
end do
end subroutine skip_space
recursive subroutine parse_value(doc, text, pos, node, ok, message)
type(json_document), intent(inout) :: doc
character(len=*), intent(in) :: text
integer, intent(inout) :: pos
integer, intent(out) :: node
logical, intent(out) :: ok
character(len=:), allocatable, intent(out) :: message
node = 0
ok = .false.
message = ''
call skip_space(text, pos)
if (pos > len(text)) then
message = 'unexpected end of input'
return
end if
select case (text(pos:pos))
case ('{')
call parse_object(doc, text, pos, node, ok, message)
case ('[')
call parse_array(doc, text, pos, node, ok, message)
case ('"')
block
character(len=:), allocatable :: value
call parse_string(text, pos, value, ok, message)
if (.not. ok) return
call new_node(doc, node)
doc%nodes(node)%kind = JSON_STRING
doc%nodes(node)%text = value
end block
case ('t')
call parse_literal(doc, text, pos, 'true', node, ok, message)
case ('f')
call parse_literal(doc, text, pos, 'false', node, ok, message)
case ('n')
call parse_literal(doc, text, pos, 'null', node, ok, message)
case default
call parse_number(doc, text, pos, node, ok, message)
end select
end subroutine parse_value
subroutine parse_literal(doc, text, pos, word, node, ok, message)
type(json_document), intent(inout) :: doc
character(len=*), intent(in) :: text
integer, intent(inout) :: pos
character(len=*), intent(in) :: word
integer, intent(out) :: node
logical, intent(out) :: ok
character(len=:), allocatable, intent(out) :: message
node = 0
ok = .false.
message = ''
if (pos + len(word) - 1 > len(text)) then
message = 'truncated literal'
return
end if
if (text(pos:pos + len(word) - 1) /= word) then
message = 'unknown literal at position '//from_integer(int(pos, int64))
return
end if
pos = pos + len(word)
call new_node(doc, node)
select case (word)
case ('true')
doc%nodes(node)%kind = JSON_BOOL
doc%nodes(node)%truth = .true.
case ('false')
doc%nodes(node)%kind = JSON_BOOL
doc%nodes(node)%truth = .false.
case default
doc%nodes(node)%kind = JSON_NULL
end select
ok = .true.
end subroutine parse_literal
subroutine parse_number(doc, text, pos, node, ok, message)
type(json_document), intent(inout) :: doc
character(len=*), intent(in) :: text
integer, intent(inout) :: pos
integer, intent(out) :: node
logical, intent(out) :: ok
character(len=:), allocatable, intent(out) :: message
integer :: first
real(real64) :: value
node = 0
ok = .false.
message = ''
first = pos
if (text(pos:pos) == '-') pos = pos + 1
do while (pos <= len(text))
select case (text(pos:pos))
case ('0':'9', '.', 'e', 'E', '+', '-')
pos = pos + 1
case default
exit
end select
end do
if (pos == first) then
message = 'unexpected character '''//text(pos:pos)//''''
return
end if
call parse_real(text(first:pos - 1), value, ok)
if (.not. ok) then
message = 'malformed number '''//text(first:pos - 1)//''''
return
end if
call new_node(doc, node)
doc%nodes(node)%kind = JSON_NUMBER
doc%nodes(node)%number = value
end subroutine parse_number
subroutine parse_string(text, pos, value, ok, message)
character(len=*), intent(in) :: text
integer, intent(inout) :: pos
character(len=:), allocatable, intent(out) :: value
logical, intent(out) :: ok
character(len=:), allocatable, intent(out) :: message
type(string_buffer) :: out
integer :: code, low
logical :: got
value = ''
ok = .false.
message = ''
pos = pos + 1
do
if (pos > len(text)) then
message = 'unterminated string'
return
end if
if (text(pos:pos) == '"') then
pos = pos + 1
value = out%text()
ok = .true.
return
else if (text(pos:pos) /= BACKSLASH) then
call out%append(text(pos:pos))
pos = pos + 1
cycle
end if
pos = pos + 1
if (pos > len(text)) then
message = 'unterminated escape'
return
end if
select case (text(pos:pos))
case ('"')
call out%append('"')
case (BACKSLASH)
call out%append(BACKSLASH)
case ('/')
call out%append('/')
case ('b')
call out%append(char(8))
case ('f')
call out%append(char(12))
case ('n')
call out%append(char(10))
case ('r')
call out%append(char(13))
case ('t')
call out%append(char(9))
case ('u')
call read_hex4(text, pos + 1, code, got)
if (.not. got) then
message = 'malformed unicode escape'
return
end if
pos = pos + 4
if (code >= 55296 .and. code <= 56319) then
if (pos + 6 <= len(text)) then
if (text(pos + 1:pos + 1) == BACKSLASH .and. &
text(pos + 2:pos + 2) == 'u') then
call read_hex4(text, pos + 3, low, got)
if (got .and. low >= 56320 .and. low <= 57343) then
code = 65536 + (code - 55296)*1024 + &
(low - 56320)
pos = pos + 6
end if
end if
end if
end if
call out%append(utf8_encode(code))
case default
message = 'unknown escape sequence'
return
end select
pos = pos + 1
end do
end subroutine parse_string
pure subroutine read_hex4(text, at, code, ok)
character(len=*), intent(in) :: text
integer, intent(in) :: at
integer, intent(out) :: code
logical, intent(out) :: ok
integer :: i, digit
code = 0
ok = .false.
if (at + 3 > len(text)) return
do i = at, at + 3
select case (text(i:i))
case ('0':'9')
digit = iachar(text(i:i)) - 48
case ('a':'f')
digit = iachar(text(i:i)) - 87
case ('A':'F')
digit = iachar(text(i:i)) - 55
case default
return
end select
code = code*16 + digit
end do
ok = .true.
end subroutine read_hex4
recursive subroutine parse_array(doc, text, pos, node, ok, message)
type(json_document), intent(inout) :: doc
character(len=*), intent(in) :: text
integer, intent(inout) :: pos
integer, intent(out) :: node
logical, intent(out) :: ok
character(len=:), allocatable, intent(out) :: message
integer, allocatable :: items(:), bigger(:)
integer :: count, child
node = 0
ok = .false.
message = ''
allocate (items(8))
count = 0
pos = pos + 1
call skip_space(text, pos)
if (pos <= len(text)) then
if (text(pos:pos) == ']') then
pos = pos + 1
call finish_array(doc, items, count, node)
ok = .true.
return
end if
end if
do
call parse_value(doc, text, pos, child, ok, message)
if (.not. ok) return
if (count >= size(items)) then
allocate (bigger(2*size(items)))
bigger(1:count) = items(1:count)
call move_alloc(bigger, items)
end if
count = count + 1
items(count) = child
call skip_space(text, pos)
if (pos > len(text)) then
ok = .false.
message = 'unterminated array'
return
end if
if (text(pos:pos) == ',') then
pos = pos + 1
cycle
else if (text(pos:pos) == ']') then
pos = pos + 1
call finish_array(doc, items, count, node)
ok = .true.
return
else
ok = .false.
message = 'expected '','' or '']'' in array'
return
end if
end do
end subroutine parse_array
subroutine finish_array(doc, items, count, node)
type(json_document), intent(inout) :: doc
integer, intent(in) :: items(:)
integer, intent(in) :: count
integer, intent(out) :: node
call new_node(doc, node)
doc%nodes(node)%kind = JSON_ARRAY
allocate (doc%nodes(node)%items(count))
if (count > 0) doc%nodes(node)%items = items(1:count)
end subroutine finish_array
recursive subroutine parse_object(doc, text, pos, node, ok, message)
type(json_document), intent(inout) :: doc
character(len=*), intent(in) :: text
integer, intent(inout) :: pos
integer, intent(out) :: node
logical, intent(out) :: ok
character(len=:), allocatable, intent(out) :: message
type(json_member), allocatable :: members(:), bigger(:)
character(len=:), allocatable :: name
integer :: count, child
node = 0
ok = .false.
message = ''
allocate (members(8))
count = 0
pos = pos + 1
call skip_space(text, pos)
if (pos <= len(text)) then
if (text(pos:pos) == '}') then
pos = pos + 1
call finish_object(doc, members, count, node)
ok = .true.
return
end if
end if
do
call skip_space(text, pos)
if (pos > len(text)) then
message = 'unterminated object'
return
end if
if (text(pos:pos) /= '"') then
message = 'expected a string key in object'
return
end if
call parse_string(text, pos, name, ok, message)
if (.not. ok) return
call skip_space(text, pos)
if (pos > len(text)) then
ok = .false.
message = 'unterminated object'
return
end if
if (text(pos:pos) /= ':') then
ok = .false.
message = 'expected '':'' after key '''//name//''''
return
end if
pos = pos + 1
call parse_value(doc, text, pos, child, ok, message)
if (.not. ok) return
if (count >= size(members)) then
allocate (bigger(2*size(members)))
bigger(1:count) = members(1:count)
call move_alloc(bigger, members)
end if
count = count + 1
members(count)%name = name
members(count)%node = child
call skip_space(text, pos)
if (pos > len(text)) then
ok = .false.
message = 'unterminated object'
return
end if
if (text(pos:pos) == ',') then
pos = pos + 1
cycle
else if (text(pos:pos) == '}') then
pos = pos + 1
call finish_object(doc, members, count, node)
ok = .true.
return
else
ok = .false.
message = 'expected '','' or ''}'' in object'
return
end if
end do
end subroutine parse_object
subroutine finish_object(doc, members, count, node)
type(json_document), intent(inout) :: doc
type(json_member), intent(in) :: members(:)
integer, intent(in) :: count
integer, intent(out) :: node
call new_node(doc, node)
doc%nodes(node)%kind = JSON_OBJECT
allocate (doc%nodes(node)%members(count))
if (count > 0) doc%nodes(node)%members = members(1:count)
end subroutine finish_object
end module cepis_json
|