我的部分配置包含任意 JSON。我想将该 JSON 反序列化为 JValue 以供以后处理。
但是,ConfigSource.load
抱怨type
找不到密钥。
测试代码:
import org.json4s.JsonAST.JValue
import pureconfig.ConfigReader.Result
import pureconfig._
import pureconfig.generic.auto._
object PureConfig2JValue extends App {
case class Config(json: JValue)
val source: ConfigObjectSource = ConfigSource.string("{ \"json\": { \"test\": \"test\" } }")
val loadedSource: Result[Config] = source.load[Config]
println(loadedSource)
}
输出:
Left(ConfigReaderFailures(ConvertFailure(KeyNotFound(type,Set()),None,json),List()))
如何让 PureConfig 反序列化为 JValue?
更新:
我将 Gagandeep 的答案改编为我的旧版 PureConfig:
implicit val configReader: ConfigReader[JValue] = new ConfigReader[JValue] {
override def from(cur: ConfigCursor): Either[ConfigReaderFailures, JValue] =
cur.asString match {
case Right(jsonString: String) => Right(parse(jsonString))
case Left(configReaderFailures: ConfigReaderFailures) => Left(configReaderFailures)
}
}
它改变了错误消息,我认为这是进步:
Left(ConfigReaderFailures(ConvertFailure(WrongType(OBJECT,Set(STRING)),None,json),List()))
似乎 PureConfig 在某处期望一个字符串,但找到了一个对象。我不确定断开连接在哪里。我cur.asString
用来确保该项目以其适当的类型返回。
更新 2:
这可能不是最强大的解决方案,但它适用于我的测试用例:
implicit val configReader: ConfigReader[JValue] = new ConfigReader[JValue] {
override def from(cur: ConfigCursor): Either[ConfigReaderFailures, JValue] = {
Right(
// Parse PureConfig-rendered JSON.
parse(
// Render config as JSON.
cur.value.render(ConfigRenderOptions.concise.setJson(true))
)
)
}
}