Skip to content

View Transformations

Jan Bernitt edited this page Jan 20, 2024 · 2 revisions

While the actual JsonNode tree is immutable and never changed for any virtual JsonValue tree layered on top of it the virtual tree can be virtually transformed. This means looking at the same tree through a lens that maps the affected level to some other way to look at it. Usually this is used to "de-objectify" elements or members by mapping to one of their members.

Available view transformations are:

  • JsonArray#viewAsList: maps all array elements resulting in a list view
  • JsonList#viewAsList: maps all list elements resulting in a list view
  • JsonMap#viewAsMap: maps all map members resulting in a map view
  • JsonObject#viewAsObject: maps all object member values resulting in an object view
  • JsonMultiMap#viewAsMultiMap: maps all values of the multimap lists returning in a multimap view

Given the list [{"x":1},{"x":2}] the view

array.viewAsList(e -> e.asObject().getNumber("x"))

would result in an effective value of [1,2]. Again, the underlying tree is not changed by this, just the view on top of it is transformed.

Such transformation can be especially useful in combination with toList to extract a list of the values that can be compared to an expected list.

Clone this wiki locally