@@ -3078,33 +3078,47 @@ subroutine json_value_add_member(json,p,member)
30783078 implicit none
30793079
30803080 class(json_core),intent (inout ) :: json
3081- type (json_value),pointer :: p ! ! `p` should be a `json_object`
3082- ! ! or a `json_array` (although this
3083- ! ! is not currently checked)
3081+ type (json_value),pointer :: p ! ! `p` must be a `json_object`
3082+ ! ! or a `json_array`
30843083 type (json_value),pointer :: member ! ! the child member
30853084 ! ! to add to `p`
30863085
3086+ integer (IK) :: var_type ! ! variable type of `p`
3087+
30873088 if (.not. json% exception_thrown) then
30883089
3089- ! associate the parent
3090- member% parent = > p
3090+ if (associated (p)) then
30913091
3092- ! add to linked list
3093- if (associated (p% children)) then
3092+ call json% info(p,var_type= var_type)
30943093
3095- p % tail % next = > member
3096- member % previous = > p % tail
3094+ select case (var_type)
3095+ case (json_object, json_array)
30973096
3098- else
3097+ ! associate the parent
3098+ member% parent = > p
3099+
3100+ ! add to linked list
3101+ if (associated (p% children)) then
3102+ p% tail% next = > member
3103+ member% previous = > p% tail
3104+ else
3105+ p% children = > member
3106+ member% previous = > null () ! first in the list
3107+ end if
30993108
3100- p% children = > member
3101- member% previous = > null () ! first in the list
3109+ ! new member is now the last one in the list
3110+ p% tail = > member
3111+ p% n_children = p% n_children + 1
31023112
3103- end if
3113+ case default
3114+ call json% throw_exception(' Error in json_value_add_member: ' // &
3115+ ' can only add child to object or array' )
3116+ end select
31043117
3105- ! new member is now the last one in the list
3106- p% tail = > member
3107- p% n_children = p% n_children + 1
3118+ else
3119+ call json% throw_exception(' Error in json_value_add_member: ' // &
3120+ ' the pointer is not associated' )
3121+ end if
31083122
31093123 end if
31103124
0 commit comments