Read and write JSON content from an individual molecule file Properly handles nested structures from fragmented calculations
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | unit_in | |||
| integer, | intent(in) | :: | mol_index | |||
| integer, | intent(in) | :: | unit_out | |||
| character(len=*), | intent(in) | :: | filename |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=10000), | private, | allocatable | :: | all_lines(:) | |||
| integer, | private | :: | i | ||||
| integer, | private | :: | io_stat | ||||
| character(len=10000), | private | :: | line | ||||
| integer, | private | :: | nlines |
subroutine read_json_content(unit_in, mol_index, unit_out, filename) !! Read and write JSON content from an individual molecule file !! Properly handles nested structures from fragmented calculations use mqc_io_helpers, only: get_molecule_name integer, intent(in) :: unit_in, mol_index, unit_out character(len=*), intent(in) :: filename character(len=10000), allocatable :: all_lines(:) character(len=10000) :: line integer :: io_stat, nlines, i ! Read all lines into memory allocate (all_lines(1000)) ! Reasonable size for most JSON files nlines = 0 do read (unit_in, '(a)', iostat=io_stat) line if (io_stat /= 0) exit nlines = nlines + 1 if (nlines > size(all_lines)) then ! Reallocate if needed call logger%error("JSON file too large: "//trim(filename)) return end if all_lines(nlines) = line end do ! Lines structure: ! 1: "{" ! 2: ' "molecule_name": {' ! 3..(n-2): content ! n-1: " }" ! n: "}" if (nlines < 3) then call logger%error("Invalid JSON structure: "//trim(filename)) return end if ! Write molecule key (extracted from filename) if (mol_index > 1) write (unit_out, '(a)') ' },' write (unit_out, '(a)') ' "'//trim(get_molecule_name(filename))//'" : {' ! Write all content lines (from line 3 to line n-2) do i = 3, nlines - 2 write (unit_out, '(a)') ' '//trim(all_lines(i)) ! Add 2 spaces for proper indentation end do deallocate (all_lines) end subroutine read_json_content