diff --git a/docs/changelog.qmd b/docs/changelog.qmd index 44d4798f7..749e0f524 100644 --- a/docs/changelog.qmd +++ b/docs/changelog.qmd @@ -21,6 +21,9 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). `SimpleReservoir` and `Lake` struct, and not to the corresponding river index. This resulted in incorrect surface water abstractions from reservoir and lake volumes, and surface water abstractions were set at zero at the wrong river locations. +- Wflow ZMQ server: allow JSON reading and writing of `NaN` and `Inf` values to avoid a JSON + spec error. For example, during initialization of a wflow model, some (diagnostic) model + variables are initialized with `NaN` values. ### Changed - Removed vertical concepts `HBV` and `FLEXTopo`. diff --git a/server/src/server.jl b/server/src/server.jl index b3aded4be..33cae0d33 100644 --- a/server/src/server.jl +++ b/server/src/server.jl @@ -89,7 +89,7 @@ function wflow_bmi(s::ZMQ.Socket, handler::ModelHandler, f) response(s) else @info "Send response including output from Wflow function `$(f.fn)`" - ZMQ.send(s, JSON3.write(ret)) + ZMQ.send(s, JSON3.write(ret; allow_inf = true)) end catch e @error "Wflow function `$(f.fn)` failed" exception = (e, catch_backtrace()) @@ -151,7 +151,7 @@ function start(port::Int) while true # Wait for next request from client req = ZMQ.recv(socket) - json = JSON3.read(req) + json = JSON3.read(req; allow_inf = true) @info "Received request to run function `$(json.fn)`..." if haskey(map_structs, json.fn) diff --git a/server/test/client.jl b/server/test/client.jl index 58f039923..bb03c40d9 100644 --- a/server/test/client.jl +++ b/server/test/client.jl @@ -10,7 +10,7 @@ ZMQ.connect(socket, "tcp://localhost:5555") function request(message) ZMQ.send(socket, JSON3.write(message)) - ret_value = JSON3.read(ZMQ.recv(socket), Dict) + ret_value = JSON3.read(ZMQ.recv(socket), Dict; allow_inf = true) return ret_value end @@ -24,6 +24,12 @@ end @test request((fn = "get_time_units",)) == Dict("time_units" => "s") end +@testset "Reading and writing NaN values allowed" begin + msg = + (fn = "get_value", name = "vertical.soil.variables.vwc[1]", dest = fill(0.0, 50063)) + @test isnan(mean(request(msg)["value"])) +end + @testset "update functions" begin @test request((fn = "update_until", time = 86400.0)) == Dict("status" => "OK") @test request((fn = "get_current_time",)) == Dict("current_time" => 86400)