Class JsonToMetadata

java.lang.Object
com.adaptris.core.ServiceImp
com.adaptris.core.json.JsonToMetadata
All Implemented Interfaces:
AdaptrisComponent, ComponentLifecycle, ComponentLifecycleExtension, ConfigComment, MessageEventGenerator, Service, StateManagedComponent

@ComponentProfile(summary="Extract all root fields from a JSON document as metadata", tag="service,json,metadata") public class JsonToMetadata extends ServiceImp
Takes a JSON payload and adds every field at the root element as metadata.

This class is included as a convenience service for you to extract all the fields of a simple JSON document as metadata. In the situation where you have a complex nested structure, then you continue use JsonPathService as normal. In the event that there are nested objects, they will be turned into strings and added as-is.

  
    {
        "category": "fiction",
        "title": "The Lord of the Rings Trilogy",
        "price": 22.99,
        "volumes": [1, 2, 3],
        "suggested": {
            "title": "The Hobbit"
        }
    }
  
  

will give you 5 metadata keys category, title, price, volumes, suggested. The field volumes is not a string (it would be an array of integers), but it will be set as metadata as though it were (i.e the value is [1,2,3]); similarly, suggested is a nested JSON object, but it will be simply rendered as a string and added as metadata (i.e the value will be {"title:"The Hobbit"}).

In the adapter configuration file this class is aliased as json-to-metadata which is the preferred alternative to the fully qualified classname when building your configuration.

  • Constructor Details

    • JsonToMetadata

      public JsonToMetadata()
  • Method Details

    • doService

      public void doService(AdaptrisMessage msg) throws ServiceException
      Throws:
      ServiceException
    • prepare

      public void prepare() throws CoreException
      Throws:
      CoreException
    • closeService

      protected void closeService()
      Specified by:
      closeService in class ServiceImp
    • initService

      protected void initService() throws CoreException
      Specified by:
      initService in class ServiceImp
      Throws:
      CoreException
    • getMetadataPrefix

      public String getMetadataPrefix()
    • setMetadataPrefix

      public void setMetadataPrefix(String prefix)
      Specify a metadata prefix if you need one.
      Parameters:
      prefix - the prefix, defaults to the empty string.
    • getNullConverter

      public NullConverter getNullConverter()
      Returns:
      the nullConverter
    • setNullConverter

      public void setNullConverter(NullConverter nc)
      Specify the behaviour when a NullNode is encountered.
      Parameters:
      nc - the NullConverter to set, the default is effectively the string "null" as returned by NullNode#asText()