nlight-jdev / jcouchdb

Automatically exported from code.google.com/p/jcouchdb
Other
0 stars 0 forks source link

setJsonConfig(); not working as expected #40

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I want to register a DateConverter to automatically convert java.util.Dates as 
was described in the last comment overthere:

http://code.google.com/p/svenson/wiki/TypeConversion

Here is what I do:

// 1. Create the Database object
Database couchdb = new Database(host, databaseName);
// 2. Get the configuration from my Database 
JSONConfig jsonConfig = couchdb.getJsonConfig();
// 3. get the parser
JSONParser jsonParser = jsonConfig.getJsonParser();
// 4. create a new parser
jsonParser = new JSONParser();
// 5. and register the converter
jsonParser.registerTypeConversion(java.util.Date.class, new DateConverter());
// 6. finally, submit the new configuration to the Database
couchdb.setJsonConfig(new JSONConfig(jsonConfig.getJsonGenerator(), 
jsonParser));

What is the expected output? What do you see instead?

When I call couchdb.getDocument(MyDocument.class, someID) 
where "MyDocument" is a bean with setDate(java.util.Date date) and getDate() 
(returning a java.util.Date)
I get a 

org.svenson.JSONParserException: Cannot set property day on class 
java.util.Date.

The post mentioned above ( http://code.google.com/p/svenson/wiki/TypeConversion 
) pointed me to Svenson's registerTypeConversion() method. 
However, the couchdb.setJsonConfig() method does not seem to propagate the 
submitted JSONParser when it retrieves and parses responses from the 
server. 

What version of the product are you using? On what operating system?

jcouchdb-0.10.0-3.jar
svenson-1.3.6.jar

Original issue reported on code.google.com by stefan.u...@gmail.com on 26 Feb 2010 at 2:29

GoogleCodeExporter commented 8 years ago
Your problem here is that you don't register the TypeConverter in the JSON 
generator.
So the date gets treated like a normal bean on generation and then the 
typeconverter
that you registered fails to parse that. If you register it in the generator, 
too,
your example works

(
http://code.google.com/p/jcouchdb/source/browse/trunk/test/org/jcouchdb/db/TypeC
onverterPropagationTestCase.java
)

I think there's also some weird behaviour with type converters at play here, 
hidden
beneath the surface.. The type converters registered by registerTypeConversion 
behave
differently from those added via a typeconverter-repository, because the former 
are
only active "after-the-fact", just being used to convert strange primitive 
values.
(like String -> Date). 

I will have to think about how to improve that.

Original comment by ff...@gmx.de on 3 Mar 2010 at 11:16

GoogleCodeExporter commented 8 years ago
Thanks for checking this out! I will have another try.

Original comment by stefan.u...@gmail.com on 4 Mar 2010 at 7:10