json-lenses icon indicating copy to clipboard operation
json-lenses copied to clipboard

Issue with Optional Fields

Open conikeec opened this issue 11 years ago • 1 comments

I encountered an interesting situation where optional fields does not address to solve this problem

val json = JsonParser(
"""
{
"host" : {
"name" : "disposable-cluster-05.cphy.local",
"config" : {
"storageDevice" : {
"scsiLun" : [ 
{
"uuid" : "0005000000766d68626133323a303a30",
"lunType" : "cdrom"
}, 
{
"capacity" : {
"blockSize" : 512,
"block" : 1048576000
},
"devicePath" : "/vmfs/devices/disks/naa.6000c2914633a6cf3c6b48a67089ca09",
"ssd" : false,
"uuid" : "02000000006000c2914633a6cf3c6b48a67089ca09566972747561",
"lunType" : "disk"
}, 
{
"capacity" : {
"blockSize" : 512,
"block" : 8388608
},
"devicePath" : "/vmfs/devices/disks/mpx.vmhba1:C0:T0:L0",
"ssd" : false,
"uuid" : "0000000000766d686261313a303a30",
"lunType" : "disk"
}
]
}
}
}
}
""".stripMargin)

The result returned is 2 elements (rightfully) as the first element of the array is missing for valid reasons.

> json.extract[Boolean]("host" / "config" / "storageDevice" / "scsiLun" / * / optionalField("ssd"))
res12: Seq[Boolean] = List(false, false)

Now there is a positional implication in this extraction, since 2 elements are returns (subscript (1),(2) and not (0)) ... How will I align these two elements with other elements that I extract from this Array ?

Is it possible to receive a List("default value if missing",false,false) in subscript(0) of the array element ... ?

conikeec avatar Jul 04 '13 14:07 conikeec

There's currently no way to do that. The reason is that the combination operator / automatically gets rid of the element (by using flatMap for combination). I guess the easiest solution would be to provide another combination operator that instead nests results. I'm not sure when and I get to this.

jrudolph avatar Jul 07 '13 19:07 jrudolph