jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
332 stars 83 forks source link

Problem when reading from array shaped json file #481

Closed RobertoLorusso closed 3 years ago

RobertoLorusso commented 3 years ago

Hello, after a lot of tests and searches inside of the issues I was unable to reach my goal. Once I read the issue (#282) I made changes to my code in order to conform it with the answer given by @jacobwilliams (which, as I can read, is a not verified answer) but the code still doesn't work.

Let me explain: I'm trying to read an array shaped json inside a user-defined type that reflects the json structure following the answer of @jacobwilliams to (#282) but the do-loop works only for the first iteration, whilst inside the next iterations of the loop I found out that the reference to the parent array gets 'lost' (thanks to the json_core%info() subroutine). For every iteration the code prints out the values obtained from json_core%info(): as you can see the value of 'name' variable is correct for the first child and empty for the remaining ones. What I'm doing wrong?

I'm posting the code and the result of execution. As you can see just before the do-loop I try the same instructions used inside of it and the code works (values correctly putted inside user-defined variable)

As requested from the contributing guidelines:

PS:

Kind Regards

        type(json_value),pointer :: p,root,c,out,child
        type(json_value),pointer :: snap
        type(json_file) :: json
        type(json_core), target :: core
        type(snapshot), allocatable, dimension(:) :: snapshots

        character(kind=json_CK,len=:),allocatable :: string,name
        character(len=50) :: frm
        logical(json_LK) :: found
        integer(json_IK) :: var_type,n_children
        integer :: index, n

        !init json_file and read from ./flight_no.json

        call json%initialize(strict_integer_type_checking=.false.)
        call core%initialize()
        call json%load(filename = './flight_no.json')
        call json%print_file()

        call json%get_core(core)
        call core%create_object(root, '')
        call core%create_object(out, 'snap')

        !get the root from json_file

        call json%get('snapshots', snap) 
        !get the 'snapshots' child and number of childrens, then allocate snapshot array
        !call core%get_child(root, 'snapshots', snap)
        call core%info(snap, var_type,n_children,name)
        allocate(snapshots(n_children))

        write(*,*) 'n_child:---->',n_children

        write(*,*) ''

        write(*,*) 'name:----> ',name
        write(*,*) ''

        !first check of instructions before iteration
        call core%get_child(snap, 2, child, found)
        call core%get(child, 'position.lat', out)
        call core%get(out,snapshots(2)%pos%lat )
        !call core%print(out, 'snap.json')
        write(*,*) 'snap 2 lat'
        write(*,*) snapshots(2)%pos%lat
        write(*,*) ''

        !second check of instructions before iteration

        call core%get_child(snap, 3, child, found)
        call core%get(child, 'position.lat', out)
        call core%get(out,snapshots(3)%pos%lat )
        write(*,*) 'snap 3 lat'
        write(*,*) snapshots(3)%pos%lat
        write(*,*) ''

        !apply the above instructions to the iterations

        !but fails 
        do index = 1, n_children
        call core%get_child(snap, index, p,found)
        call core%info(p, name = name, n_children = n_children)
        write(*,*) ''
        write(*,*) '{'
        write(*,*) '  FOUND CHILDREN:---->', found
        write(*,*) ''
        call core%info(snap, name = name, n_children = n_children)
        write(*,*) '  Iteration:name:----> ',name
        write(*,*) ''
        write(*,*) '  Iteration:n_children:----> ',n_children
        write(*,*) '}'
        if(found) then 
          call core%get(p,'position.lat', c)
          call core%get(c, snapshots(index)%pos%lat)

          call core%get(p,'position.long', c)
          call core%get(c, snapshots(index)%pos%lon)

          call core%get(p,'attitude.pitch', c)
          call core%get(c, snapshots(index)%att%pitch)

          call core%get(p,'attitude.roll', c)
          call core%get(c, snapshots(index)%att%roll)

          call core%get(p,'attitude.heading', c)
          call core%get(c, snapshots(index)%att%heading)

          call core%get(p,'alt.amsl', c)
          call core%get(c, snapshots(index)%alt%amsl)

          call core%get(p,'alt.agl', c)
          call core%get(c, snapshots(index)%alt%agl)

          call core%get(p,'dist', c)
          call core%get(c, snapshots(index)%dist)

        end if

        end do 

!=============================================================
!      ALTERNATIVE EXECUTION (FAILED)
!=============================================================

       ! do index = 1, n_children
        !write(frm, '(A,I1,A)') 'snapshots[',index,'].position.lat'
       ! call json%get(frm,snapshots(index)%pos%lat)
        !write(frm, '(A,I1,A)') 'snapshots[',index,'].position.long'
        !call json%get(frm,snapshots(index)%pos%lon)

       ! write(frm, '(A,I1,A)') 'snapshots[',index,'].attitude.pitch'
       ! call json%get(frm,snapshots(index)%att%PITCH)
        !write(frm, '(A,I1,A)') 'snapshots[',index,'].attitude.roll'
        !call json%get(frm//'.attitude.roll',snapshots(index)%att%ROLL)
       ! write(frm, '(A,I1,A)') 'snapshots[',index,'].attitude.heading'
       ! call json%get(frm,snapshots(index)%att%HEADING)

        !write(frm, '(A,I1,A)') 'snapshots[',index,'].alt.amsl'
        !call json%get(frm,snapshots(index)%alt%amsl)
        !write(frm, '(A,I1,A)') 'snapshots[',index,'].alt.agl'
        !call json%get(frm,snapshots(index)%alt%agl)
        !write(frm, '(A,I1,A)') 'snapshots[',index,'].dist'
        !call json%get(frm,snapshots(index)%dist)
        !end do
!========================================================================

        nullify(p)
        nullify(root)

        call json%destroy()
        write(*,*) ''

        write(*,*) snapshots(1)%pos%lat
        write(*,*) snapshots(1)%pos%lon
        write(*,*) ''
        !VERIFY THE VALUES (FAILS, values are set to zero)
        write(*,*) 'Verify writing in position different from the first'
        write(*,*) snapshots(2)%pos%lat
        write(*,*) snapshots(2)%pos%lon

CODE RESULT

{ 
"snapshots":[
{
  "timestamp": 0.1584527434E+13,
  "position": {
    "lat": 0.40423983622244442E+2,
    "long": 0.17687515777788889E+2,
    "alt": {
      "amsl": 0.99670000000000016E+2,
      "agl": 0.139E+2
    },
    "dist": 0.139E+2
  },
  "attitude": {
    "pitch": -0.22000000000000002E+1,
    "roll": -0.80000000000000004E+0,
    "heading": -0.25100000000000001E+2
  },
  "frame": {
    "obj": "29",
    "size": {
      "width": 640,
      "height": 360
    },
    "type": "jpeg"
  }
},
{
  "timestamp": 1584527299000,
  "position": {
    "lat": 40.42411417779999,
    "long": 17.687415777788885,
    "alt": {
      "amsl": 93.17000000000002,
      "agl": 7.4
    },
    "dist": 7.4
  },
  "attitude": {
    "pitch": -1.1,
    "roll": 0.2,
    "heading": 159.1
  },
  "frame": {
    "obj": "2",
    "size": {
      "width": 640,
      "height": 360
    },
    "type": "jpeg"
  }
  },

  {
  "timestamp": 0.1584527439E+13,
  "position": {
    "lat": 0.40424030844466664E+2,
    "long": 0.17687488000011108E+2,
    "alt": {
      "amsl": 0.99670000000000016E+2,
      "agl": 0.139E+2
    },
    "dist": 0.139E+2
  },
  "attitude": {
    "pitch": 0.57000000000000002E+1,
    "roll": -0.12E+1,
    "heading": -0.25199999999999999E+2
  },
  "frame": {
    "obj": "30",
    "size": {
      "width": 640,
      "height": 360
    },
    "type": "jpeg"
      }
      }
    ]
  }
  n_child:---->          3

  name:----> snapshots

  snap 2 lat
 40.423675288911106     

  snap 3 lat
  40.423747511133328     

  {
    FOUND CHILDREN:----> T

     Iteration:name:----> snapshots

    Iteration:n_children:---->           3
  }

{
   FOUND CHILDREN:----> F

   Iteration:name:----> 

    Iteration:n_children:---->            0
 }

 {
    FOUND CHILDREN:----> F

     Iteration:name:----> 

    Iteration:n_children:---->            0
  }

   40.424172511133328     
   17.687390777788888     

    Verify writing in position different from the first
    0.0000000000000000     
    0.0000000000000000 
RobertoLorusso commented 3 years ago

I'm sorry for the bother, I figured out that the problem was related to the user-defined type and path to variables in json file, I've substituted it with a simple array of double precision values and works it all. I'm posting the results. Anyway thank you, especially for this great library that you made. Kind Regards

          {
   FOUND CHILDREN:----> T

   Iteration:name:----> snapshots

   Iteration:n_children:---->           31
    }
    snapshots[        26].position.lat

     {
   FOUND CHILDREN:----> T

   Iteration:name:----> snapshots

   Iteration:n_children:---->           31
    }
    snapshots[        27].position.lat

    {
   FOUND CHILDREN:----> T

   Iteration:name:----> snapshots

   Iteration:n_children:---->           31
    }
    snapshots[        28].position.lat

    {
   FOUND CHILDREN:----> T

   Iteration:name:----> snapshots

   Iteration:n_children:---->           31
    }
    snapshots[        29].position.lat

    {
   FOUND CHILDREN:----> T

   Iteration:name:----> snapshots

   Iteration:n_children:---->           31
    }
    snapshots[        30].position.lat

    {
   FOUND CHILDREN:----> T

   Iteration:name:----> snapshots

   Iteration:n_children:---->           31
    }
    snapshots[        31].position.lat

   40.424172511133328
   17.687390777788888

    Verify writing in position different from the first
   40.424164177799994
   17.687390777788888
    lat:------>   40.424172511133328        40.424164177799994        40.424114177799993        40.424064177799998                40.423997511133329        40.423922511133327        40.423847511133332        40.423769733355549        40.423680844466659        40.423586400022216        40.423522511133328        40.423447511133325        40.423447511133325        40.423464177799993        40.423483622244440        40.423500288911107        40.423505844466661        40.423514177799994        40.423555844466662        40.423608622244437        40.423675288911106        40.423747511133328        40.423819733355550        40.423900288911106        40.423922511133327        40.423908622244440        40.423883622244439        40.423886400022219                                                              40.423930844466661                                                   40.423983622244442        40.424030844466664
    lon:------>   17.687390777788888        17.687390777788888        17.687415777788885        17.687449111122220        17.687496333344441        17.687546333344443        17.687596333344445        17.687643555566666        17.687701888900001        17.687763000011110        17.687804666677778        17.687854666677779        17.687860222233333        17.687910222233334        17.687976888900000        17.688040777788888        17.688051888899999        17.688043555566665        17.688021333344444        17.687993555566667        17.687954666677776        17.687915777788888        17.687874111122220        17.687826888899998        17.687813000011111        17.687743555566666        17.687657444455553        17.687579666677777        17.687538000011109        17.687515777788889        17.687488000011108