Question on serialization/deserialization

Hi, I’m using Swift with CB Lite. I’m actually new to both. I’m looking for best practices on serialize/de-serialize between strongly typed Swift objects and CB ResultSet/Result which appear to be standard Dictionary objects.

This problem isn’t unique to Swift, but I’m interested if anyone else is using Swift and how they’re addressing the issue. It appears one must write their own converters for Result objects, but even that requires some introspection, which I suspect one would have the context based on the query executed.

I ran across this SO posting by @borrrden researching this issue:

The post was dated a year ago. I’m interested to hear if there has been any other new thinking or progress on this?

If JSON strings were exposed, it would be trivial to serialize/de-serialize, but I get the fact that this is not how the data is stored…but some assistance on how to convert between totally generic objects/dictionaries in a strongly typed language would be interesting to hear.

I’m interested to know if there’s anything out there like this for Swift yet: (or maybe I will have to write it :slight_smile:

Using JSON strings would be kind of convoluted, as Couchbase Lite would end up serializing the document to JSON, you’d pass that to some Swift utility, which would then deserialize the JSON to an object tree, and then use that object tree to populate your model object.

We already give you an object tree, so you can use that directly to populate your objects. There are Swift libraries that make this easier; they often take JSON strings, but if you look at their code the first thing they do is parse the JSON into a Dictionary, so hopefully they also expose API that takes a Dictionary directly.

Yea, I’ve come to the conclusion that I’ll be building my own encoder/decoder in Swift perhaps using Mirror API or Codable. Not a huge hurdle, but that seems to be the way forward. Maybe we’ll OSS it – who knows :slight_smile:

FYI …for your homegrown mapping library - I had done some work on codables a while ago that you may find useful . This unrelated to couchbase lite. I seemed to have lost some of my work w/ Mirror API …
While the homegrown option should work, as Jens noted, it may be worthwhile checking if there are libraries that convert from Dictionary to native objects directly to avoid the multiple layers of conversion

Would be great if you can OSS your library!

Are there any news on this topic? It’s quite unfortunate that it’s not possible to use Codable out of the box.

Is the intended way to do it to convert objects by hand?

We do not have an officially supported way to do that in our product yet but we have a couchbaselabs project that you can use as a starting point. There is work to do but it is a good start/reference. Contributions welcome! (CC @pasin )