Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix #203

Closed
rudy26190 opened this issue Jun 10, 2016 · 4 comments
Closed

Matrix #203

rudy26190 opened this issue Jun 10, 2016 · 4 comments

Comments

@rudy26190
Copy link

rudy26190 commented Jun 10, 2016

Hi Jacob,

Apparently it is not possible to create a matrix (in the form arrays of arrays) with json fortran and the matrix must be reshaped as in the test_12.f90 (provided with the json fortran src code). I checked whether the code below (containing arrays of arrays ) was a valid json format and it was according to "jsonlint.com".
Is it possible to extend json fortran to take into account that kind of structure ?

Thanks a lot in advance,
Rudy

{
    "ShipProfile": {
        "WindMatrix": [
            [
                100,
                100,
                100,
                100,
                100
            ],
            [
                100,
                100,
                100,
                100,
                100
            ],
            [
                95,
                96,
                98,
                100,
                101
            ],
            [
                85,
                87,
                93,
                99,
                101
            ],
            [
                69,
                74,
                86,
                97,
                102
            ],
            [
                46,
                55,
                75,
                94,
                102
            ]
        ]

    }
}
@jacobwilliams
Copy link
Owner

Actually, I think JSON-Fortran can read and write a file like this (need to verify though). However, there isn't an API to automatically get/set WindMatrix to/from a WindMatrix(:,:) Fortran variable. That's the part you would have to do manually. Note: I have another ticket to add this capability (see #156) but haven't finished it yet.

@rudy26190
Copy link
Author

Thanks for your answer.
Rudy.

On 10 June 2016 at 15:33, Jacob Williams [email protected] wrote:

Actually, I think JSON-Fortran can read and write a file like this (need
to verify though). However, there isn't an API to automatically get/set
WindMatrix to/from a WindMatrix(:,:) Fortran variable. That's the part
you would have to do manually. Note: I have another ticket to add this
capability (see #156
#156) but haven't
finished it yet.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#203 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AS7u-wyA-0K1RssoxmSU8ScJVEAuWaCgks5qKWfCgaJpZM4Iysnp
.

@jacobwilliams
Copy link
Owner

Here is a way to do it (assuming data is in the file test.json):

program test

use json_module
use iso_fortran_env,    only: output_unit

implicit none

type(json_file) :: json
integer,dimension(:),allocatable :: ivec
integer,dimension(:,:),allocatable :: imat
integer :: i,n_cols,n_rows,var_type
logical :: found
type(json_value),pointer :: WindMatrix,child
type(json_core) :: core

!load the file and print it to console:
call json%load_file('test.json')
if (json%failed()) error stop 'error loading file'

!get number of rows and columns
!assuming data stored by column
!assuming each column has the same number of elements,
!and is the same data type (integer in this case):
call json%info('ShipProfile.WindMatrix',found,var_type,n_cols)
if (.not. found) error stop 'error: ShipProfile.WindMatrix not found'

call json%info('ShipProfile.WindMatrix(1)',found,var_type,n_rows)
if (.not. found) error stop 'error: ShipProfile.WindMatrix(1) not found'

!get a pointer to the wind matrix:
call json%get('ShipProfile.WindMatrix',WindMatrix)
if (.not. associated(WindMatrix)) error stop 'error: ShipProfile.WindMatrix not found'

!size the array:
allocate(imat(n_rows,n_cols))

!grab each column of the windmatrix:
! [we need a json_core for this so we can use json_value_get_by_index]
do i=1,n_cols
    call core%get_child(WindMatrix,i,child)
    if (.not. associated(child)) error stop 'error: column not found'
    call core%get(child,ivec) !get the vector of integers (column of the matrix)
    if (.not. allocated(ivec)) error stop 'error: could not get integer column'
    if (size(ivec)/=n_rows) error stop 'error: column is wrong size'
    imat(:,i) = ivec
    deallocate(ivec)
    nullify(child)
end do
nullify(WindMatrix)

write(*,*) ''
write(*,*) 'matrix:'
do i=1,n_rows
    write(*,*) imat(i,:)
end do
write(*,*) ''

end program test

So, you basically read the matrix column by column. The result is:

 matrix:
         100         100          95          85          69          46
         100         100          96          87          74          55
         100         100          98          93          86          75
         100         100         100          99          97          94
         100         100         101         101         102         102

@eriiikj
Copy link

eriiikj commented Jun 27, 2022

Hi,

Is there a procedure for writing a similar file, i.e. a nested list with the format
{
"vec": [
[
1,
2,
],
[
3,
4,
]
]
}

I try the following code:
type(json_core) :: core
type(json_value),pointer :: p, inp

call core%initialize()
call core%create_object(p,'s')
call core%add(p, 'vec',[[1,2],[3,4]])
call core%print(p,'nested_out.json')
call core%destroy()

But this does not produce a nested output:

{
"vec": [
1,
2,
3,
4
]
}

Would be great if there was a solution to this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants