-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.f90
executable file
·4181 lines (3235 loc) · 119 KB
/
json.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!*****************************************************************************************
module json_m
!*****************************************************************************************
!****h* JSON/json_module
!
! NAME
! json_module
!
! DESCRIPTION
! JSON-FORTRAN: A Fortran 2008 JSON (JavaScript Object Notation) API.
!
! NOTES
! -Based on fson by Joseph A. Levin (see License below)
! -The original F95 code was split into four files:
! fson_path_m.f95, fson_string_m.f95, fson_value_m.f95, fson.f95
! -The code has been extensively modified and combined into this
! one module (json_module.f90).
! -Some Fortran 2003/2008 features are now used
! (e.g., allocatable strings, associate, newunit, generic, class,
! and abstract interface)
! -The headers in this file follow the ROBODoc conventions.
! Compile with: robodoc --src ./ --doc ./doc --multidoc --html
! --tabsize 4 --ignore_case_when_linking
! --syntaxcolors --source_line_numbers --index
!
! HISTORY
! Joseph A. Levin : March 2012
! Jacob Williams : 2/8/2013 : Extensive modifications to the original code.
!
! SEE ALSO
! [1] http://github.com/jacobwilliams/json-fortran [json-fortran development site]
! [2] http://jacobwilliams.github.io/json-fortran [json-fortran online documentation]
! [3] http://www.json.org/ [JSON website]
! [4] http://jsonlint.com/ [JSON validator]
!
! COPYRIGHT
!
! -------------------------------------------------------------------------------------
! json-fortran License:
!
! JSON-FORTRAN: A Fortran 2003/2008 JSON API
! http://github.com/jacobwilliams/json-fortran
!
! Copyright (c) 2014, Jacob Williams
! All rights reserved.
!
! Redistribution and use in source and binary forms, with or without modification,
! are permitted provided that the following conditions are met:
!
! * Redistributions of source code must retain the above copyright notice, this
! list of conditions and the following disclaimer.
!
! * Redistributions in binary form must reproduce the above copyright notice, this
! list of conditions and the following disclaimer in the documentation and/or
! other materials provided with the distribution.
!
! * The names of its contributors may not be used to endorse or promote products
! derived from this software without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
! WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
! DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
! ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
! (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
! ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
! -------------------------------------------------------------------------------------
! Original FSON License:
!
! http://github.com/josephalevin/fson [FSON code retrieved on 12/2/2013]
!
! Copyright (c) 2012 Joseph A. Levin
!
! Permission is hereby granted, free of charge, to any person obtaining a copy of this
! software and associated documentation files (the "Software"), to deal in the Software
! without restriction, including without limitation the rights to use, copy, modify, merge,
! publish, distribute, sublicense, and/or sell copies of the Software, and to permit
! persons to whom the Software is furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all copies or
! substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
! INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
! PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
! LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
! OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
! DEALINGS IN THE SOFTWARE.
!
!*****************************************************************************************
implicit none
private
!parameters:
character(len=*),parameter,public :: json_ext = '.json' !JSON file extension
character(len=1),parameter :: space = ' ' !special characters
character(len=1),parameter :: bspace = achar(8)
character(len=1),parameter :: horizontal_tab = achar(9)
character(len=1),parameter :: newline = achar(10)
character(len=1),parameter :: formfeed = achar(12)
character(len=1),parameter :: carriage_return = achar(13)
character(len=1),parameter :: quotation_mark = achar(34)
character(len=1),parameter :: slash = achar(47)
character(len=1),parameter :: backslash = achar(92)
character(len=*),parameter :: real_fmt = '(ES30.16E3)' !format for real numbers
character(len=*),parameter :: int_fmt = '(I10)' !format for integers
! The types of data:
integer,parameter,public :: json_unknown = 0
integer,parameter,public :: json_null = 1
integer,parameter,public :: json_object = 2
integer,parameter,public :: json_array = 3
integer,parameter,public :: json_logical = 4
integer,parameter,public :: json_integer = 5
integer,parameter,public :: json_real = 6
integer,parameter,public :: json_string = 7
!*********************************************************
!****ic* json_module/json_data_non_polymorphic
!
! NAME
! json_data_non_polymorphic
!
! DESCRIPTION
! The data in a json_value class
!
! SOURCE
type :: json_data_non_polymorphic
integer :: var_type = json_unknown
logical,allocatable :: log_value
integer,allocatable :: int_value
real,allocatable :: dbl_value
character(len=:),allocatable :: str_value
contains
procedure :: destroy => destroy_json_data_non_polymorphic
end type json_data_non_polymorphic
!*********************************************************
!*********************************************************
!****c* json_module/json_value
!
! NAME
! json_value
!
! DESCRIPTION
! Type used to construct the linked-list json structure
!
! SOURCE
type,public :: json_value
!variable name:
character(len=:),allocatable :: name
!the data for this variable:
type(json_data_non_polymorphic) :: data
!for the linked list:
type(json_value), pointer :: next => null()
type(json_value), pointer :: parent => null()
type(json_value), pointer :: children => null()
end type json_value
!*********************************************************
!*********************************************************
type,public :: json_file
!*********************************************************
!****c* json_module/json_file
!
! NAME
! json_file
!
! DESCRIPTION
! The json_file is the main public class that is
! used to open a file and get data from it.
!
! EXAMPLE
! type(json_file) :: json
! call json%load_file(filename)
! call json%print_file()
! call json%get('var.i',ival,found)
! call json%get('var.d',rval,found)
! call json%get('var.c',cval,found)
! call json%destroy()
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
private
type(json_value), pointer :: p => null() !the JSON structure read from the file
contains
procedure,public :: load_file => load_json_file
procedure,public :: print_file => print_json_file
procedure,public :: destroy => destroy_json_file
procedure,public :: info => variable_info_in_file
generic,public :: get => get_pointer,&
get_integer,&
get_double,&
get_logical,&
get_chars,&
get_double_vec,&
get_char_vec,&
get_integer_vec,&
get_logical_vec
!scalars:
procedure :: get_pointer => get_object_from_json_file
procedure :: get_integer => get_integer_from_json_file
procedure :: get_double => get_double_from_json_file
procedure :: get_logical => get_logical_from_json_file
procedure :: get_chars => get_chars_from_json_file
!vectors:
procedure :: get_integer_vec => get_integer_vec_from_json_file
procedure :: get_double_vec => get_double_vec_from_json_file
procedure :: get_logical_vec => get_logical_vec_from_json_file
procedure :: get_char_vec => get_char_vec_from_json_file
!*********************************************************
end type json_file
!*********************************************************
!array element callback function
abstract interface
subroutine array_callback_func(element, i, count)
import :: json_value
implicit none
type(json_value), pointer,intent(in) :: element
integer,intent(in) :: i !index
integer,intent(in) :: count !size of array
end subroutine array_callback_func
end interface
interface json_value_get !consider renaming this json_value_get_child
module procedure get_by_index
module procedure get_by_name_chars
end interface json_value_get
interface json_value_add
module procedure :: json_value_add_member
module procedure :: json_value_add_integer, json_value_add_integer_vec
module procedure :: json_value_add_real, json_value_add_real_vec
module procedure :: json_value_add_logical, json_value_add_logical_vec
module procedure :: json_value_add_string, json_value_add_string_vec
end interface json_value_add
interface json_get
module procedure :: json_get_by_path
module procedure :: json_get_integer, json_get_integer_vec
module procedure :: json_get_double, json_get_double_vec
module procedure :: json_get_logical, json_get_logical_vec
module procedure :: json_get_chars, json_get_char_vec
module procedure :: json_get_array
end interface json_get
interface json_print_to_string
module procedure :: json_value_to_string
end interface
interface json_destroy
module procedure :: json_value_destroy
end interface
!public routines:
public :: json_initialize !to initialize the module
public :: json_destroy !clear a JSON structure (destructor)
public :: json_parse !read a JSON file and populate the structure
public :: json_clear_exceptions !clear exceptions
public :: json_check_for_errors !check for error and get error message
public :: json_failed !check for error
public :: json_value_get !use either a 1 based index or member
! name to get a json_value.
public :: json_value_add !add data to a JSON structure
public :: json_get !get data from the JSON structure
public :: json_print !print the JSON structure to a file
public :: json_print_to_string !write the JSON structure to a string
public :: json_value_create !initialize a json_value pointer
public :: json_value_count !count the number of children
public :: json_info !get info about a json_value
public :: to_logical !set the data type of a json_value
public :: to_integer !
public :: to_string !
public :: to_real !
public :: to_null !
public :: to_object !
public :: to_array !
public :: integer_to_string !basic integer to string routine
!exception handling [private variables]
logical :: exception_thrown = .false. !the error flag
character(len=:),allocatable :: err_message !the error message
logical,parameter :: print_tracebacks = .false. !used when debugging
! POP/PUSH CHARACTER [private variables]
integer :: char_count = 0
integer :: line_count = 1
integer :: pushed_index = 0
character (len = 10) :: pushed_char !JW : what is this magic number 10??
contains
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/destroy_json_data_non_polymorphic
!
! NAME
! destroy_json_data_non_polymorphic
!
! USAGE
! call me%destroy()
!
! DESCRIPTION
! Destroy the nonpolymorphic data type.
!
! AUTHOR
! Jacob Williams
!
! SOURCE
subroutine destroy_json_data_non_polymorphic(me)
implicit none
class(json_data_non_polymorphic),intent(inout) :: me
me%var_type = 0
if (allocated(me%log_value)) deallocate(me%log_value)
if (allocated(me%int_value)) deallocate(me%int_value)
if (allocated(me%dbl_value)) deallocate(me%dbl_value)
if (allocated(me%str_value)) deallocate(me%str_value)
end subroutine destroy_json_data_non_polymorphic
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/destroy_json_file
!
! NAME
! destroy_json_file
!
! USAGE
! call me%destroy()
!
! DESCRIPTION
! Destroy the JSON file.
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
subroutine destroy_json_file(me)
implicit none
class(json_file),intent(inout) :: me
if (associated(me%p)) call json_value_destroy(me%p)
end subroutine destroy_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/load_json_file
!
! NAME
! load_json_file
!
! USAGE
! call me%load_file(filename)
!
! DESCRIPTION
! Load the JSON file.
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
subroutine load_json_file(me, filename)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: filename
call json_parse(filename, me%p)
end subroutine load_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/print_json_file
!
! NAME
! print_json_file
!
! USAGE
! call me%print_file()
!
! DESCRIPTION
! Print the JSON file.
! Prints to the specified file unit (if not present, then prints to the console)
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
subroutine print_json_file(me, iunit)
use, intrinsic :: iso_fortran_env, only: output_unit
implicit none
class(json_file),intent(inout) :: me
integer,intent(in),optional :: iunit !must be non-zero
integer :: i
character(len=:),allocatable :: dummy
if (present(iunit)) then
if (iunit/=0) then
i = iunit
else
call throw_exception('Error in print_json_file: iunit must be nonzero.')
return
end if
else
i = output_unit
end if
call json_value_print(me%p,iunit=i,str=dummy)
end subroutine print_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/variable_info_in_file
!
! NAME
! variable_info_in_file
!
! USAGE
! call me%info(path,found,var_type,n_children)
!
! DESCRIPTION
! Returns information about a variable in a file.
!
! AUTHOR
! Jacob Williams : 2/3/2014
!
! SOURCE
subroutine variable_info_in_file(me,path,found,var_type,n_children)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
logical,intent(out) :: found
integer,intent(out) :: var_type
integer,intent(out) :: n_children
type(json_value),pointer :: p
!initialize:
nullify(p)
!get a pointer to the variable (if it is there):
call me%get(path,p,found)
if (found) then
!get info:
call json_info(p,var_type,n_children)
else
!set to dummy values:
var_type = json_unknown
n_children = 0
end if
!cleanup:
nullify(p)
end subroutine variable_info_in_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/json_info
!
! NAME
! json_info
!
! USAGE
! call me%info(path,found,var_type,n_children)
!
! DESCRIPTION
! Returns information about a json_value
!
! AUTHOR
! Jacob Williams : 2/13/2014
!
! SOURCE
subroutine json_info(p,var_type,n_children)
implicit none
type(json_value),pointer :: p
integer,intent(out),optional :: var_type
integer,intent(out),optional :: n_children
if (present(var_type)) var_type = p%data%var_type !variable type
if (present(n_children)) n_children = json_value_count(p) !number of children
end subroutine json_info
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_object_from_json_file
!
! NAME
! get_object_from_json_file
!
! USAGE
! call me%get(path,p,found)
!
! DESCRIPTION
! Get a pointer to an object from a JSON file.
!
! AUTHOR
! Jacob Williams : 2/3/2014
!
! SOURCE
subroutine get_object_from_json_file(me, path, p, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
type(json_value),pointer,intent(out) :: p
logical,intent(out),optional :: found
call json_get_by_path(me%p, path=path, p=p, found=found)
end subroutine get_object_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_integer_from_json_file
!
! NAME
! get_integer_from_json_file
!
! USAGE
! call me%get(path,val)
!
! DESCRIPTION
! Get an integer from a JSON file.
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
subroutine get_integer_from_json_file(me, path, val, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
integer,intent(out) :: val
logical,intent(out),optional :: found
call json_get(me%p, path=path, value=val, found=found)
end subroutine get_integer_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_integer_vec_from_json_file
!
! NAME
! get_integer_vec_from_json_file
!
! USAGE
! call me%get(path,vec)
!
! DESCRIPTION
! Get an integer vector from a JSON file.
!
! AUTHOR
! Jacob Williams : 1/20/2014
!
! SOURCE
subroutine get_integer_vec_from_json_file(me, path, vec, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
integer,dimension(:),allocatable,intent(out) :: vec
logical,intent(out),optional :: found
call json_get(me%p, path, vec, found)
end subroutine get_integer_vec_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_double_from_json_file
!
! NAME
! get_double_from_json_file
!
! USAGE
! call me%get(path,val,found)
!
! DESCRIPTION
! Get a double from a JSON file.
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
subroutine get_double_from_json_file (me, path, val, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
real,intent(out) :: val
logical,intent(out),optional :: found
call json_get(me%p, path=path, value=val, found=found)
end subroutine get_double_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_double_vec_from_json_file
!
! NAME
! get_double_vec_from_json_file
!
! USAGE
! call me%get(path,vec,found)
!
! DESCRIPTION
! Get a double vector from a JSON file.
!
! AUTHOR
! Jacob Williams : 1/19/2014
!
! SOURCE
subroutine get_double_vec_from_json_file(me, path, vec, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
real,dimension(:),allocatable,intent(out) :: vec
logical,intent(out),optional :: found
call json_get(me%p, path, vec, found)
end subroutine get_double_vec_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_logical_from_json_file
!
! NAME
! get_logical_from_json_file
!
! USAGE
! call me%get(path,val,found)
!
! DESCRIPTION
! Get an logical from a JSON file.
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
subroutine get_logical_from_json_file(me,path,val,found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
logical,intent(out) :: val
logical,intent(out),optional :: found
call json_get(me%p, path=path, value=val, found=found)
end subroutine get_logical_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_logical_vec_from_json_file
!
! NAME
! get_logical_vec_from_json_file
!
! USAGE
! call me%get(path,vec)
!
! DESCRIPTION
! Get a logical vector from a JSON file.
!
! AUTHOR
! Jacob Williams : 1/20/2014
!
! SOURCE
subroutine get_logical_vec_from_json_file(me, path, vec, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
logical,dimension(:),allocatable,intent(out) :: vec
logical,intent(out),optional :: found
call json_get(me%p, path, vec, found)
end subroutine get_logical_vec_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_chars_from_json_file
!
! NAME
! get_chars_from_json_file
!
! USAGE
! call me%get(path,val)
!
! DESCRIPTION
! Get a character string from a json file.
! Note: val is an allocatable character string.
!
! AUTHOR
! Jacob Williams : 12/9/2013
!
! SOURCE
subroutine get_chars_from_json_file(me, path, val, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
character(len=:),allocatable,intent(out) :: val
logical,intent(out),optional :: found
call json_get(me%p, path=path, value=val, found=found)
end subroutine get_chars_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/get_char_vec_from_json_file
!
! NAME
! get_char_vec_from_json_file
!
! USAGE
! call me%get(path,vec)
!
! DESCRIPTION
! Get a char vector from a JSON file.
!
! AUTHOR
! Jacob Williams : 1/19/2014
!
! SOURCE
subroutine get_char_vec_from_json_file(me, path, vec, found)
implicit none
class(json_file),intent(inout) :: me
character(len=*),intent(in) :: path
character(len=*),dimension(:),allocatable,intent(out) :: vec
logical,intent(out),optional :: found
call json_get(me%p, path, vec, found)
end subroutine get_char_vec_from_json_file
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/json_initialize
!
! NAME
! json_initialize
!
! DESCRIPTION
! Initialize the module.
! Should be called before the routines are used.
! It can also be called after using the module and encountering exceptions.
!
! AUTHOR
! Jacob Williams : 12/4/2013
!
! SOURCE
subroutine json_initialize()
implicit none
!clear any errors from previous runs:
call json_clear_exceptions()
!Just in case, clear these global variables also:
pushed_index = 0
pushed_char = ''
char_count = 0
line_count = 1
end subroutine json_initialize
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/json_clear_exceptions
!
! NAME
! json_clear_exceptions
!
! DESCRIPTION
! Clear exceptions in the JSON module.
!
! AUTHOR
! Jacob Williams : 12/4/2013
!
! SOURCE
subroutine json_clear_exceptions()
implicit none
!clear the flag and message:
exception_thrown = .false.
err_message = ''
end subroutine json_clear_exceptions
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/throw_exception
!
! NAME
! throw_exception
!
! DESCRIPTION
! Throw an exception in the JSON module.
! This routine sets the error flag, and prevents any subsequent routine
! from doing anything, until json_clear_exceptions is called.
!
! AUTHOR
! Jacob Williams : 12/4/2013
!
! SOURCE
subroutine throw_exception(msg)
!use ifcore, only: tracebackqq !Intel routine
implicit none
character(len=*),intent(in) :: msg !the error message
exception_thrown = .true.
err_message = trim(msg)
!if (print_tracebacks) then
!
! !This is a feature of the Intel Fortran Compiler:
! !Throw a traceback and return control to the user.
! call tracebackqq(string=trim(msg), user_exit_code=-1)
!
! !write(*,'(A)') trim(msg) !gfortran version
! !stop
!
!end if
end subroutine throw_exception
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/json_check_for_errors
!
! NAME
! json_check_for_errors
!
! DESCRIPTION
! Retrieve error code from the module.
! This should be called after json_parse to check for errors.
! If an error is thrown, before using the module again, json_initialize
! should be called to clean up before it is used again.
!
! AUTHOR
! Jacob Williams : 12/4/2013
!
! SOURCE
subroutine json_check_for_errors(status_ok, error_msg)
implicit none
logical,intent(out) :: status_ok
character(len=:),allocatable,intent(out) :: error_msg
status_ok = .not. exception_thrown
if (.not. status_ok) then
if (allocated(err_message)) then
error_msg = err_message
else
error_msg = 'Unknown Error'
end if
else
error_msg = ''
end if
end subroutine json_check_for_errors
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/json_failed
!
! NAME
! json_failed
!
! DESCRIPTION
! Logical function to indicate if an exception has been thrown.
!
! USAGE
! if (json_failed()) then
! !do something about it
! call json_clear_exceptions()
! end if
!
! AUTHOR
! Jacob Williams : 12/5/2013
!
! SOURCE
function json_failed() result(failed)
implicit none
logical :: failed
failed = exception_thrown
end function json_failed
!*****************************************************************************************
!*****************************************************************************************
!****f* json_module/json_value_create
!
! NAME
! json_value_create