@@ -267,15 +267,16 @@ module json_file_module
267267 ! > author: Izaak Beekman
268268 ! date: 07/23/2015
269269 !
270- ! Structure constructor to initialize a [[json_file(type)]] object
271- ! with an existing [[json_value]] object, and either the [[json_core(type)]]
272- ! settings or a [[json_core(type)]] instance.
270+ ! Structure constructor to initialize a [[json_file(type)]]
271+ ! object with an existing [[json_value]] object or a JSON
272+ ! string, and either the [[json_core(type)]] settings or a
273+ ! [[json_core(type)]] instance.
273274 !
274275 ! ### Example
275276 !
276277 ! ```fortran
277278 ! ...
278- ! type(json_file) :: my_file
279+ ! type(json_file) :: my_file
279280 ! type(json_value),pointer :: json_object
280281 ! type(json_core) :: json_core_object
281282 ! ...
@@ -285,10 +286,17 @@ module json_file_module
285286 ! !or:
286287 ! my_file = json_file(json_object,verbose=.true.)
287288 ! !or:
289+ ! my_file = json_file('{"x": [1]}',verbose=.true.)
290+ ! !or:
288291 ! my_file = json_file(json_object,json_core_object)
292+ ! !or:
293+ ! my_file = json_file('{"x": [1]}',json_core_object)
289294 ! ```
290295 interface json_file
291- module procedure initialize_json_file, initialize_json_file_v2
296+ module procedure initialize_json_file, &
297+ initialize_json_file_v2, &
298+ MAYBEWRAP(initialize_json_file_from_string), &
299+ MAYBEWRAP(initialize_json_file_from_string_v2)
292300 end interface
293301 ! *************************************************************************************
294302
@@ -374,7 +382,9 @@ end subroutine json_file_print_error_message
374382! @note This does not destroy the data in the file.
375383!
376384! @note [[initialize_json_core]], [[json_initialize]],
377- ! [[initialize_json_core_in_file]], and [[initialize_json_file]]
385+ ! [[initialize_json_core_in_file]], [[initialize_json_file]],
386+ ! [[initialize_json_file_v2]], [[initialize_json_file_from_string]],
387+ ! and [[initialize_json_file_from_string_v2]]
378388! all have a similar interface.
379389
380390 subroutine initialize_json_core_in_file (me ,verbose ,compact_reals ,&
@@ -461,7 +471,9 @@ end subroutine get_json_core_in_file
461471! It also calls the `initialize()` method.
462472!
463473! @note [[initialize_json_core]], [[json_initialize]],
464- ! [[initialize_json_core_in_file]], and [[initialize_json_file]]
474+ ! [[initialize_json_core_in_file]], [[initialize_json_file]],
475+ ! [[initialize_json_file_v2]], [[initialize_json_file_from_string]],
476+ ! and [[initialize_json_file_from_string_v2]]
465477! all have a similar interface.
466478
467479 function initialize_json_file (p ,verbose ,compact_reals ,&
@@ -528,6 +540,151 @@ function initialize_json_file_v2(json_value_object, json_core_object) &
528540 end function initialize_json_file_v2
529541! *****************************************************************************************
530542
543+ ! *****************************************************************************************
544+ ! > author: Jacob Williams
545+ ! date: 01/19/2019
546+ !
547+ ! Cast a JSON string as a [[json_file(type)]] object.
548+ ! It also calls the `initialize()` method.
549+ !
550+ ! ### Example
551+ !
552+ ! ```fortran
553+ ! type(json_file) :: f
554+ ! f = json_file('{"key ": 1}', trailing_spaces_significant=.true.)
555+ ! ```
556+ !
557+ ! @note [[initialize_json_core]], [[json_initialize]],
558+ ! [[initialize_json_core_in_file]], [[initialize_json_file]],
559+ ! [[initialize_json_file_v2]], [[initialize_json_file_from_string]],
560+ ! and [[initialize_json_file_from_string_v2]]
561+ ! all have a similar interface.
562+
563+ function initialize_json_file_from_string (str ,verbose ,compact_reals ,&
564+ print_signs ,real_format ,spaces_per_tab ,&
565+ strict_type_checking ,&
566+ trailing_spaces_significant ,&
567+ case_sensitive_keys ,&
568+ no_whitespace ,&
569+ unescape_strings ,&
570+ comment_char ,&
571+ path_mode ,&
572+ path_separator ,&
573+ compress_vectors ,&
574+ allow_duplicate_keys ,&
575+ escape_solidus ,&
576+ stop_on_error ) result(file_object)
577+
578+ implicit none
579+
580+ type (json_file) :: file_object
581+ character (kind= CK,len=* ),intent (in ) :: str ! ! string to load JSON data from
582+ #include " json_initialize_arguments.inc"
583+
584+ call file_object% initialize(verbose,compact_reals,&
585+ print_signs,real_format,spaces_per_tab,&
586+ strict_type_checking,&
587+ trailing_spaces_significant,&
588+ case_sensitive_keys,&
589+ no_whitespace,&
590+ unescape_strings,&
591+ comment_char,&
592+ path_mode,&
593+ path_separator,&
594+ compress_vectors,&
595+ allow_duplicate_keys,&
596+ escape_solidus,&
597+ stop_on_error)
598+
599+ call file_object% load_from_string(str)
600+
601+ end function initialize_json_file_from_string
602+ ! *****************************************************************************************
603+
604+ ! *****************************************************************************************
605+ ! >
606+ ! Alternate version of [[initialize_json_file_from_string]], where "str" is kind=CDK.
607+
608+ function wrap_initialize_json_file_from_string (str ,verbose ,compact_reals ,&
609+ print_signs ,real_format ,spaces_per_tab ,&
610+ strict_type_checking ,&
611+ trailing_spaces_significant ,&
612+ case_sensitive_keys ,&
613+ no_whitespace ,&
614+ unescape_strings ,&
615+ comment_char ,&
616+ path_mode ,&
617+ path_separator ,&
618+ compress_vectors ,&
619+ allow_duplicate_keys ,&
620+ escape_solidus ,&
621+ stop_on_error ) result(file_object)
622+
623+ implicit none
624+
625+ type (json_file) :: file_object
626+ character (kind= CDK,len=* ),intent (in ) :: str ! ! string to load JSON data from
627+ #include " json_initialize_arguments.inc"
628+
629+ file_object = initialize_json_file_from_string(&
630+ to_unicode(str),verbose,compact_reals,&
631+ print_signs,real_format,spaces_per_tab,&
632+ strict_type_checking,&
633+ trailing_spaces_significant,&
634+ case_sensitive_keys,&
635+ no_whitespace,&
636+ unescape_strings,&
637+ comment_char,&
638+ path_mode,&
639+ path_separator,&
640+ compress_vectors,&
641+ allow_duplicate_keys,&
642+ escape_solidus,&
643+ stop_on_error)
644+
645+ end function wrap_initialize_json_file_from_string
646+ ! *****************************************************************************************
647+
648+ ! *****************************************************************************************
649+ ! > author: Jacob Williams
650+ ! date: 1/19/2019
651+ !
652+ ! Cast a JSON string and a [[json_core(type)]] object
653+ ! as a [[json_file(type)]] object.
654+
655+ function initialize_json_file_from_string_v2 (str , json_core_object ) &
656+ result(file_object)
657+
658+ implicit none
659+
660+ type (json_file) :: file_object
661+ character (kind= CK,len=* ),intent (in ) :: str ! ! string to load JSON data from
662+ type (json_core),intent (in ) :: json_core_object
663+
664+ file_object% core = json_core_object
665+ call file_object% load_from_string(str)
666+
667+ end function initialize_json_file_from_string_v2
668+ ! *****************************************************************************************
669+
670+ ! *****************************************************************************************
671+ ! >
672+ ! Alternate version of [[initialize_json_file_from_string_v2]], where "str" is kind=CDK.
673+
674+ function wrap_initialize_json_file_from_string_v2 (str ,json_core_object ) &
675+ result(file_object)
676+
677+ implicit none
678+
679+ type (json_file) :: file_object
680+ character (kind= CDK,len=* ),intent (in ) :: str ! ! string to load JSON data from
681+ type (json_core),intent (in ) :: json_core_object
682+
683+ file_object = initialize_json_file_from_string_v2(to_unicode(str),json_core_object)
684+
685+ end function wrap_initialize_json_file_from_string_v2
686+ ! *****************************************************************************************
687+
531688! *****************************************************************************************
532689! > author: Jacob Williams
533690!
0 commit comments