Skip to content

How to read integer arrays with null elements? #517

Closed
@milancurcic

Description

@milancurcic

This program:

use json_module, only: json_core, json_value

type(json_core) :: json
type(json_value), pointer :: json_val
integer, allocatable :: vec(:)
logical :: found

call json % parse(json_val, '{"a": [1, 2]}')
call json % get(json_val, 'a', vec, found)
print *, found, allocated(vec)

end

produces T T as expected.

However, this program

use json_module, only: json_core, json_value

type(json_core) :: json
type(json_value), pointer :: json_val
integer, allocatable :: vec(:)
logical :: found

call json % parse(json_val, '{"a": [1, null]}')
call json % get(json_val, 'a', vec, found)
print *, found, allocated(vec)

end

produces F F, which I didn't expect. I suspect that the null value in an integer array confuses json-fortran here.

If I declare vec as real, then I get NaN for null elements (also expected), and I can recover my non-null integer values from the intermediate real array. Is this your recommended way for getting integer arrays with null elements, or is there a better way?

As an aside, I suggest json % get to return found as .true. in this scenario, and handle a null in some other way. I know that there's no appropriate integer value for a null, but perhaps -huge(vec) would be OK as long as it's documented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions