Skip to content

Commit 1fca289

Browse files
committed
Merge branch 'master' of https://github.com/jacobwilliams/json-fortran into increase-coverage
2 parents 75bd3c5 + db0d35b commit 1fca289

File tree

9 files changed

+109
-36
lines changed

9 files changed

+109
-36
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: 39 additions & 23 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

@@ -3078,33 +3078,47 @@ subroutine json_value_add_member(json,p,member)
30783078
implicit none
30793079

30803080
class(json_core),intent(inout) :: json
3081-
type(json_value),pointer :: p !! `p` should be a `json_object`
3082-
!! or a `json_array` (although this
3083-
!! is not currently checked)
3081+
type(json_value),pointer :: p !! `p` must be a `json_object`
3082+
!! or a `json_array`
30843083
type(json_value),pointer :: member !! the child member
30853084
!! to add to `p`
30863085

3086+
integer(IK) :: var_type !! variable type of `p`
3087+
30873088
if (.not. json%exception_thrown) then
30883089

3089-
! associate the parent
3090-
member%parent => p
3090+
if (associated(p)) then
30913091

3092-
! add to linked list
3093-
if (associated(p%children)) then
3092+
call json%info(p,var_type=var_type)
30943093

3095-
p%tail%next => member
3096-
member%previous => p%tail
3094+
select case (var_type)
3095+
case(json_object, json_array)
30973096

3098-
else
3097+
! associate the parent
3098+
member%parent => p
3099+
3100+
! add to linked list
3101+
if (associated(p%children)) then
3102+
p%tail%next => member
3103+
member%previous => p%tail
3104+
else
3105+
p%children => member
3106+
member%previous => null() !first in the list
3107+
end if
30993108

3100-
p%children => member
3101-
member%previous => null() !first in the list
3109+
! new member is now the last one in the list
3110+
p%tail => member
3111+
p%n_children = p%n_children + 1
31023112

3103-
end if
3113+
case default
3114+
call json%throw_exception('Error in json_value_add_member: '//&
3115+
'can only add child to object or array')
3116+
end select
31043117

3105-
! new member is now the last one in the list
3106-
p%tail => member
3107-
p%n_children = p%n_children + 1
3118+
else
3119+
call json%throw_exception('Error in json_value_add_member: '//&
3120+
'the pointer is not associated')
3121+
end if
31083122

31093123
end if
31103124

@@ -5687,10 +5701,11 @@ end function wrap_json_valid_path
56875701
!>
56885702
! Returns the [[json_value]] pointer given the path string.
56895703
!
5690-
! It uses either of two methods:
5704+
! It uses one of three methods:
56915705
!
56925706
! * The original JSON-Fortran defaults
56935707
! * [RFC 6901](https://tools.ietf.org/html/rfc6901)
5708+
! * [JSONPath](http://goessner.net/articles/JsonPath/) "bracket-notation"
56945709

56955710
subroutine json_get_by_path(json, me, path, p, found)
56965711

@@ -5710,7 +5725,6 @@ subroutine json_get_by_path(json, me, path, p, found)
57105725

57115726
if (.not. json%exception_thrown) then
57125727

5713-
! note: it can only be 1 or 2 (3 not currently enabled)
57145728
select case (json%path_mode)
57155729
case(1_IK)
57165730
call json%json_get_by_path_default(me, path, p, found)
@@ -6294,8 +6308,9 @@ end subroutine json_get_by_path_default
62946308
! are user-specified. To fully conform to the RFC 6901 standard,
62956309
! should probably set (via `initialize`):
62966310
!
6297-
! * `trailing_spaces_significant` = .true. [this is not the default setting]
6298-
! * `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]
62996314
!
63006315
!### Example
63016316
!
@@ -6440,7 +6455,7 @@ subroutine json_get_by_path_rfc6901(json, me, path, p, found)
64406455
end if
64416456
if (status_ok) then
64426457
! if we make it this far, it should be
6443-
! convertable to an integer, so do it.
6458+
! convertible to an integer, so do it.
64446459
call string_to_integer(token,ival,status_ok)
64456460
end if
64466461
end if
@@ -6739,6 +6754,7 @@ subroutine json_get_by_path_jsonpath_bracket(json,me,path,p,found,create_it,was_
67396754

67406755
! verify that there are no spaces or other
67416756
! characters in the string:
6757+
status_ok = .true.
67426758
do i=1,len(token)
67436759
! It must only contain (0..9) characters
67446760
! (it must be unsigned)
@@ -9875,7 +9891,7 @@ subroutine parse_string(json, unit, str, string)
98759891
! start accumulating the hex string (should be the next 4 characters)
98769892
if (escape) then
98779893
escape = .false.
9878-
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
98799895
else
98809896
escape = (c==backslash)
98819897
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)