Package com.adaptris.core.json.streaming
Class JsonPathStreamingService
- java.lang.Object
-
- com.adaptris.core.ServiceImp
-
- com.adaptris.core.services.path.json.JsonPathServiceImpl
-
- com.adaptris.core.json.streaming.JsonPathStreamingService
-
- All Implemented Interfaces:
AdaptrisComponent,ComponentLifecycle,ComponentLifecycleExtension,MessageEventGenerator,Service,StateManagedComponent
@ComponentProfile(summary="Extract a value from a large JSON document", tag="service,transform,json,metadata,streaming,large") public class JsonPathStreamingService extends com.adaptris.core.services.path.json.JsonPathServiceImplThis service allows you to search JSON content and the results are then set back into the message. The advantage of this implementation is that it doesn't need to parse the entire JSON document and so is able to handle arbitrarily large documents.The searching works in much the same way as XPath, for more information on how to build a JSON path see the JSONPath documentation and the JSON Surfer> documentation.
For example, if you have a message with the following payload:
You could configure 2 target destinations, each one creating a new metadata item with the results of the specified search, like this;{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }
The first target above searches for the first book title, the second target searches for the second book title. Each target-destination will be executed in the order they are configured and therefore with the two targets shown here, your message, after the service has run, will include two new metadata items;<json-path-streaming-service> <json-path-execution> <source class="constant-data-input-parameter"> <value>$.store.book[0].title</value> </source> <target class="metadata-data-output-parameter"> <metadata-key>metadata-key-1</metadata-key> </target> <suppress-path-not-found>true</suppress-path-not-found> </json-path-execution> <json-path-execution> <source class="constant-data-input-parameter"> <value>$.store.book[1].title</value> </source> <target class="metadata-data-output-parameter"> <metadata-key>metadata-key-2</metadata-key> </target> </json-path-execution> </json-path-streaming-service>- metadata-key-1 = "Sayings of the Century"
- metadata-key-2 = "Sword of Honour"
Any results returned by this service will normally include the json brackets wrapping the returned value. However you can configure this service to unwrap the result for you, such that a value returned as "[myValue]" will now be returned as "myValue".
The default value is false, but to override simply configure the "unwrap";<json-path-service> <unwrap-json>true</unwrap-json> ... </json-path-service>In the adapter configuration file this class is aliased as json-path-streaming-service which is the preferred alternative to the fully qualified classname when building your configuration.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJsonPathStreamingService.Surfer
-
Field Summary
-
Fields inherited from class com.adaptris.core.services.path.json.JsonPathServiceImpl
executions, unwrapJson
-
Fields inherited from class com.adaptris.core.ServiceImp
log
-
-
Constructor Summary
Constructors Constructor Description JsonPathStreamingService()JsonPathStreamingService(DataInputParameter<java.io.InputStream> source, Execution... executions)JsonPathStreamingService(DataInputParameter<java.io.InputStream> source, java.util.List<Execution> executions)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddoService(AdaptrisMessage message).@NonNull DataInputParameter<java.io.InputStream>getSource()The source for executing the jsonpath against.@NonNull JsonPathStreamingService.SurfergetSurfer()The JSON surfer implementation to use.voidprepare()voidsetSource(@NonNull DataInputParameter<java.io.InputStream> source)The source for executing the jsonpath against.voidsetSurfer(@NonNull JsonPathStreamingService.Surfer surfer)The JSON surfer implementation to use.-
Methods inherited from class com.adaptris.core.services.path.json.JsonPathServiceImpl
closeService, convertIfNull, getExecutions, getUnwrapJson, initService, setExecutions, setUnwrapJson, suppressPathNotFound, toString, unwrap, unwrapJson
-
Methods inherited from class com.adaptris.core.ServiceImp
changeState, close, continueOnFailure, createName, createQualifier, getContinueOnFail, getIsTrackingEndpoint, getUniqueId, init, isBranching, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, setContinueOnFail, setIsTrackingEndpoint, setUniqueId, start, stop
-
-
-
-
Constructor Detail
-
JsonPathStreamingService
public JsonPathStreamingService(DataInputParameter<java.io.InputStream> source, Execution... executions)
-
JsonPathStreamingService
public JsonPathStreamingService(DataInputParameter<java.io.InputStream> source, java.util.List<Execution> executions)
-
JsonPathStreamingService
public JsonPathStreamingService()
-
-
Method Detail
-
doService
public void doService(AdaptrisMessage message) throws ServiceException
.- Throws:
ServiceException
-
prepare
public void prepare()
-
getSource
@NonNull public @NonNull DataInputParameter<java.io.InputStream> getSource()
The source for executing the jsonpath against.
-
setSource
public void setSource(@NonNull @NonNull DataInputParameter<java.io.InputStream> source)The source for executing the jsonpath against.
-
getSurfer
@NonNull public @NonNull JsonPathStreamingService.Surfer getSurfer()
The JSON surfer implementation to use.
-
setSurfer
public void setSurfer(@NonNull @NonNull JsonPathStreamingService.Surfer surfer)The JSON surfer implementation to use.
-
-