Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of `render` in json4s

Tags:

json

scala

json4s

In json4s examples and documentation I often see the idioms

compact(render(jval))

and

pretty(render(jval))

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.

What does render actually do?

like image 547
Daniel Mahler Avatar asked Oct 28 '14 02:10

Daniel Mahler


1 Answers

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

The method render returns a generic type T, which is the entry type for the compact and pretty methods.

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?

like image 73
MacLuq Avatar answered Oct 14 '22 13:10

MacLuq