-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathjson_value_module.F90
11600 lines (9200 loc) · 448 KB
/
json_value_module.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
!*****************************************************************************************
!> author: Jacob Williams
! license: BSD
!
! This module provides a low-level interface for manipulation of JSON data.
! The two public entities are [[json_value]], and [[json_core(type)]].
! The [[json_file_module]] provides a higher-level interface to some
! of these routines.
!
!### License
! * JSON-Fortran is released under a BSD-style license.
! See the [LICENSE](https://github.com/jacobwilliams/json-fortran/blob/master/LICENSE)
! file for details.
module json_value_module
use,intrinsic :: iso_fortran_env, only: iostat_end,error_unit,output_unit
use,intrinsic :: ieee_arithmetic
use json_kinds
use json_parameters
use json_string_utilities
implicit none
private
#include "json_macros.inc"
!*********************************************************
!>
! If Unicode is not enabled, then
! JSON files are opened using access='STREAM' and
! form='UNFORMATTED'. This allows the file to
! be read faster.
!
#ifdef USE_UCS4
logical,parameter :: use_unformatted_stream = .false.
#else
logical,parameter :: use_unformatted_stream = .true.
#endif
!*********************************************************
!*********************************************************
!>
! If Unicode is not enabled, then
! JSON files are opened using access='STREAM' and
! form='UNFORMATTED'. This allows the file to
! be read faster.
!
#ifdef USE_UCS4
character(kind=CDK,len=*),parameter :: access_spec = 'SEQUENTIAL'
#else
character(kind=CDK,len=*),parameter :: access_spec = 'STREAM'
#endif
!*********************************************************
!*********************************************************
!>
! If Unicode is not enabled, then
! JSON files are opened using access='STREAM' and
! form='UNFORMATTED'. This allows the file to
! be read faster.
!
#ifdef USE_UCS4
character(kind=CDK,len=*),parameter :: form_spec = 'FORMATTED'
#else
character(kind=CDK,len=*),parameter :: form_spec = 'UNFORMATTED'
#endif
!*********************************************************
!*********************************************************
!>
! Type used to construct the linked-list JSON structure.
! Normally, this should always be a pointer variable.
! This type should only be used by an instance of [[json_core(type)]].
!
!### Example
!
! The following test program:
!
!````fortran
! program test
! use json_module
! implicit none
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_object(p,'') !create the root
! call json%add(p,'year',1805) !add some data
! call json%add(p,'value',1.0_RK) !add some data
! call json%print(p,'test.json') !write it to a file
! call json%destroy(p) !cleanup
! end program test
!````
!
! Produces the JSON file **test.json**:
!
!````json
! {
! "year": 1805,
! "value": 0.1E+1
! }
!````
!
!@warning Pointers of this type should only be allocated
! using the methods from [[json_core(type)]].
type,public :: json_value
!force the constituents to be stored contiguously
![note: on Intel, the order of the variables below
! is significant to avoid the misaligned field warnings]
sequence
private
!for the linked list:
type(json_value),pointer :: previous => null() !! previous item in the list
type(json_value),pointer :: next => null() !! next item in the list
type(json_value),pointer :: parent => null() !! parent item of this
type(json_value),pointer :: children => null() !! first child item of this
type(json_value),pointer :: tail => null() !! last child item of this
character(kind=CK,len=:),allocatable :: name !! variable name (unescaped)
real(RK),allocatable :: dbl_value !! real data for this variable
logical(LK),allocatable :: log_value !! logical data for this variable
character(kind=CK,len=:),allocatable :: str_value !! string data for this variable
!! (unescaped)
integer(IK),allocatable :: int_value !! integer data for this variable
integer(IK) :: var_type = json_unknown !! variable type
integer(IK),private :: n_children = 0 !! number of children
end type json_value
!*********************************************************
!*********************************************************
!>
! To access the core routines for manipulation
! of [[json_value]] pointer variables. This class allows
! for thread safe use of the module.
!
!### Usage
!````fortran
! program test
! use json_module, wp=>json_RK
! implicit none
! type(json_core) :: json !<--have to declare this
! type(json_value),pointer :: p
! call json%create_object(p,'') !create the root
! call json%add(p,'year',1805) !add some data
! call json%add(p,'value',1.0_wp) !add some data
! call json%print(p,'test.json') !write it to a file
! call json%destroy(p) !cleanup
! end program test
!````
type,public :: json_core
private
integer(IK) :: spaces_per_tab = 2 !! number of spaces for indenting
logical(LK) :: compact_real = .true. !! to use the "compact" form of real
!! numbers for output
character(kind=CDK,len=:),allocatable :: real_fmt !! the format string to use
!! for converting real numbers to strings.
!! It can be set in [[json_initialize]],
!! and used in [[json_value_print]]
!! If not set, then `default_real_fmt`
!! is used instead.
logical(LK) :: is_verbose = .false. !! if true, all exceptions are
!! immediately printed to console.
logical(LK) :: stop_on_error = .false. !! if true, then the program is
!! stopped immediately when an
!! exception is raised.
logical(LK) :: exception_thrown = .false. !! The error flag. Will be set to true
!! when an error is thrown in the class.
!! Many of the methods will check this
!! and return immediately if it is true.
character(kind=CK,len=:),allocatable :: err_message
!! the error message.
!! if `exception_thrown=False` then
!! this variable is not allocated.
integer(IK) :: char_count = 0 !! character position in the current line
integer(IK) :: line_count = 1 !! lines read counter
integer(IK) :: pushed_index = 0 !! used when parsing lines in file
character(kind=CK,len=pushed_char_size) :: pushed_char = CK_'' !! used when parsing
!! lines in file
integer(IK) :: ipos = 1 !! for allocatable strings: next character to read
logical(LK) :: strict_type_checking = .false. !! if true, then no type conversions are done
!! in the `get` routines if the actual variable
!! type is different from the return type (for
!! example, integer to real).
logical(LK) :: trailing_spaces_significant = .false. !! for name and path comparisons, if trailing
!! space is to be considered significant.
logical(LK) :: case_sensitive_keys = .true. !! if name and path comparisons
!! are case sensitive.
logical(LK) :: no_whitespace = .false. !! when printing a JSON string, don't include
!! non-significant spaces or line breaks.
!! If true, the entire structure will be
!! printed on one line.
logical(LK) :: unescaped_strings = .true. !! If false, then the escaped
!! string is returned from [[json_get_string]]
!! and similar routines. If true [default],
!! then the string is returned unescaped.
logical(LK) :: allow_comments = .true. !! if true, any comments will be ignored when
!! parsing a file. The comment tokens are defined
!! by the `comment_char` character variable.
character(kind=CK,len=:),allocatable :: comment_char !! comment tokens when
!! `allow_comments` is true.
!! Examples: '`!`' or '`#`'.
!! Default is `CK_'/!#'`.
integer(IK) :: path_mode = 1_IK !! How the path strings are interpreted in the
!! `get_by_path` routines:
!!
!! * 1 -- Default mode (see [[json_get_by_path_default]])
!! * 2 -- as RFC 6901 "JSON Pointer" paths
!! (see [[json_get_by_path_rfc6901]])
!! * 3 -- JSONPath "bracket-notation"
!! see [[json_get_by_path_jsonpath_bracket]])
character(kind=CK,len=1) :: path_separator = dot !! The `path` separator to use
!! in the "default" mode for
!! the paths in the various
!! `get_by_path` routines.
!! Note: if `path_mode/=1`
!! then this is ignored.
logical(LK) :: compress_vectors = .false. !! If true, then arrays of integers,
!! nulls, reals, & logicals are
!! printed all on one line.
!! [Note: `no_whitespace` will
!! override this option if necessary]
logical(LK) :: allow_duplicate_keys = .true. !! If False, then after parsing, if any
!! duplicate keys are found, an error is
!! thrown. A call to [[json_value_validate]]
!! will also check for duplicates. If True
!! [default] then no special checks are done
logical(LK) :: escape_solidus = .false. !! If True then the solidus "`/`" is always escaped
!! ("`\/`") when serializing JSON.
!! If False [default], then it is not escaped.
!! Note that this option does not affect parsing
!! (both escaped and unescaped versions are still
!! valid in all cases).
integer(IK) :: null_to_real_mode = 2_IK !! if `strict_type_checking=false`:
!!
!! * 1 : an exception will be raised if
!! try to retrieve a `null` as a real.
!! * 2 : a `null` retrieved as a real
!! will return NaN. [default]
!! * 3 : a `null` retrieved as a real
!! will return 0.0.
logical(LK) :: non_normals_to_null = .false. !! How to serialize NaN, Infinity,
!! and -Infinity real values:
!!
!! * If true : as JSON `null` values
!! * If false : as strings (e.g., "NaN",
!! "Infinity", "-Infinity") [default]
logical(LK) :: use_quiet_nan = .true. !! if true [default], `null_to_real_mode=2`
!! and [[string_to_real]] will use
!! `ieee_quiet_nan` for NaN values. If false,
!! `ieee_signaling_nan` will be used.
logical(LK) :: strict_integer_type_checking = .true.
!! * If false, when parsing JSON, if an integer numeric value
!! cannot be converted to an integer (`integer(IK)`),
!! then an attempt is then make to convert it
!! to a real (`real(RK)`).
!! * If true [default], an exception will be raised if an integer
!! value cannot be read when parsing JSON.
logical(LK) :: allow_trailing_comma = .true.
!! Allow a single trailing comma in arrays and objects.
integer :: ichunk = 0 !! index in `chunk` for [[pop_char]]
!! when `use_unformatted_stream=True`
integer :: filesize = 0 !! the file size when when `use_unformatted_stream=True`
character(kind=CK,len=:),allocatable :: chunk !! a chunk read from a stream file
!! when `use_unformatted_stream=True`
contains
private
!>
! Return a child of a [[json_value]] structure.
generic,public :: get_child => json_value_get_child_by_index, &
json_value_get_child,&
MAYBEWRAP(json_value_get_child_by_name)
procedure,private :: json_value_get_child_by_index
procedure,private :: MAYBEWRAP(json_value_get_child_by_name)
procedure,private :: json_value_get_child
!>
! Add objects to a linked list of [[json_value]]s.
!
!@note It might make more sense to call this `add_child`.
generic,public :: add => json_value_add_member, &
MAYBEWRAP(json_value_add_null), &
MAYBEWRAP(json_value_add_integer), &
MAYBEWRAP(json_value_add_integer_vec), &
#ifndef REAL32
MAYBEWRAP(json_value_add_real32), &
MAYBEWRAP(json_value_add_real32_vec), &
#endif
MAYBEWRAP(json_value_add_real), &
MAYBEWRAP(json_value_add_real_vec), &
#ifdef REAL128
MAYBEWRAP(json_value_add_real64), &
MAYBEWRAP(json_value_add_real64_vec), &
#endif
MAYBEWRAP(json_value_add_logical), &
MAYBEWRAP(json_value_add_logical_vec), &
MAYBEWRAP(json_value_add_string), &
MAYBEWRAP(json_value_add_string_vec)
#ifdef USE_UCS4
generic,public :: add => json_value_add_string_name_ascii, &
json_value_add_string_val_ascii, &
json_value_add_string_vec_name_ascii, &
json_value_add_string_vec_val_ascii
#endif
procedure,private :: json_value_add_member
procedure,private :: MAYBEWRAP(json_value_add_integer)
procedure,private :: MAYBEWRAP(json_value_add_null)
procedure,private :: MAYBEWRAP(json_value_add_integer_vec)
#ifndef REAL32
procedure,private :: MAYBEWRAP(json_value_add_real32)
procedure,private :: MAYBEWRAP(json_value_add_real32_vec)
#endif
procedure,private :: MAYBEWRAP(json_value_add_real)
procedure,private :: MAYBEWRAP(json_value_add_real_vec)
#ifdef REAL128
procedure,private :: MAYBEWRAP(json_value_add_real64)
procedure,private :: MAYBEWRAP(json_value_add_real64_vec)
#endif
procedure,private :: MAYBEWRAP(json_value_add_logical)
procedure,private :: MAYBEWRAP(json_value_add_logical_vec)
procedure,private :: MAYBEWRAP(json_value_add_string)
procedure,private :: MAYBEWRAP(json_value_add_string_vec)
#ifdef USE_UCS4
procedure,private :: json_value_add_string_name_ascii
procedure,private :: json_value_add_string_val_ascii
procedure,private :: json_value_add_string_vec_name_ascii
procedure,private :: json_value_add_string_vec_val_ascii
#endif
!>
! These are like the `add` methods, except if a variable with the
! same path is already present, then its value is simply updated.
! Note that currently, these only work for scalar variables.
! These routines can also change the variable's type (but an error will be
! thrown if the existing variable is not a scalar).
!
!### See also
! * [[json_core(type):add_by_path]] - this one can be used to change
! arrays and objects to scalars if so desired.
!
!@note Unlike some routines, the `found` output is not optional,
! so it doesn't present exceptions from being thrown.
!
!@note These have been mostly supplanted by the [[json_core(type):add_by_path]]
! methods, which do a similar thing (and can be used for
! scalars and vectors, etc.)
generic,public :: update => MAYBEWRAP(json_update_logical),&
#ifndef REAL32
MAYBEWRAP(json_update_real32),&
#endif
MAYBEWRAP(json_update_real),&
#ifdef REAL128
MAYBEWRAP(json_update_real64),&
#endif
MAYBEWRAP(json_update_integer),&
MAYBEWRAP(json_update_string)
#ifdef USE_UCS4
generic,public :: update => json_update_string_name_ascii,&
json_update_string_val_ascii
#endif
procedure,private :: MAYBEWRAP(json_update_logical)
#ifndef REAL32
procedure,private :: MAYBEWRAP(json_update_real32)
#endif
procedure,private :: MAYBEWRAP(json_update_real)
#ifdef REAL128
procedure,private :: MAYBEWRAP(json_update_real64)
#endif
procedure,private :: MAYBEWRAP(json_update_integer)
procedure,private :: MAYBEWRAP(json_update_string)
#ifdef USE_UCS4
procedure,private :: json_update_string_name_ascii
procedure,private :: json_update_string_val_ascii
#endif
!>
! Add variables to a [[json_value]] linked list
! by specifying their paths.
!
!### Example
!
!````fortran
! use, intrinsic :: iso_fortran_env, only: output_unit
! use json_module, wp=>json_RK
! type(json_core) :: json
! type(json_value) :: p
! call json%create_object(p,'root') ! create the root
! ! now add some variables using the paths:
! call json%add_by_path(p,'inputs.t', 0.0_wp )
! call json%add_by_path(p,'inputs.x(1)', 100.0_wp)
! call json%add_by_path(p,'inputs.x(2)', 200.0_wp)
! call json%print(p) ! now print to console
!````
!
!### Notes
! * This uses [[json_create_by_path]]
!
!### See also
! * The `json_core%update` methods.
! * [[json_create_by_path]]
generic,public :: add_by_path => MAYBEWRAP(json_add_member_by_path),&
MAYBEWRAP(json_add_integer_by_path),&
#ifndef REAL32
MAYBEWRAP(json_add_real32_by_path),&
#endif
MAYBEWRAP(json_add_real_by_path),&
#ifdef REAL128
MAYBEWRAP(json_add_real64_by_path),&
#endif
MAYBEWRAP(json_add_logical_by_path),&
MAYBEWRAP(json_add_string_by_path),&
MAYBEWRAP(json_add_integer_vec_by_path),&
#ifndef REAL32
MAYBEWRAP(json_add_real32_vec_by_path),&
#endif
MAYBEWRAP(json_add_real_vec_by_path),&
#ifdef REAL128
MAYBEWRAP(json_add_real64_vec_by_path),&
#endif
MAYBEWRAP(json_add_logical_vec_by_path),&
MAYBEWRAP(json_add_string_vec_by_path)
#ifdef USE_UCS4
generic,public :: add_by_path => json_add_string_by_path_value_ascii,&
json_add_string_by_path_path_ascii,&
json_add_string_vec_by_path_value_ascii,&
json_add_string_vec_by_path_path_ascii
#endif
procedure :: MAYBEWRAP(json_add_member_by_path)
procedure :: MAYBEWRAP(json_add_integer_by_path)
#ifndef REAL32
procedure :: MAYBEWRAP(json_add_real32_by_path)
#endif
procedure :: MAYBEWRAP(json_add_real_by_path)
#ifdef REAL128
procedure :: MAYBEWRAP(json_add_real64_by_path)
#endif
procedure :: MAYBEWRAP(json_add_logical_by_path)
procedure :: MAYBEWRAP(json_add_string_by_path)
procedure :: MAYBEWRAP(json_add_integer_vec_by_path)
#ifndef REAL32
procedure :: MAYBEWRAP(json_add_real32_vec_by_path)
#endif
procedure :: MAYBEWRAP(json_add_real_vec_by_path)
#ifdef REAL128
procedure :: MAYBEWRAP(json_add_real64_vec_by_path)
#endif
procedure :: MAYBEWRAP(json_add_logical_vec_by_path)
procedure :: MAYBEWRAP(json_add_string_vec_by_path)
#ifdef USE_UCS4
procedure :: json_add_string_by_path_value_ascii
procedure :: json_add_string_by_path_path_ascii
procedure :: json_add_string_vec_by_path_value_ascii
procedure :: json_add_string_vec_by_path_path_ascii
#endif
!>
! Create a [[json_value]] linked list using the
! path to the variables. Optionally return a
! pointer to the variable.
!
! (This will create a `null` variable)
!
!### See also
! * [[json_core(type):add_by_path]]
generic,public :: create => MAYBEWRAP(json_create_by_path)
procedure :: MAYBEWRAP(json_create_by_path)
!>
! Get data from a [[json_value]] linked list.
!
!@note There are two versions (e.g. [[json_get_integer]] and [[json_get_integer_by_path]]).
! The first one gets the value from the [[json_value]] passed into the routine,
! while the second one gets the value from the [[json_value]] found by parsing the
! path. The path version is split up into unicode and non-unicode versions.
generic,public :: get => &
MAYBEWRAP(json_get_by_path), &
json_get_integer, MAYBEWRAP(json_get_integer_by_path), &
json_get_integer_vec, MAYBEWRAP(json_get_integer_vec_by_path), &
#ifndef REAL32
json_get_real32, MAYBEWRAP(json_get_real32_by_path), &
json_get_real32_vec, MAYBEWRAP(json_get_real32_vec_by_path), &
#endif
json_get_real, MAYBEWRAP(json_get_real_by_path), &
json_get_real_vec, MAYBEWRAP(json_get_real_vec_by_path), &
#ifdef REAL128
json_get_real64, MAYBEWRAP(json_get_real64_by_path), &
json_get_real64_vec, MAYBEWRAP(json_get_real64_vec_by_path), &
#endif
json_get_logical, MAYBEWRAP(json_get_logical_by_path), &
json_get_logical_vec, MAYBEWRAP(json_get_logical_vec_by_path), &
json_get_string, MAYBEWRAP(json_get_string_by_path), &
json_get_string_vec, MAYBEWRAP(json_get_string_vec_by_path), &
json_get_alloc_string_vec, MAYBEWRAP(json_get_alloc_string_vec_by_path),&
json_get_array, MAYBEWRAP(json_get_array_by_path)
procedure,private :: json_get_integer
procedure,private :: json_get_integer_vec
#ifndef REAL32
procedure,private :: json_get_real32
procedure,private :: json_get_real32_vec
#endif
procedure,private :: json_get_real
procedure,private :: json_get_real_vec
#ifdef REAL128
procedure,private :: json_get_real64
procedure,private :: json_get_real64_vec
#endif
procedure,private :: json_get_logical
procedure,private :: json_get_logical_vec
procedure,private :: json_get_string
procedure,private :: json_get_string_vec
procedure,private :: json_get_alloc_string_vec
procedure,private :: json_get_array
procedure,private :: MAYBEWRAP(json_get_by_path)
procedure,private :: MAYBEWRAP(json_get_integer_by_path)
procedure,private :: MAYBEWRAP(json_get_integer_vec_by_path)
#ifndef REAL32
procedure,private :: MAYBEWRAP(json_get_real32_by_path)
procedure,private :: MAYBEWRAP(json_get_real32_vec_by_path)
#endif
procedure,private :: MAYBEWRAP(json_get_real_by_path)
procedure,private :: MAYBEWRAP(json_get_real_vec_by_path)
#ifdef REAL128
procedure,private :: MAYBEWRAP(json_get_real64_by_path)
procedure,private :: MAYBEWRAP(json_get_real64_vec_by_path)
#endif
procedure,private :: MAYBEWRAP(json_get_logical_by_path)
procedure,private :: MAYBEWRAP(json_get_logical_vec_by_path)
procedure,private :: MAYBEWRAP(json_get_string_by_path)
procedure,private :: MAYBEWRAP(json_get_string_vec_by_path)
procedure,private :: MAYBEWRAP(json_get_array_by_path)
procedure,private :: MAYBEWRAP(json_get_alloc_string_vec_by_path)
procedure,private :: json_get_by_path_default
procedure,private :: json_get_by_path_rfc6901
procedure,private :: json_get_by_path_jsonpath_bracket
!>
! Print the [[json_value]] to an output unit or file.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value) :: p
! !...
! call json%print(p,'test.json') !this is [[json_print_to_filename]]
!````
generic,public :: print => json_print_to_console,&
json_print_to_unit,&
json_print_to_filename
procedure :: json_print_to_console
procedure :: json_print_to_unit
procedure :: json_print_to_filename
!>
! Destructor routine for a [[json_value]] pointer.
! This must be called explicitly if it is no longer needed,
! before it goes out of scope. Otherwise, a memory leak will result.
!
!### Example
!
! Destroy the [[json_value]] pointer before the variable goes out of scope:
!````fortran
! subroutine example1()
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_object(p,'')
! call json%add(p,'year',2015)
! call json%print(p)
! call json%destroy(p)
! end subroutine example1
!````
!
! Note: it should NOT be called for a [[json_value]] pointer than has already been
! added to another [[json_value]] structure, since doing so may render the
! other structure invalid. Consider the following example:
!````fortran
! subroutine example2(p)
! type(json_core) :: json
! type(json_value),pointer,intent(out) :: p
! type(json_value),pointer :: q
! call json%create_object(p,'')
! call json%add(p,'year',2015)
! call json%create_object(q,'q')
! call json%add(q,'val',1)
! call json%add(p, q) !add q to p structure
! ! do NOT call json%destroy(q) here, because q is
! ! now part of the output structure p. p should be destroyed
! ! somewhere upstream by the caller of this routine.
! nullify(q) !OK, but not strictly necessary
! end subroutine example2
!````
generic,public :: destroy => json_value_destroy,destroy_json_core
procedure :: json_value_destroy
procedure :: destroy_json_core
!>
! If the child variable is present, then remove it.
generic,public :: remove_if_present => MAYBEWRAP(json_value_remove_if_present)
procedure :: MAYBEWRAP(json_value_remove_if_present)
!>
! Allocate a [[json_value]] pointer and make it a real variable.
! The pointer should not already be allocated.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_real(p,'value',1.0_RK)
!````
!
!### Note
! * [[json_core(type):create_real]] is just an alias
! to this one for backward compatibility.
generic,public :: create_real => MAYBEWRAP(json_value_create_real)
procedure :: MAYBEWRAP(json_value_create_real)
#ifndef REAL32
generic,public :: create_real => MAYBEWRAP(json_value_create_real32)
procedure :: MAYBEWRAP(json_value_create_real32)
#endif
#ifdef REAL128
generic,public :: create_real => MAYBEWRAP(json_value_create_real64)
procedure :: MAYBEWRAP(json_value_create_real64)
#endif
!>
! This is equivalent to [[json_core(type):create_real]],
! and is here only for backward compatibility.
generic,public :: create_double => MAYBEWRAP(json_value_create_real)
#ifndef REAL32
generic,public :: create_double => MAYBEWRAP(json_value_create_real32)
#endif
#ifdef REAL128
generic,public :: create_double => MAYBEWRAP(json_value_create_real64)
#endif
!>
! Allocate a [[json_value]] pointer and make it an array variable.
! The pointer should not already be allocated.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_array(p,'arrayname')
!````
generic,public :: create_array => MAYBEWRAP(json_value_create_array)
procedure :: MAYBEWRAP(json_value_create_array)
!>
! Allocate a [[json_value]] pointer and make it an object variable.
! The pointer should not already be allocated.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_object(p,'objectname')
!````
!
!@note The name is not significant for the root structure or an array element.
! In those cases, an empty string can be used.
generic,public :: create_object => MAYBEWRAP(json_value_create_object)
procedure :: MAYBEWRAP(json_value_create_object)
!>
! Allocate a json_value pointer and make it a null variable.
! The pointer should not already be allocated.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_null(p,'value')
!````
generic,public :: create_null => MAYBEWRAP(json_value_create_null)
procedure :: MAYBEWRAP(json_value_create_null)
!>
! Allocate a json_value pointer and make it a string variable.
! The pointer should not already be allocated.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_string(p,'value','foobar')
!````
generic,public :: create_string => MAYBEWRAP(json_value_create_string)
procedure :: MAYBEWRAP(json_value_create_string)
!>
! Allocate a json_value pointer and make it an integer variable.
! The pointer should not already be allocated.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_integer(p,42,'value')
!````
generic,public :: create_integer => MAYBEWRAP(json_value_create_integer)
procedure :: MAYBEWRAP(json_value_create_integer)
!>
! Allocate a json_value pointer and make it a logical variable.
! The pointer should not already be allocated.
!
!### Example
!
!````fortran
! type(json_core) :: json
! type(json_value),pointer :: p
! call json%create_logical(p,'value',.true.)
!````
generic,public :: create_logical => MAYBEWRAP(json_value_create_logical)
procedure :: MAYBEWRAP(json_value_create_logical)
!>
! Parse the JSON file and populate the [[json_value]] tree.
generic,public :: load => json_parse_file
procedure :: json_parse_file
!>
! Print the [[json_value]] structure to an allocatable string
procedure,public :: serialize => json_value_to_string
!>
! The same as `serialize`, but only here for backward compatibility
procedure,public :: print_to_string => json_value_to_string
!>
! Parse the JSON string and populate the [[json_value]] tree.
generic,public :: deserialize => MAYBEWRAP(json_parse_string)
procedure :: MAYBEWRAP(json_parse_string)
!>
! Same as `load` and `deserialize` but only here for backward compatibility.
generic,public :: parse => json_parse_file, &
MAYBEWRAP(json_parse_string)
!>
! Throw an exception.
generic,public :: throw_exception => MAYBEWRAP(json_throw_exception)
procedure :: MAYBEWRAP(json_throw_exception)
!>
! Rename a [[json_value]] variable.
generic,public :: rename => MAYBEWRAP(json_value_rename),&
MAYBEWRAP(json_rename_by_path)
procedure :: MAYBEWRAP(json_value_rename)
procedure :: MAYBEWRAP(json_rename_by_path)
#ifdef USE_UCS4
generic,public :: rename => json_rename_by_path_name_ascii,&
json_rename_by_path_path_ascii
procedure :: json_rename_by_path_name_ascii
procedure :: json_rename_by_path_path_ascii
#endif
!>
! get info about a [[json_value]]
generic,public :: info => json_info, MAYBEWRAP(json_info_by_path)
procedure :: json_info
procedure :: MAYBEWRAP(json_info_by_path)
!>
! get string info about a [[json_value]]
generic,public :: string_info => json_string_info
procedure :: json_string_info
!>
! get matrix info about a [[json_value]]
generic,public :: matrix_info => json_matrix_info, MAYBEWRAP(json_matrix_info_by_path)
procedure :: json_matrix_info
procedure :: MAYBEWRAP(json_matrix_info_by_path)
!>
! insert a new element after an existing one,
! updating the JSON structure accordingly
generic,public :: insert_after => json_value_insert_after, &
json_value_insert_after_child_by_index
procedure :: json_value_insert_after
procedure :: json_value_insert_after_child_by_index
!>
! get the path to a JSON variable in a structure:
generic,public :: get_path => MAYBEWRAP(json_get_path)
procedure :: MAYBEWRAP(json_get_path)
!>
! verify if a path is valid
! (i.e., a variable with this path exists in the file).
generic,public :: valid_path => MAYBEWRAP(json_valid_path)
procedure :: MAYBEWRAP(json_valid_path)
procedure,public :: remove => json_value_remove !! Remove a [[json_value]] from a
!! linked-list structure.
procedure,public :: replace => json_value_replace !! Replace a [[json_value]] in a
!! linked-list structure.
procedure,public :: reverse => json_value_reverse !! Reverse the order of the children
!! of an array of object.
procedure,public :: check_for_errors => json_check_for_errors !! check for error and get error message
procedure,public :: clear_exceptions => json_clear_exceptions !! clear exceptions
procedure,public :: count => json_count !! count the number of children
procedure,public :: clone => json_clone !! clone a JSON structure (deep copy)
procedure,public :: failed => json_failed !! check for error
procedure,public :: get_parent => json_get_parent !! get pointer to json_value parent
procedure,public :: get_next => json_get_next !! get pointer to json_value next
procedure,public :: get_previous => json_get_previous !! get pointer to json_value previous
procedure,public :: get_tail => json_get_tail !! get pointer to json_value tail
procedure,public :: initialize => json_initialize !! to initialize some parsing parameters
procedure,public :: traverse => json_traverse !! to traverse all elements of a JSON
!! structure
procedure,public :: print_error_message => json_print_error_message !! simply routine to print error
!! messages
procedure,public :: swap => json_value_swap !! Swap two [[json_value]] pointers
!! in a structure (or two different
!! structures).
procedure,public :: is_child_of => json_value_is_child_of !! Check if a [[json_value]] is a
!! descendant of another.
procedure,public :: validate => json_value_validate !! Check that a [[json_value]] linked
!! list is valid (i.e., is properly
!! constructed). This may be useful
!! if it has been constructed externally.
procedure,public :: check_for_duplicate_keys &
=> json_check_all_for_duplicate_keys !! Check entire JSON structure
!! for duplicate keys (recursively)
procedure,public :: check_children_for_duplicate_keys &
=> json_check_children_for_duplicate_keys !! Check a `json_value` object's
!! children for duplicate keys
!other private routines:
procedure :: name_equal
procedure :: name_strings_equal
procedure :: json_value_print
procedure :: string_to_int
procedure :: string_to_dble
procedure :: prepare_parser => json_prepare_parser
procedure :: parse_end => json_parse_end
procedure :: parse_value
procedure :: parse_number
procedure :: parse_string
procedure :: parse_for_chars
procedure :: parse_object
procedure :: parse_array
procedure :: annotate_invalid_json
procedure :: pop_char
procedure :: push_char
procedure :: get_current_line_from_file_stream
procedure,nopass :: get_current_line_from_file_sequential
procedure :: convert
procedure :: to_string
procedure :: to_logical
procedure :: to_integer
procedure :: to_real
procedure :: to_null
procedure :: to_object
procedure :: to_array
procedure,nopass :: json_value_clone_func
procedure :: is_vector => json_is_vector
end type json_core
!*********************************************************
!*********************************************************
!>
! Structure constructor to initialize a
! [[json_core(type)]] object
!
!### Example
!
!```fortran
! type(json_file) :: json_core
! json_core = json_core()
!```
interface json_core
module procedure initialize_json_core
end interface
!*********************************************************
!*************************************************************************************
abstract interface
subroutine json_array_callback_func(json, element, i, count)
!! Array element callback function. Used by [[json_get_array]]
import :: json_value,json_core,IK
implicit none
class(json_core),intent(inout) :: json
type(json_value),pointer,intent(in) :: element
integer(IK),intent(in) :: i !! index
integer(IK),intent(in) :: count !! size of array
end subroutine json_array_callback_func
subroutine json_traverse_callback_func(json,p,finished)
!! Callback function used by [[json_traverse]]
import :: json_value,json_core,LK
implicit none
class(json_core),intent(inout) :: json
type(json_value),pointer,intent(in) :: p
logical(LK),intent(out) :: finished !! set true to stop traversing
end subroutine json_traverse_callback_func
end interface
public :: json_array_callback_func
public :: json_traverse_callback_func
!*************************************************************************************
contains
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
! date: 4/17/2016
!
! Destructor for the [[json_core(type)]] type.
subroutine destroy_json_core(me)
implicit none
class(json_core),intent(out) :: me
end subroutine destroy_json_core
!*****************************************************************************************
!*****************************************************************************************
!> author: Jacob Williams
! date: 4/26/2016
!
! Function constructor for a [[json_core(type)]].
! This is just a wrapper for [[json_initialize]].
!
!@note [[initialize_json_core]], [[json_initialize]],
! [[initialize_json_core_in_file]], and [[initialize_json_file]]
! all have a similar interface.
function initialize_json_core(&
#include "json_initialize_dummy_arguments.inc"
) result(json_core_object)
implicit none
type(json_core) :: json_core_object
#include "json_initialize_arguments.inc"
call json_core_object%initialize(&
#include "json_initialize_dummy_arguments.inc"
)
end function initialize_json_core
!*****************************************************************************************
!*****************************************************************************************