Converting objects to dictionaries and back

.net

#1

What is the best practice for converting objects to dictionaries and back? For example, we have the following class:

public class User
{
public string FirstName { get; private set; }

public string LastName { get; private set; }

public DateTime Birthday { get; private set; }

public string Gender { get; private set; }

public string Email { get; private set; }


public User(string firstName, string lastName, DateTime birthday, string gender, string email)
{
    Email = email;
    FirstName = firstName;
    LastName = lastName;
    Birthday = birthday;
    Gender = gender;
}

}

Currently, i am using an extension method from stackoverflow (http://stackoverflow.com/a/4944547/6939988) to serialise this as a dictionary in order to input it into a document. However, the key names are therefore not camel case and sometimes the method doesn’t know how to convert a list etc.

Another way is to serialise the object as json and then deserialise it as a dictionary then input it to the document. Then, I am assuming, the document serialises the dictionary back to json. This seams pretty inefficient.

Obviously, we can just input the dictionary key/values directly but this isn’t very nice and it means we can’t share the schema class across our teams or between couchbase server projects.

So, I am just wondering what is the best practice, and how are others dealing with this?


#2

any thoughts on this?


#3

I’m not a Java programmer so I can’t answer the question, but my understanding is that there are a lot of Java libraries for object-to-JSON mapping; you should probably shop around and pick one that works the way you like.

I don’t think there is a single best practice; it seems to be based on people’s preferences and their priorities.


#4

Oh, wait, that’s Swift, isn’t it? (Hint: It always helps if you clearly state what platform/language you’re concerned with. It can be hard to tell languages apart at first glance.)

Just use CBLModel. It’s already built-in, and it’s compatible with Swift.

(And if you don’t like it, then there are multiple open source Swift libraries for serializing Swift objects to/from JSON. Look around and pick your favorite.)


#5

Ok, thanks.
I am actually developing in .net c#. Sorry, I tagged .net in the title. I should have been more descriptive.


#6

It’s currently best to simply manually serialize into a dictionary (I like to think of it like apple’s NSCoder). You could have a base class that passes a dictionary to a protected virtual method that will pass the dictionary in and receive all subclasses information if you like. I am currently in the R&D phase of getting a more automatic method for C#.


#7

Ok cool thanks. I look forward to seeing the automated method!!


#8

Wondering… is there a modern Swift equivalent to CBLModel within the current Couchbase Lite library for iOS / Swift?


#9

Looks like you have posted the same/similar question in several places. I had responded here but I will mention here again - We currently do not have support for mapping POSO to Document types and while it is on our radar, there is no immediate plans of supporting the same.


#10

Thanks @priya.rajagopal.

So is it possible to get the JSON from a query ResultSet?

If that is possible then I could write my own decoder/encoder functions.


#11

No, it isn’t. (It isn’t JSON internally, FWIW; we use a more efficient binary encoding called Fleece.)
I’m not sure how that relates to CBLModel anyway, as a query result ≠ a document.

You can implement Swift models by writing implementation of Swift’s Encoder and Decoder protocols that use CBLDictionary objects.


#12

Thanks @jens . I’ll investigate that avenue.