Created
July 21, 2016 14:52
-
-
Save playerjamesbattleground/ab675b8983dddc2aad175f7e9082d10a to your computer and use it in GitHub Desktop.
Used as Groovy Component in MuleESB Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see <a href="http://stackoverflow.com/questions/6511880/how-to-parse-a-json-input-stream">how-to-parse-a-json-input-stream</a> | |
* @see <a href="http://stackoverflow.com/questions/24835431/use-jackson-to-stream-parse-an-array-of-json-objects">use-jackson-to-stream-parse-an-array-of-json-objects</a> | |
* @author BoostHungry <a href="http://stackoverflow.com/users/1110526/boosthungry">BoostHungry</a> | |
* @author Ian Roberts <a href="http://stackoverflow.com/users/592139/ian-roberts">Ian Roberts</a> | |
* @author leijiang | |
* | |
*/ | |
import static org.apache.commons.lang.StringUtils.* | |
import org.apache.commons.io.IOUtils | |
import org.slf4j.Logger | |
import org.slf4j.LoggerFactory | |
import com.fasterxml.jackson.core.JsonFactory | |
import com.fasterxml.jackson.core.JsonParser | |
import com.fasterxml.jackson.core.JsonToken | |
import com.fasterxml.jackson.core.type.TypeReference | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.node.ObjectNode | |
Logger logger = LoggerFactory.getLogger("mix.notification-api-v1.json-object-iterator") | |
if(!payload instanceof InputStream) { | |
throw new RuntimeException("Wrong payload type returned! Should be InputStream") | |
} | |
def jsonstream = payload | |
interface CloseableJsonObjectIterator extends Iterator, Closeable {} | |
ObjectMapper mapper = new ObjectMapper() | |
JsonFactory factory = mapper.getFactory() | |
JsonParser parser | |
try { | |
parser = factory.createParser(jsonstream) | |
} catch (IOException e) { | |
logger.error("There was a problem setting up the JsonParser: " + e.getMessage(), e); | |
throw new RuntimeException("There was a problem setting up the JsonParser: " + e.getMessage(), e); | |
} | |
try { | |
// Check that the first element is the start of an array | |
JsonToken arrayStartToken = parser.nextToken(); | |
if (arrayStartToken != JsonToken.START_ARRAY) { | |
throw new IllegalStateException("The first element of the Json structure was expected to be a start array token, but it was: " + arrayStartToken); | |
} | |
} catch (final Exception e) { | |
logger.error("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e); | |
throw new RuntimeException("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e); | |
} | |
def nextToken = parser.nextToken(); | |
if (nextToken != JsonToken.START_OBJECT) { | |
throw new IllegalStateException("The second token of Json structure was expected to be a start object token, but it was: " + nextToken); | |
} | |
return [ | |
hasNext: { | |
//println("hasNext? $nextToken") | |
if(nextToken != null && nextToken != JsonToken.END_ARRAY) { | |
return true | |
}else { | |
return false | |
} | |
}, | |
next: { | |
//println("next? $nextToken") | |
ObjectNode node | |
if(nextToken == JsonToken.START_OBJECT) { | |
// read everything from this START_OBJECT to the matching END_OBJECT | |
// and return it as a tree model ObjectNode | |
node = mapper.readTree(parser); | |
} | |
nextToken = parser.nextToken() | |
//println("nextDone? $nextToken") | |
return node.toString() | |
}, | |
close: { | |
IOUtils.closeQuietly(is); | |
parser.close() | |
} | |
] as CloseableJsonObjectIterator | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment