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!
On Wed, Jan 28, 2015 at 3:27 AM, Hideki Itakura <[email protected]>
wrote:
> 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)
>
> http://jackson.codehaus.org/1.6.2/javadoc/org/codehaus/jackson/map/ObjectMapper.html#writeValueAsBytes(java.lang.Object)
>
> From JSON to Object, CBL calls <T> ObjectMapper.readValue(InputStream src,
> Class<T> valueType);
>
> http://jackson.codehaus.org/1.6.2/javadoc/org/codehaus/jackson/map/ObjectMapper.html#readValue(java.io.InputStream,
> java.lang.Class)
>
> 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
>
> On Tuesday, January 27, 2015 at 3:18:59 AM UTC-8, Guofeng Zhang wrote:
>>
>> 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.
>>
>>
>> On Tue, Jan 27, 2015 at 12:31 AM, Jens Alfke <[email protected]> wrote:
>>
>>>
>>> On Jan 26, 2015, at 1:16 AM, Guofeng Zhang <[email protected]> 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!)
>>>
>>> --
>>> 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 [email protected].
>>> To view this discussion on the web visit https://groups.google.com/d/
>>> msgid/mobile-couchbase/12C9F656-4416-40D8-9F6D-
>>> 11908BABC950%40couchbase.com
>>> <https://groups.google.com/d/msgid/mobile-couchbase/12C9F656-4416-40D8-9F6D-11908BABC950%40couchbase.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mobile-couchbase/dd0d76d4-d16d-4047-92f2-f98256ecce07%40googlegroups.com
> <https://groups.google.com/d/msgid/mobile-couchbase/dd0d76d4-d16d-4047-92f2-f98256ecce07%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mobile-couchbase/CAMpUGn0RXSwwrR77odsHHUEs%2BKBNP8cEX50YbD6cvsvjHJqwmQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.