module CStringConfiguration
	use CStringConfigurationVariables
	use SimulationVariables
	use json_module
	
	implicit none    
	public    
    contains
    
    subroutine StringConfigurationToJson(parent)

        type(json_value),pointer :: parent
        type(json_core) :: json
        type(json_value),pointer :: pitems,pitem,pbit,p
		integer:: i
        ! 1. create new node
		call json%create_object(p,'StringConfiguration')
        call json%create_array(pitems,'StringConfigurationItems')

        ! 2. add member of data type to new node
		do i=1,data%Configuration%StringConfiguration%StringConfigurationCount
			call json%create_object(pitem,'StringConfigurationItem')
			call json%add(pitem,"ComponentLength",data%Configuration%StringConfiguration%StringConfigurations(i)%ComponentLength)
			call json%add(pitem,"ComponentType",data%Configuration%StringConfiguration%StringConfigurations(i)%ComponentType)
			call json%add(pitem,"Grade",data%Configuration%StringConfiguration%StringConfigurations(i)%Grade)
			call json%add(pitem,"LengthPerJoint",data%Configuration%StringConfiguration%StringConfigurations(i)%LengthPerJoint)
			call json%add(pitem,"NominalId",data%Configuration%StringConfiguration%StringConfigurations(i)%NominalId)
			call json%add(pitem,"NominalOd",data%Configuration%StringConfiguration%StringConfigurations(i)%NominalOd)
			call json%add(pitem,"NominalToolJointOd",data%Configuration%StringConfiguration%StringConfigurations(i)%NominalToolJointOd)
			call json%add(pitem,"NumberOfJoint",data%Configuration%StringConfiguration%StringConfigurations(i)%NumberOfJoint)
			call json%add(pitem,"WeightPerLength",data%Configuration%StringConfiguration%StringConfigurations(i)%WeightPerLength)
			call json%add(pitems,pitem)
		end do
		call json%create_object(pbit,"BitDefenition")
		call json%add(pbit,"BitCodeHundreds",data%Configuration%StringConfiguration%BitDefinition%BitCodeHundreds)
		call json%add(pbit,"BitCodeOnes",data%Configuration%StringConfiguration%BitDefinition%BitCodeOnes)
		call json%add(pbit,"BitCodeTens",data%Configuration%StringConfiguration%BitDefinition%BitCodeTens)
		call json%add(pbit,"BitLength",data%Configuration%StringConfiguration%BitDefinition%BitLength)
		call json%add(pbit,"BitNozzleNo",data%Configuration%StringConfiguration%BitDefinition%BitNozzleNo)
		call json%add(pbit,"BitNozzleSize",data%Configuration%StringConfiguration%BitDefinition%BitNozzleSize)
		call json%add(pbit,"BitSize",data%Configuration%StringConfiguration%BitDefinition%BitSize)
		call json%add(pbit,"BitType",data%Configuration%StringConfiguration%BitDefinition%BitType)
		call json%add(pbit,"BitWeightPerLength",data%Configuration%StringConfiguration%BitDefinition%BitWeightPerLength)
		call json%add(pbit,"FloatValve",data%Configuration%StringConfiguration%BitDefinition%FloatValve)

		! call json%add(p,"StringConfigurationCount",dat%Configuration%StringConfiguration%StringConfigurationCount)
        ! 3. add new node to parent
		call json%add(p,pitems)
		call json%add(p,pbit)
		call json%add(parent,p)
    end subroutine
    
end module CStringConfiguration