Skip to content

Commit db0d35b

Browse files
Merge pull request #340 from jacobwilliams/339-json-file-remove
json file remove
2 parents 8022459 + 0f6dee7 commit db0d35b

File tree

9 files changed

+77
-18
lines changed

9 files changed

+77
-18
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# --compiler : gnu or gfortran for gfortran, intel or ifort for intel compiler
3232
# A custom compiler may also be specified here, e.g. ftn
3333
#
34-
# --cflags : Enter any aditiol/custom compiler flags here and make sure they are
34+
# --cflags : Enter any additional/custom compiler flags here and make sure they are
3535
# properly quoted
3636
#
3737
# --help : Print a usage message and exit.

src/json_file_module.F90

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ module json_file_module
169169
json_file_update_string_val_ascii
170170
#endif
171171

172+
!>
173+
! Remove a variable from a [[json_file(type)]]
174+
! by specifying the path.
175+
generic,public :: remove => MAYBEWRAP(json_file_remove)
176+
172177
!traverse
173178
procedure,public :: traverse => json_file_traverse
174179

@@ -244,6 +249,9 @@ module json_file_module
244249
procedure :: json_file_update_string_val_ascii
245250
#endif
246251

252+
!remove:
253+
procedure :: MAYBEWRAP(json_file_remove)
254+
247255
!print_file:
248256
procedure :: json_file_print_to_console
249257
procedure :: json_file_print_1
@@ -2192,6 +2200,42 @@ subroutine json_file_traverse(me,traverse_callback)
21922200
end subroutine json_file_traverse
21932201
!*****************************************************************************************
21942202

2203+
!*****************************************************************************************
2204+
!> author: Jacob Williams
2205+
! date: 7/7/2018
2206+
!
2207+
! Remove a variable from a JSON file.
2208+
!
2209+
!@note This is just a wrapper to [[remove_if_present]].
2210+
2211+
subroutine json_file_remove(me,path)
2212+
2213+
implicit none
2214+
2215+
class(json_file),intent(inout) :: me
2216+
character(kind=CK,len=*),intent(in) :: path !! the path to the variable
2217+
2218+
call me%core%remove_if_present(me%p,path)
2219+
2220+
end subroutine json_file_remove
2221+
!*****************************************************************************************
2222+
2223+
!*****************************************************************************************
2224+
!>
2225+
! Alternate version of [[json_file_remove]], where "path" is kind=CDK.
2226+
2227+
subroutine wrap_json_file_remove(me,path)
2228+
2229+
implicit none
2230+
2231+
class(json_file),intent(inout) :: me
2232+
character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
2233+
2234+
call me%remove(to_unicode(path))
2235+
2236+
end subroutine wrap_json_file_remove
2237+
!*****************************************************************************************
2238+
21952239
!*****************************************************************************************
21962240
end module json_file_module
21972241
!*****************************************************************************************

src/json_value_module.F90

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ module json_value_module
168168
logical(LK) :: is_verbose = .false. !! if true, all exceptions are
169169
!! immediately printed to console.
170170

171-
logical(LK) :: stop_on_error = .false. !! if true, then the program is
171+
logical(LK) :: stop_on_error = .false. !! if true, then the program is
172172
!! stopped immediately when an
173173
!! exception is raised.
174174

@@ -6308,8 +6308,9 @@ end subroutine json_get_by_path_default
63086308
! are user-specified. To fully conform to the RFC 6901 standard,
63096309
! should probably set (via `initialize`):
63106310
!
6311-
! * `trailing_spaces_significant` = .true. [this is not the default setting]
6312-
! * `case_sensitive_keys` = .true. [this is the default setting]
6311+
! * `case_sensitive_keys = .true.` [this is the default setting]
6312+
! * `trailing_spaces_significant = .true.` [this is *not* the default setting]
6313+
! * `allow_duplicate_keys = .false.` [this is *not* the default setting]
63136314
!
63146315
!### Example
63156316
!
@@ -6454,7 +6455,7 @@ subroutine json_get_by_path_rfc6901(json, me, path, p, found)
64546455
end if
64556456
if (status_ok) then
64566457
! if we make it this far, it should be
6457-
! convertable to an integer, so do it.
6458+
! convertible to an integer, so do it.
64586459
call string_to_integer(token,ival,status_ok)
64596460
end if
64606461
end if
@@ -6753,6 +6754,7 @@ subroutine json_get_by_path_jsonpath_bracket(json,me,path,p,found,create_it,was_
67536754

67546755
! verify that there are no spaces or other
67556756
! characters in the string:
6757+
status_ok = .true.
67566758
do i=1,len(token)
67576759
! It must only contain (0..9) characters
67586760
! (it must be unsigned)
@@ -9889,7 +9891,7 @@ subroutine parse_string(json, unit, str, string)
98899891
! start accumulating the hex string (should be the next 4 characters)
98909892
if (escape) then
98919893
escape = .false.
9892-
is_hex = (c=='u') !the next four characters are the hex string
9894+
is_hex = (c==CK_'u') !the next four characters are the hex string
98939895
else
98949896
escape = (c==backslash)
98959897
end if

src/tests/jf_test_01.F90

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ subroutine test_1(error_cnt)
7272
! print the parsed data to the console
7373
write(error_unit,'(A)') ''
7474
write(error_unit,'(A)') 'printing the file...'
75-
call json%print_file()
75+
call json%print_file(error_unit)
7676
if (json%failed()) then
7777
call json%print_error_message(error_unit)
7878
error_cnt = error_cnt + 1
@@ -326,9 +326,17 @@ subroutine test_1(error_cnt)
326326
end if
327327
end if
328328

329+
! remove a variable using the remove method:
330+
call json%remove(json_CK_'version.patch')
331+
call json%remove(json_CDK_'version.minor')
332+
if (json%failed()) then
333+
call json%print_error_message(error_unit)
334+
error_cnt = error_cnt + 1
335+
end if
336+
329337
write(error_unit,'(A)') ''
330338
write(error_unit,'(A)') 'printing the modified structure...'
331-
call json%print_file()
339+
call json%print_file(error_unit)
332340
if (json%failed()) then
333341
call json%print_error_message(error_unit)
334342
error_cnt = error_cnt + 1
@@ -362,7 +370,7 @@ subroutine test_1(error_cnt)
362370

363371
write(error_unit,'(A)') ''
364372
write(error_unit,'(A)') 'printing the modified structure...'
365-
call json%print_file()
373+
call json%print_file(error_unit)
366374
if (json%failed()) then
367375
call json%print_error_message(error_unit)
368376
error_cnt = error_cnt + 1
@@ -371,7 +379,7 @@ subroutine test_1(error_cnt)
371379
write(error_unit,'(A)') ''
372380
write(error_unit,'(A)') 'printing the modified structure (compact mode)...'
373381
call json%initialize(no_whitespace=.true.)
374-
call json%print_file()
382+
call json%print_file(error_unit)
375383
if (json%failed()) then
376384
call json%print_error_message(error_unit)
377385
error_cnt = error_cnt + 1

src/tests/jf_test_05.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ subroutine test_5(error_cnt)
6060

6161
! print the parsed data to the console:
6262
write(error_unit,'(A)') 'print file...'
63-
call json%print_file()
63+
call json%print_file(error_unit)
6464
if (json%failed()) then
6565
call json%print_error_message(error_unit)
6666
error_cnt = error_cnt + 1

src/tests/jf_test_18.F90

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ subroutine test_18(error_cnt)
3030
integer :: ival
3131
logical :: found
3232
logical,dimension(4) :: ok
33+
integer,dimension(4) :: iresult
3334

3435
write(error_unit,'(A)') ''
3536
write(error_unit,'(A)') '================================='
@@ -53,19 +54,23 @@ subroutine test_18(error_cnt)
5354

5455
call json%initialize(trailing_spaces_significant=.true.,&
5556
case_sensitive_keys=.true.)
56-
call go([1,2,3,4])
57+
iresult = [1,2,3,4]
58+
call go(iresult)
5759

5860
call json%initialize(trailing_spaces_significant=.false.,&
5961
case_sensitive_keys=.true.)
60-
call go([1,2,1,2])
62+
iresult = [1,2,1,2]
63+
call go(iresult)
6164

6265
call json%initialize(trailing_spaces_significant=.true.,&
6366
case_sensitive_keys=.false.)
64-
call go([1,1,3,3])
67+
iresult = [1,1,3,3]
68+
call go(iresult)
6569

6670
call json%initialize(trailing_spaces_significant=.false.,&
6771
case_sensitive_keys=.false.)
68-
call go([1,1,1,1])
72+
iresult = [1,1,1,1]
73+
call go(iresult)
6974

7075
!cleanup:
7176
call json%destroy(p)

src/tests/jf_test_22.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ subroutine test_22(error_cnt)
5757
! print the parsed data to the console
5858
write(error_unit,'(A)') ''
5959
write(error_unit,'(A)') 'printing the file...'
60-
call json%print_file()
60+
call json%print_file(error_unit)
6161
if (json%failed()) then
6262
call json%print_error_message(error_unit)
6363
error_cnt = error_cnt + 1

src/tests/jf_test_23.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ subroutine test_23(error_cnt)
6868
! print the parsed data to the console
6969
write(error_unit,'(A)') ''
7070
write(error_unit,'(A)') 'printing the file...'
71-
call json%print_file()
71+
call json%print_file(error_unit)
7272
if (json%failed()) then
7373
call json%print_error_message(error_unit)
7474
error_cnt = error_cnt + 1

src/tests/jf_test_30.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ subroutine test_30(error_cnt)
4444

4545
call json%initialize(escape_solidus=(i==1), stop_on_error=.true.)
4646
call json%load_from_string(str)
47-
call json%print_file()
47+
call json%print_file(error_unit)
4848

4949
if (json%failed()) then
5050
call json%print_error_message(error_unit)

0 commit comments

Comments
 (0)