document.putProperties: What types of the values are allowed?

59 views
Skip to first unread message

Guofeng Zhang

unread,
Jan 26, 2015, 4:16:55 AM1/26/15
to mobile-c...@googlegroups.com
When creating a document in couchbase-lite in Java, we do it as the following:

 Map<String, Object> properties = new HashMap<String, Object>();
 ......
Document document = database.createDocument();
document.putProperties(properties); 

The map's key is String, but what type of the map's value allowed here? Could it be any complex Java class?

Thanks for your help.

Guofeng

Jens Alfke

unread,
Jan 26, 2015, 11:32:58 AM1/26/15
to mobile-c...@googlegroups.com

On Jan 26, 2015, at 1:16 AM, Guofeng Zhang <guof...@gmail.com> wrote:

The map's key is String, but what type of the map's value allowed here? Could it be any complex Java class?

Documents are stored as JSON, so only types that can be converted to JSON — booleans, numbers, strings, and arrays or maps of those (maps have to have strings as keys.)

—Jens

PS: And JSON has a "null" value, but I don't know if Java has an object for that (it's not the same as a null pointer!)

Guofeng Zhang

unread,
Jan 27, 2015, 6:18:59 AM1/27/15
to mobile-c...@googlegroups.com
Thanks for the reply. 

To confirm one thing about "only types that can be converted to JSON".

Suppose I have a class like:
    public class User {
         public String name;
         public Address address ;
   }

  public class Address { public String line ; }

The User can be converted to JSON, so I think the following should work well, right?
    User u = new User() ; 
    // ....assign some values to the instance
    document.putProperties( "somekey", u ) ;

Thanks again.
       

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/12C9F656-4416-40D8-9F6D-11908BABC950%40couchbase.com.
For more options, visit https://groups.google.com/d/optout.

Jens Alfke

unread,
Jan 27, 2015, 1:33:05 PM1/27/15
to mobile-c...@googlegroups.com

On Jan 27, 2015, at 3:18 AM, Guofeng Zhang <guof...@gmail.com> wrote:

The User can be converted to JSON, so I think the following should work well, right?

I don’t know. I think you’re assuming the encoder will use Java reflection to look through an object’s instance variables and encode the object as a JSON dictionary? I don’t know enough about the Java implementation to know whether it’ll do that. I do know that the Objective-C JSON encoder will not.

Did you try it? Did it work? Hopefully one of the people who works on the Java implementation can answer more definitively.

—Jens

Hideki Itakura

unread,
Jan 27, 2015, 2:27:00 PM1/27/15
to mobile-c...@googlegroups.com
Hi Guofeng,

Couchbase Lite for Android/Java uses Jackson (http://jackson.codehaus.org) to parse and serialize JSON.

To serialize Java object to JSON, CBL calls byte[] ObjectMapper.writeValueAsByte(Object value)

From JSON to Object, CBL calls <T> ObjectMapper.readValue(InputStream src, Class<T> valueType);

For serializing Java object to JSON, I believe Jackson can serialize your User or Address class objects to JSON. 
For parsing JSON, as CBL calls ObjectMapper.readValue() with specifying Map.class as valueType. It is not able to return User or Address class objects. However I believe Jackson returns  nicely populated Map object.

I hope this answers to your question.

Thanks,
Hideki

Guofeng Zhang

unread,
Jan 28, 2015, 2:44:06 AM1/28/15
to mobile-c...@googlegroups.com
Based on the information provided by your replies, I tested and get the following conclusions:

The following statement works well:
 User u = new User() ; 
    // ....assign some values to the instance
    document.putProperties( "somekey", u ) ;

When I use
     Map<String, Object> userMap = (Map<String, Object>)docContent.get("somekey");
I get an object of java.util.LinkedHashMap. then using:
      User v = new ObjectMapper().convertValue(userMap, User.class) ;
to convert it to an instance of User.

the 'null' value in JSON  would be deserialized to null in Java.

Thanks!

Reply all
Reply to author
Forward
0 new messages