jacobwilliams / json-fortran

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

Write Update values on JSON File #430

Closed nyckmaia closed 5 years ago

nyckmaia commented 5 years ago

I would like to update / replace the old JSON file values by new ones:

I have a simple JSON file like this:

{
  "height": 1,
  "weight": 2,
  "size": 3
}

I tried to use the json%update function but this function do not overwrite the new values on the actual JSON file.

subroutine fortran_subroutine() bind(C)
    use, intrinsic :: ISO_C_BINDING

        use json_module        
        implicit none

        type(json_file) :: json
        logical :: found
        integer :: height, weight, size

        ! initialize the class
        call json%initialize()

        ! read the file
        call json%load_file(filename = 'my_json.json')

        call json%update('height', 100, found)
        call json%update('weight', 200, found)
        call json%update('size', 300, found)

        ! clean up
        call json%destroy()
        if (json%failed()) stop 1

end subroutine fortran_subroutine

After the execution of this subroutine, I would like that my_json should be like:

{
  "height": 100,
  "weight": 200,
  "size": 300
}

How can I update the values direct on my JSON file?

nyckmaia commented 5 years ago

Ok...I found...

call json%print_file("my_json.json")