read_json Subroutine

public subroutine read_json(self, unit, error)

Arguments

Type IntentOptional Attributes Name
type(structure_type), intent(out) :: self

Instance of the molecular structure data

integer, intent(in) :: unit

File handle

type(error_type), intent(out), allocatable :: error

Error handling


Source Code

subroutine read_json(self, unit, error)

   !> Instance of the molecular structure data
   type(structure_type), intent(out) :: self

   !> File handle
   integer, intent(in) :: unit

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

#if WITH_JSON
   class(json_value), allocatable :: root
   type(json_object), pointer :: object
   type(json_error), allocatable :: parse_error
   type(json_context) :: ctx

   call json_load(root, unit, config=json_parser_config(context_detail=1), &
      & context=ctx, error=parse_error)
   if (allocated(parse_error)) then
      allocate(error)
      call move_alloc(parse_error%message, error%message)
      return
   end if
   object => cast_to_object(root)
   if (.not.associated(object)) then
      call fatal_error(error, ctx%report("Invalid JSON object", root%origin, "Expected JSON object"))
      return
   end if

   ! QCSchema JSON uses "schema_name" and "schema_version" keys
   if (object%has_key("schema_name") .or. object%has_key("schema_version")) then
      call read_qcschema(self, object, ctx, error)
      return
   end if

   ! Pymatgen serialized via monty adds "@module" and "@class" keys
   if (object%has_key("@module") .or. object%has_key("@class")) then
      call read_pymatgen(self, object, ctx, error)
      return
   end if

   ! Chemical JSON (cjson) tracks version via "chemical json" or "chemicalJson" keys
   if (object%has_key("chemical json") .or. object%has_key("chemicalJson")) then
      call read_cjson(self, object, ctx, error)
      return
   end if

   ! Default to QCSchema if no specific schema is detected
   call read_qcschema(self, object, ctx, error)
#else
   call fatal_error(error, "JSON support not enabled")
#endif
end subroutine read_json