With the introduction of Functional Programming in Java 8 new possibilities have opened up. One use case I recently encountered was that of processing JSONs to return data inside them. Let us, for the sake of argument, say the data in an element could be one of the following:
- An
Integere.g.1 - A
Stringe.g.The quick brown fox jumps over the lazy dog - A
JsonObjectrepresenting a Java Object e.g.{"payload":[{"lastName\":\"AB","firstName":"Karun","email":"test[at]email.com"} - A
JsonArraycontainingIntegers e.g.[1, 2, 3] - A
JsonArraycontainingStrings e.g.["String 1", "String 2"] - A
JsonArraycontainingJsonObjects representing Java Objects e.g.[{"lastName":"AB","firstName":"Karun","email":"test[at]email.com"},{"lastName":"FooBar","firstName":"Kung","email":"kung[at]foobar.com"}]
If you want such a wide variety of data parsed and handled (relatively) safely (i.e. with compile time type safety where possible), here is what you do.
