Skip to content

Commit 5f50600

Browse files
authoredApr 2, 2017
Zero-length arrays (#277)
* When reading an empty JSON array it is now returned as an allocated array with zero length. Fixes #276 * fixed unicode issues in last commit.
1 parent c46f234 commit 5f50600

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
 

‎src/json_value_module.F90

+48
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,8 @@ subroutine json_value_insert_after_child_by_index(json,p,idx,element)
29652965
type(json_value),pointer :: p !! a JSON object or array.
29662966
integer(IK),intent(in) :: idx !! the index of the child of `p` to
29672967
!! insert the new element after
2968+
!! (this is a 1-based Fortran
2969+
!! style array index)
29682970
type(json_value),pointer :: element !! the element to insert
29692971

29702972
type(json_value),pointer :: tmp !! for getting the `idx`-th child of `p`
@@ -6228,6 +6230,15 @@ subroutine json_get_integer_vec(json, me, vec)
62286230

62296231
logical(LK) :: initialized
62306232

6233+
! check for 0-length arrays first:
6234+
select case (me%var_type)
6235+
case (json_array)
6236+
if (json%count(me)==0) then
6237+
allocate(vec(0))
6238+
return
6239+
end if
6240+
end select
6241+
62316242
initialized = .false.
62326243

62336244
!the callback function is called for each element of the array:
@@ -6440,6 +6451,15 @@ subroutine json_get_double_vec(json, me, vec)
64406451

64416452
logical(LK) :: initialized
64426453

6454+
! check for 0-length arrays first:
6455+
select case (me%var_type)
6456+
case (json_array)
6457+
if (json%count(me)==0) then
6458+
allocate(vec(0))
6459+
return
6460+
end if
6461+
end select
6462+
64436463
initialized = .false.
64446464

64456465
!the callback function is called for each element of the array:
@@ -6648,6 +6668,15 @@ subroutine json_get_logical_vec(json, me, vec)
66486668

66496669
logical(LK) :: initialized
66506670

6671+
! check for 0-length arrays first:
6672+
select case (me%var_type)
6673+
case (json_array)
6674+
if (json%count(me)==0) then
6675+
allocate(vec(0))
6676+
return
6677+
end if
6678+
end select
6679+
66516680
initialized = .false.
66526681

66536682
!the callback function is called for each element of the array:
@@ -6920,6 +6949,15 @@ subroutine json_get_string_vec(json, me, vec)
69206949

69216950
logical(LK) :: initialized
69226951

6952+
! check for 0-length arrays first:
6953+
select case (me%var_type)
6954+
case (json_array)
6955+
if (json%count(me)==0) then
6956+
allocate(vec(0))
6957+
return
6958+
end if
6959+
end select
6960+
69236961
initialized = .false.
69246962

69256963
!the callback function is called for each element of the array:
@@ -7043,6 +7081,16 @@ subroutine json_get_alloc_string_vec(json, me, vec, ilen)
70437081
logical(LK) :: initialized !! if the output array has been sized
70447082
integer(IK) :: max_len !! the length of the longest string in the array
70457083

7084+
! check for 0-length arrays first:
7085+
select case (me%var_type)
7086+
case (json_array)
7087+
if (json%count(me)==0) then
7088+
allocate(character(kind=CK,len=0) :: vec(0))
7089+
allocate(ilen(0))
7090+
return
7091+
end if
7092+
end select
7093+
70467094
initialized = .false.
70477095

70487096
call json%string_info(me,ilen=ilen,max_str_len=max_len)

‎src/tests/jf_test_1.f90

+20
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ subroutine test_1(error_cnt)
3333
real(wp) :: rval
3434
logical :: found
3535
logical :: lval
36+
integer,dimension(:),allocatable :: ivec
37+
integer,dimension(:),allocatable :: ilen
38+
real(wp),dimension(:),allocatable :: rvec
39+
character(kind=json_CK,len=1),dimension(:),allocatable :: cvec
40+
character(kind=json_CK,len=:),dimension(:),allocatable :: acvec
41+
logical,dimension(:),allocatable :: lvec
3642

3743
error_cnt = 0
3844
call json%initialize()
@@ -221,6 +227,20 @@ subroutine test_1(error_cnt)
221227
write(error_unit,'(A)') 'files[5] = '//trim(cval)
222228
end if
223229

230+
! test empty array (each type):
231+
write(error_unit,'(A)') ''
232+
call json%get('empty_array', ivec )
233+
if (.not. json%failed()) call json%get('empty_array', rvec )
234+
if (.not. json%failed()) call json%get('empty_array', cvec )
235+
if (.not. json%failed()) call json%get('empty_array', acvec, ilen )
236+
if (.not. json%failed()) call json%get('empty_array', lvec )
237+
if (json%failed()) then
238+
call json%print_error_message(error_unit)
239+
error_cnt = error_cnt + 1
240+
else
241+
write(error_unit,'(A)') 'empty_array = ',ivec
242+
end if
243+
224244
!
225245
! Test of values that aren't there:
226246
! Note: when using the "found" output, the exceptions are cleared automatically.

0 commit comments

Comments
 (0)
Please sign in to comment.