Skip to content

Commit 46170a2

Browse files
committed
Experimental support for ignoring comments when parsing JSON file.
Currently, comment lines can only be specified by a single character. Fixes #234
1 parent f3deedb commit 46170a2

File tree

4 files changed

+254
-33
lines changed

4 files changed

+254
-33
lines changed

files/inputs/comments.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
!
2+
! This is a JSON file with comments
3+
!
4+
! The comments are lines beginning with the `!` character.
5+
! Here is an example of multiple comment lines
6+
7+
{
8+
! this is an object
9+
"version": {
10+
"major": 2,
11+
"minor": 2,
12+
"patch": 1,
13+
"string": "2.2.1", ! this is a string
14+
"svn": 7191
15+
}
16+
!the next line is a comma
17+
,
18+
!the previous line was a comma
19+
"files": [
20+
"..\\path\\to\\files\\file1.txt",
21+
"..\\path\\to\\files\\file2.txt",
22+
"..\\path\\to\\files\\file3.txt",
23+
"test \u2FA4 \uABCD \uABCD\uABCDtest",! this is an array element
24+
! another comment line.
25+
" test \\u \" blah\\\" test test",
26+
"..\\path\\to\\files\\",
27+
" this string has ! comment characters !"
28+
],
29+
"empty_array": [
30+
!an empty array
31+
],
32+
"empty_object": {
33+
},
34+
"empty_string": "",
35+
"data": [
36+
{
37+
"number": 1,
38+
"tf1": true,
39+
"tf2": false,
40+
"empty": null,
41+
"name": "Horatio",
42+
"array": [
43+
"1",
44+
"2",
45+
"3"
46+
]
47+
},
48+
! blah blah
49+
{
50+
"number": 2,
51+
"integer": 33,
52+
"real": 0.2333000000000000E+003 ! how about a comment after a real
53+
,
54+
"name": "Nelson"
55+
}
56+
]
57+
} ! and we're done

src/json_file_module.F90

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
273273
trailing_spaces_significant,&
274274
case_sensitive_keys,&
275275
no_whitespace,&
276-
unescape_strings)
276+
unescape_strings,&
277+
comment_char)
277278

278279
implicit none
279280

@@ -298,14 +299,19 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
298299
!! string is returned from [[json_get_string]]
299300
!! and similar routines. If true [default],
300301
!! then the string is returned unescaped.
302+
character(kind=CK,len=1),intent(in),optional :: comment_char !! If present, this character is used
303+
!! to denote comments in the JSON file,
304+
!! which will be ignored if present.
305+
!! Example: `!` or `#`.
301306

302307
call me%core%initialize(verbose,compact_reals,&
303308
print_signs,real_format,spaces_per_tab,&
304309
strict_type_checking,&
305310
trailing_spaces_significant,&
306311
case_sensitive_keys,&
307312
no_whitespace,&
308-
unescape_strings)
313+
unescape_strings,&
314+
comment_char)
309315

310316
end subroutine initialize_json_core_in_file
311317
!*****************************************************************************************
@@ -365,7 +371,8 @@ function initialize_json_file(p,verbose,compact_reals,&
365371
trailing_spaces_significant,&
366372
case_sensitive_keys,&
367373
no_whitespace,&
368-
unescape_strings) result(file_object)
374+
unescape_strings,&
375+
comment_char) result(file_object)
369376

370377
implicit none
371378

@@ -392,14 +399,19 @@ function initialize_json_file(p,verbose,compact_reals,&
392399
!! string is returned from [[json_get_string]]
393400
!! and similar routines. If true [default],
394401
!! then the string is returned unescaped.
402+
character(kind=CK,len=1),intent(in),optional :: comment_char !! If present, this character is used
403+
!! to denote comments in the JSON file,
404+
!! which will be ignored if present.
405+
!! Example: `!` or `#`.
395406

396407
call file_object%initialize(verbose,compact_reals,&
397408
print_signs,real_format,spaces_per_tab,&
398409
strict_type_checking,&
399410
trailing_spaces_significant,&
400411
case_sensitive_keys,&
401412
no_whitespace,&
402-
unescape_strings)
413+
unescape_strings,&
414+
comment_char)
403415

404416
if (present(p)) file_object%p => p
405417

0 commit comments

Comments
 (0)