json4s中`render`的目的 [英] Purpose of `render` in json4s
问题描述
在json4s的示例和文档中,我经常看到惯用法
In json4s examples and documentation I often see the idioms
compact(render(jval))
和
pretty(render(jval))
我认为我实际上没有见过将紧凑或漂亮的示例直接应用于生成的代码JValue
的示例,
但是我不清楚render
在这里做什么.
渲染的类型为JValue => JValue
,我看不到有什么明显的不同
并运行
I do not think I have actually seen an example with compact or pretty applied directly to a code generated JValue
,
but it is not clear to me what render
is doing here.
Render has type JValue => JValue
and I do not see any obvious difference it makes
and running
json.take(100000).filter(x => compact(render(x)) != compact(x))
对我的某些数据返回一个空的空集合.
on some of my data returns an empty an empty collection.
render
的实际作用是什么?
What does render
actually do?
推荐答案
I guess you were looking at one of the concrete implementations of the render
method, which definition you can see in the JsonMethods trait
:
def render(value: JValue)(implicit formats: Formats = DefaultFormats): T
def compact(d: T): String
def pretty(d: T): String
方法render
返回泛型类型T
,这是compact
和pretty
方法的条目类型.
The method render
returns a generic type T
, which is the entry type for the compact
and pretty
methods.
根据 native 和 jackson 的口味, json4s 项目中有两种方法render
的实现.只是表面上检查了代码,但它们似乎都根据不同的策略过滤了json对象的空元素.假设要准备好使用pretty
和/或compact
方法了吗?
There are two implementations of the method render
in the json4s project, as per the native and jackson flavours... I've checked the code superficially only but they both seem to be filtering the empty elements of the json object according to different strategies. Let's say getting it ready for the pretty
and / or compact
methods to kick in?
这篇关于json4s中`render`的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!