@@ -505,6 +505,12 @@ module json_value_module
505
505
generic,public :: rename = > MAYBEWRAP(json_value_rename)
506
506
procedure :: MAYBEWRAP(json_value_rename)
507
507
508
+ ! >
509
+ ! get info about a json_value
510
+ generic,public :: info = > json_info, MAYBEWRAP(json_info_by_path)
511
+ procedure :: json_info
512
+ procedure :: MAYBEWRAP(json_info_by_path)
513
+
508
514
procedure ,public :: remove = > json_value_remove ! ! Remove a [[json_value]] from a linked-list structure.
509
515
procedure ,public :: check_for_errors = > json_check_for_errors ! ! check for error and get error message
510
516
procedure ,public :: clear_exceptions = > json_clear_exceptions ! ! clear exceptions
@@ -515,7 +521,6 @@ module json_value_module
515
521
procedure ,public :: get_next = > json_get_next ! ! get pointer to json_value next
516
522
procedure ,public :: get_previous = > json_get_previous ! ! get pointer to json_value previous
517
523
procedure ,public :: get_tail = > json_get_tail ! ! get pointer to json_value tail
518
- procedure ,public :: info = > json_info ! ! get info about a json_value
519
524
procedure ,public :: initialize = > json_initialize ! ! to initialize some parsing parameters
520
525
procedure ,public :: traverse = > json_traverse ! ! to traverse all elements of a JSON structure
521
526
procedure ,public :: print_error_message = > json_print_error_message ! ! simply routine to print error messages
@@ -979,6 +984,177 @@ subroutine json_info(json,p,var_type,n_children,name)
979
984
end subroutine json_info
980
985
! *****************************************************************************************
981
986
987
+ ! *****************************************************************************************
988
+ !
989
+ ! Returns information about a [[json_value]], given the path.
990
+ !
991
+ ! ### See also
992
+ ! * [[json_info]]
993
+
994
+ subroutine json_info_by_path (json ,p ,path ,found ,var_type ,n_children ,name )
995
+
996
+ implicit none
997
+
998
+ class(json_core),intent (inout ) :: json
999
+ type (json_value),pointer ,intent (in ) :: p ! ! a JSON linked list
1000
+ character (kind= CK,len=* ),intent (in ) :: path ! ! path to the variable
1001
+ logical (LK),intent (out ),optional :: found ! ! true if it was found
1002
+ integer (IK),intent (out ),optional :: var_type ! ! variable type
1003
+ integer (IK),intent (out ),optional :: n_children ! ! number of children
1004
+ character (kind= CK,len= :),allocatable ,intent (out ),optional :: name ! ! variable name
1005
+
1006
+ type (json_value),pointer :: p_var
1007
+ logical (LK) :: p_var_found
1008
+
1009
+ call json% get(p,path,p_var,found)
1010
+
1011
+ ! check if it was found:
1012
+ if (present (found)) then
1013
+ if (.not. found) then
1014
+ if (present (var_type)) var_type = json_unknown
1015
+ if (present (n_children)) n_children = 0
1016
+ if (present (name)) name = ' '
1017
+ return
1018
+ end if
1019
+ else
1020
+ if (json% failed()) return
1021
+ end if
1022
+
1023
+ ! get info:
1024
+ call json% info(p_var,var_type,n_children,name)
1025
+
1026
+ end subroutine json_info_by_path
1027
+ ! *****************************************************************************************
1028
+
1029
+ ! *****************************************************************************************
1030
+ ! >
1031
+ ! Alternate version of [[json_info_by_path]] where "path" is kind=CDK.
1032
+
1033
+ subroutine wrap_json_info_by_path (json ,p ,path ,found ,var_type ,n_children ,name )
1034
+
1035
+ implicit none
1036
+
1037
+ class(json_core),intent (inout ) :: json
1038
+ type (json_value),pointer ,intent (in ) :: p ! ! a JSON linked list
1039
+ character (kind= CDK,len=* ),intent (in ) :: path ! ! path to the variable
1040
+ logical (LK),intent (out ),optional :: found ! ! true if it was found
1041
+ integer (IK),intent (out ),optional :: var_type ! ! variable type
1042
+ integer (IK),intent (out ),optional :: n_children ! ! number of children
1043
+ character (kind= CK,len= :),allocatable ,intent (out ),optional :: name ! ! variable name
1044
+
1045
+ call json% info(p,to_unicode(path),found,var_type,n_children,name)
1046
+
1047
+ end subroutine wrap_json_info_by_path
1048
+ ! *****************************************************************************************
1049
+
1050
+ ! *****************************************************************************************
1051
+ ! > author: Jacob Williams
1052
+ ! date: 10/16/2015
1053
+ !
1054
+ ! Alternate version of [[json_info]] that returns matrix
1055
+ ! information about a [[json_value]].
1056
+ !
1057
+ ! A [[json_value]] is a valid rank 2 matrix if all of the following are true:
1058
+ !
1059
+ ! * The var_type is *json_array*
1060
+ ! * Each child is also a *json_array*, each of which has the same number of elements
1061
+ ! * Each individual element has the same variable type (integer, logical, etc.)
1062
+ !
1063
+ ! The idea here is that if it is a valid matrix, it can be interoperable with
1064
+ ! a Fortran rank 2 array of the same type.
1065
+ !
1066
+ ! # Example
1067
+ !
1068
+ ! The following example is an array with `var_type=json_integer`, `n_sets=3`, and `set_size=4`
1069
+ !
1070
+ ! ```json
1071
+ ! {
1072
+ ! "matrix": [
1073
+ ! [1,2,3,4],
1074
+ ! [5,6,7,8],
1075
+ ! [9,10,11,12]
1076
+ ! ]
1077
+ ! }
1078
+ ! ```
1079
+
1080
+ subroutine json_matrix_info (json ,p ,is_matrix ,var_type ,n_sets ,set_size ,name )
1081
+
1082
+ implicit none
1083
+
1084
+ class(json_core),intent (inout ) :: json
1085
+ type (json_value),pointer :: p ! ! a JSON linked list
1086
+ logical (LK),intent (out ) :: is_matrix ! ! true if it is a valid matrix
1087
+ integer (IK),intent (out ),optional :: var_type ! ! variable type (if all elements have the same type)
1088
+ integer (IK),intent (out ),optional :: n_sets ! ! number of data sets (i.e., matrix rows if using row-major order)
1089
+ integer (IK),intent (out ),optional :: set_size ! ! size of each data set (i.e., matrix cols if using row-major order)
1090
+ character (kind= CK,len= :),allocatable ,intent (out ),optional :: name ! ! variable name
1091
+
1092
+ type (json_value),pointer :: p_row ! ! for getting a set
1093
+ type (json_value),pointer :: p_element ! ! for getting an element in a set
1094
+ integer (IK) :: vartype ! ! json variable type of `p`
1095
+ integer (IK) :: row_vartype ! ! json variable type of a row
1096
+ integer (IK) :: element_vartype ! ! json variable type of an element in a row
1097
+ integer (IK) :: nr ! ! number of children of `p`
1098
+ integer (IK) :: nc ! ! number of elements in first child of `p`
1099
+ integer (IK) :: icount ! ! number of elements in a set
1100
+ integer :: i ! ! counter
1101
+ integer :: j ! ! counter
1102
+
1103
+ ! get info about the variable:
1104
+ call json% info(p,vartype,nr,name) ! name is set here if present
1105
+
1106
+ is_matrix = (vartype== json_array)
1107
+
1108
+ if (is_matrix) then
1109
+
1110
+ main : do i= 1 ,nr
1111
+
1112
+ call json% get_child(p,i,p_row)
1113
+ call json% info(p_row,var_type= row_vartype,n_children= icount)
1114
+
1115
+ if (row_vartype== json_array) then
1116
+ if (i== 1 ) nc = icount ! number of columns in first row
1117
+ if (icount== nc) then ! make sure each row has the same number of columns
1118
+ ! see if all the variables in this row are the same type:
1119
+ do j= 1 ,icount
1120
+ call json% get_child(p_row,j,p_element)
1121
+ call json% info(p_element,var_type= element_vartype)
1122
+ if (i== 1 .and. j== 1 ) vartype = element_vartype ! type of first element in the row
1123
+ if (vartype/= element_vartype) then
1124
+ ! not all variables are the same time
1125
+ is_matrix = .false.
1126
+ exit main
1127
+ end if
1128
+ end do
1129
+ else
1130
+ is_matrix = .false.
1131
+ exit main
1132
+ end if
1133
+ else
1134
+ is_matrix = .false.
1135
+ exit main
1136
+ end if
1137
+
1138
+ end do main
1139
+
1140
+ end if
1141
+
1142
+ if (is_matrix) then
1143
+ if (present (var_type)) var_type = vartype
1144
+ if (present (n_sets)) n_sets = nr
1145
+ if (present (set_size)) set_size = nc
1146
+ else
1147
+ if (present (var_type)) var_type = json_unknown
1148
+ if (present (n_sets)) n_sets = 0
1149
+ if (present (set_size)) set_size = 0
1150
+ end if
1151
+
1152
+ nullify(p_row)
1153
+ nullify(p_element)
1154
+
1155
+ end subroutine json_matrix_info
1156
+ ! *****************************************************************************************
1157
+
982
1158
! *****************************************************************************************
983
1159
! > author: Jacob Williams
984
1160
! date: 4/29/2016
0 commit comments