Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The catamorphism for the JSON value data type.
- */
- final def fold[X](
- jsonNull: => X,
- jsonBoolean: Boolean => X,
- jsonNumber: JsonNumber => X,
- jsonString: String => X,
- jsonArray: Vector[Json] => X,
- jsonObject: JsonObject => X
- ): X = this match {
- case JNull => jsonNull
- case JBoolean(b) => jsonBoolean(b)
- case JNumber(n) => jsonNumber(n)
- case JString(s) => jsonString(s)
- case JArray(a) => jsonArray(a)
- case JObject(o) => jsonObject(o)
- }
- //usage:
- json.fold(
- None,
- boolValue => Some(s"This is boolean: $boolValue"),
- numberValue => Some(s"This is integer: ${numberValue.toInt}"),
- _ => None,
- _ => None,
- _ => None,
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement