I receive JSON response from an upstream server that I do not have a control on, for modification.
The JSON could have the following format
{"data":[["Some_text","boolean",["key_string1","key_string2"]]]}
Or it could also show up as
{"data":[["Some_text","boolean","key_string2"]]}
Or it could show up as a combination of the two.
{"data":[["Some_text","boolean",["key_string1","key_string2"]],["Some_text","boolean","key_string2"]]}
Individually I can define the READS for each format if they don't mix. However, given that the data may be of mixed format, I am unable to wrap my head around how the Reads should be written so as to check if the underlying type is a string or array before transforming it?
Is it possible to say something like
(
(JsPath)(0).read[String] and
(JsPath)(1).read[Boolean] and
(JsPath)(2).read( **if type is simple, String OR if type is array, then seq** )
)(SomeGloriousCaseClass)
How can I approach this problem of deserializing?
case class SomeGloriousCaseClass(initialPart: String, booleanPart: Boolean, theDifficultPart: Seq[String]). I don't even mind if everything gets converted into a sequence - Serendipity(_ \ "data").read[JsArray].flatMap { jsa => ??? }- cchantepJsValueand then it's up to you to write a function that given aJsValue, return aSeq[String]. I am thinking of a recursive method which match on the value, and if the value is a string concatenate it to the final seq if it is an array recurse over it. You might be able to do this with afoldLeftmethod - Louis F.JsArray, that's an easy first validation - cchantep