Search code examples
jsonjacksonamazon-cloudsearch

SDF (SearchDataFormat) Requires Atypical JSON


Using Jackson to interface to Amazon CloudSearch, which ingests data in the format SDF. Here is an example:

[ {
  "type" : "add",
  "id" : "images_to_search_csv_1",
  "version" : 1336526759,
  "lang" : "en",
  "fields" : {
    "content" : "http://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Timba%2B1.jpg/220px-Timba%2B1.jpg",
    "title" : "Simba",
    "source" : "wikipedia",
    "description" : "Simba the wolf dog.",
    "type" : "Image"
  }
}, {
  "type" : "add",
  "id" : "images_to_search_csv_2",
  "version" : 1336526760,
  "lang" : "en",
  "fields" : {
    "content" : "http://www.wolfzone1.com/photos/sedona-01d%20copy.jpg",
    "title" : "Wolf",
    "source" : "Wolf Zone",
    "description" : "Another wolf.",
    "type" : "Image"
  }
} ]

Notice that the fields are fields of the entity that you are submitting. So clearly, if you just bind that entity to the SDF class, you get the entity and then its fields but the structure of the file ends up different. Should I just write the code to output this by hand rather than have Jackson do it?


Solution

  • You can write custom deserializers/serializers, to only write handlers for that portion manually; registration can be done globally for types, as well as on per-property basis. This way you can reduce amount of explicit work quite a bit. It is also possible to use multi-stage processing: first binding to something generic (Map, JsonNode), transform structure a bit, then convert to/from POJO (`ObjectMapper.convertValue()').