简体   繁体   English

Scala 编码 None 到 NaN json 值与circe

[英]Scala encoding None to NaN json value with circe

I have a scala service that calls to TensorFlow service by HTTP.我有一个 scala 服务,它通过 HTTP 调用 TensorFlow 服务。 I'm trying to encode None value to NaN as JSON with circe (how TensorFlow expect it), but and I'm getting "null" after encoding the object我正在尝试使用 circe 将 None 值编码为 NaN 为 JSON (TensorFlow 是如何预期的),但是在编码 ZA8CFDE6331BD59EB2AC96F8911C4B6666Z 后我得到“null”

Suppose I have the following case class that need to be serialized as JSON objects using circe:假设我有以下情况 class 需要使用 circe 序列化为 JSON 对象:

case class MyRequest(instance: Option[Double])

object MyRequest extends CirceDefaults {
  implicit val autoEncoderRequestEncoder: Encoder[MyRequest] = { (myRequest: MyRequest) =>
    myRequest.instance match {
      case Some(value) => Json.fromDouble(value).asJson
      case None => None.asJson
    }
  }
}

So None.asJson encoded to "null" and not NaN and im getting error from tensorflow service:所以 None.asJson 编码为“null”而不是 NaN 并且我从 tensorflow 服务收到错误:

Error: Invalid argument: JSON Value: "null" Type: String is not of expected type: float"错误:无效参数:JSON 值:“null”类型:字符串不是预期类型:float”

Any help would be greatly appreciated.任何帮助将不胜感激。

This is default Circe behavior.这是默认的 Circe 行为。 Please, see for more details: https://github.com/circe/circe/issues/1267请参阅更多详细信息: https://github.com/circe/circe/issues/1267

And seems like not only for Circe, but in general JSON standard has no such thing as Nan, -Infinity, +Infinity, so in Circe, you can't even create Json for Double.Nan : https://github.com/circe/circe/blob/master/modules/core/shared/src/main/scala/io/circe/Json.scala#L517 And seems like not only for Circe, but in general JSON standard has no such thing as Nan, -Infinity, +Infinity, so in Circe, you can't even create Json for Double.Nan : https://github.com/ circe/circe/blob/master/modules/core/shared/src/main/scala/io/circe/Json.scala#L517

So Json.fromDouble(Double.Nan) returns None - because Double.Nan is not real: https://github.com/circe/circe/blob/master/modules/core/shared/src/main/scala/io/circe/Json.scala#L575所以Json.fromDouble(Double.Nan)返回None - 因为Double.Nan不是真实的: https://github.com/circe/circe/blob/master/modules/core/shared/src/main/scala/io/ circe/Json.scala#L575

Hence, in order to render Nan the way you want to, unfortunately, you will need to edit JSON string after rendering因此,不幸的是,为了以您想要的方式渲染Nan ,您需要在渲染后编辑 JSON 字符串

import io.circe._, io.circe.generic.semiauto._, io.circe.syntax._

case class MyRequest(instance: Option[Double])

object MyRequest {
  implicit val encoder: Encoder[MyRequest] = request => {
    request.instance.fold(Json.fromString("NaN"))(_.asJson)
  }
}

case class BigRequest(request: MyRequest)

object BigRequest {
  implicit val encoder: Encoder[BigRequest] = deriveEncoder[BigRequest]
}

println(BigRequest(MyRequest(Some(1.0d))).asJson.noSpaces)

println(BigRequest(MyRequest(None)).asJson.noSpaces.replace(""""NaN"""", "NaN"))

Scatie: https://scastie.scala-lang.org/SGHtB9GZRpShtQ6A8qAZsA分类: https://scastie.scala-lang.org/SGHtB9GZRpShtQ6A8qAZsA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM