Jaka Jančar
Jaka Jančar

Reputation: 11606

Partial bean serialization and deserialization+merging

I am developing a RESTful web service.

I have a bunch of entity classes (mostly JPA entities, but also other beans).

There are gazillions of object mapping, serialization, binding and whatnot libraries out there. I'm looking for a one that will enable me to:

Well, that's it. I've been saying "MUST" a lot, I realize now :) The library needn't actually provide that, but there must be a way to add the functionality in a clean fashion (= not in a way that would make rewriting everything easier).

Upvotes: 3

Views: 2663

Answers (2)

StaxMan
StaxMan

Reputation: 116620

For what it is worth, Jackson has partial update out-of-the-box:

ObjectMapper mapper = new ObjectMapper();
Bean existing = ...;
mapper.updatingReader(existing).readValue(jsonSource);

and it can also convert compatible types (similar to serializing to JSON, reading back to different type).

For XML part you can use JAXB, although it can only complete binding as far as I know.

Upvotes: 1

Scott Stanchfield
Scott Stanchfield

Reputation: 30652

I don't know of existing libraries that do everything you need, but, assuming you're going to need to implement something:

  • Writing XML or JSON based on bean properties is pretty simple:

    • Look at using Apache Commons BeanUtils to be able to get all property values (http://commons.apache.org/beanutils/). In particular, the PropertyUtils class.
    • Use BeanUtils recursively to walk your entire object graph - be careful of cycles - you'll need a Set or something on the side to keep track of what you've already seen
    • XML: Look at XMLEncoder - it works using JavaBean properties to create XML
  • For reading: One approach might be to use existing libs (for JSON or XML) to create objects then deal with merging the properties between objects. The XMLDecoder class can read bean XML (assuming you create it using XMLEncoder). The tricky part to this approach is to know when a value has been "set" to null vs just not been set in the XML. This approach also has the extra overhead of creating a bunch of new objects.

  • Otherwise, reading JSON or XML is a little trickier, but not too bad

    • I assume you already have some means of indexing the objects you want to merge into (like a Map of some sort)
    • I assume you already have some means to know which property is the key that uniquely identifies an object (I assume last name is just to get the point across, as it would make a bad key)
    • XML: For this type of use, I'd recommend a SAX reader for the xml - you'll need a stack to keep track of which objects you're adding data to. The SAX reader tells you what tags are seen and then gives the values for those tags. You could also use XML pull here, which tends to be a little faster
    • JSON: take a look at some of the open source JSON libraries and do some tweaking. JSON is pretty simple to parse, and these tools tend to be pretty small so this shouldn't be a big deal. Alternatively, you could write an ANTLR (or other generator) parser to read the JSON and do with it as you like.

Upvotes: 1

Related Questions