Skip to content

Commit 77eb985

Browse files
committed
Initial thread-safe version. Work in progress.
Eliminated all global variables. The json_core class is now used to access the lower-level routines for json_value manipulation. Each json_file variable has its own instance of a json_core class. Added new initialize(), failed(), print_error_message(), check_to_errors(), and clear_exceptions() to the json_file class. The number of spaces for indenting can now be user specified. Updated the unit tests.
1 parent 7c8f7b8 commit 77eb985

18 files changed

+1958
-1622
lines changed

src/json_file_module.F90

Lines changed: 194 additions & 64 deletions
Large diffs are not rendered by default.

src/json_parameters.F90

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ module json_parameters
5050
character(kind=CK,len=*),dimension(32),parameter :: control_chars = &
5151
[(achar(i_),i_=1,31), achar(127)] !! Control characters, possibly in unicode
5252

53-
!for indenting (Note: this could also be a user input...)
54-
integer(IK),parameter :: spaces_per_tab = 2
55-
5653
!find out the precision of the floating point number system
5754
!and set safety factors
5855
integer(IK),parameter :: rp_safety_factor = 1
@@ -78,5 +75,7 @@ module json_parameters
7875

7976
integer(IK),parameter,public :: seq_chunk_size = 256 !! chunk size for reading sequential files
8077

78+
integer(IK),parameter,public :: pushed_char_size = 10 !! magic number
79+
8180
end module json_parameters
8281
!*****************************************************************************************

src/json_string_utilities.F90

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module json_string_utilities
88

99
use json_kinds
10-
use json_parameters, only: star
10+
use json_parameters
1111

1212
implicit none
1313

@@ -48,6 +48,7 @@ module json_string_utilities
4848
public :: real_to_string
4949
public :: valid_json_hex
5050
public :: to_unicode
51+
public :: escape_string
5152

5253
contains
5354
!*****************************************************************************************
@@ -194,6 +195,91 @@ subroutine compact_real_string(str)
194195
end subroutine compact_real_string
195196
!*****************************************************************************************
196197

198+
!*****************************************************************************************
199+
!> author: Jacob Williams
200+
! date: 1/21/2014
201+
!
202+
! Add the escape characters to a string for adding to JSON.
203+
204+
subroutine escape_string(str_in, str_out)
205+
206+
implicit none
207+
208+
character(kind=CK,len=*),intent(in) :: str_in
209+
character(kind=CK,len=:),allocatable,intent(out) :: str_out
210+
211+
integer(IK) :: i,ipos
212+
character(kind=CK,len=1) :: c
213+
214+
character(kind=CK,len=*),parameter :: specials = quotation_mark//&
215+
backslash//&
216+
slash//&
217+
bspace//&
218+
formfeed//&
219+
newline//&
220+
carriage_return//&
221+
horizontal_tab
222+
223+
!Do a quick scan for the special characters,
224+
! if any are present, then process the string,
225+
! otherwise, return the string as is.
226+
if (scan(str_in,specials)>0) then
227+
228+
str_out = repeat(space,chunk_size)
229+
ipos = 1
230+
231+
!go through the string and look for special characters:
232+
do i=1,len(str_in)
233+
234+
c = str_in(i:i) !get next character in the input string
235+
236+
!if the string is not big enough, then add another chunk:
237+
if (ipos+3>len(str_out)) str_out = str_out // repeat(space, chunk_size)
238+
239+
select case(c)
240+
case(quotation_mark,backslash,slash)
241+
str_out(ipos:ipos+1) = backslash//c
242+
ipos = ipos + 2
243+
case(bspace)
244+
str_out(ipos:ipos+1) = '\b'
245+
ipos = ipos + 2
246+
case(formfeed)
247+
str_out(ipos:ipos+1) = '\f'
248+
ipos = ipos + 2
249+
case(newline)
250+
str_out(ipos:ipos+1) = '\n'
251+
ipos = ipos + 2
252+
case(carriage_return)
253+
str_out(ipos:ipos+1) = '\r'
254+
ipos = ipos + 2
255+
case(horizontal_tab)
256+
str_out(ipos:ipos+1) = '\t'
257+
ipos = ipos + 2
258+
case default
259+
str_out(ipos:ipos) = c
260+
ipos = ipos + 1
261+
end select
262+
263+
end do
264+
265+
!trim the string if necessary:
266+
if (ipos<len(str_out)+1) then
267+
if (ipos==1) then
268+
str_out = ''
269+
else
270+
str_out = str_out(1:ipos-1)
271+
end if
272+
end if
273+
274+
else
275+
276+
str_out = str_in
277+
278+
end if
279+
280+
end subroutine escape_string
281+
!*****************************************************************************************
282+
197283
!*****************************************************************************************
198284
!> author: Jacob Williams
199285
! date:6/14/2014

0 commit comments

Comments
 (0)