couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sander Dijkhuis <m...@sanderdijkhuis.nl>
Subject Decimal values in JSON
Date Tue, 16 Apr 2013 11:34:48 GMT
Hi,

I’m working on a CouchDB design doc that provides a Cosm-like API to work with sensor measurements.
I know that the CouchDB guide recommends using strings to represent decimal values in JSON[1],
but was curious to try using the { "field": 4.2 } format as it gives a cleaner API. I figured
that as long as data is sent in the right format, CouchDB might handle it well as JSON doesn’t
specify that decimals should be considered as floats.

So far my update handler measurement(doc, req) seems to process the data correctly. The raw
req.body input string contains the value represented with { "field": 4.2 }, and after JSON-parsing
the input (but not changing the decimal field), the ‘doc’ variable that’s sent back
to CouchDB still seems to contain the field as 4.2, at least as shown by log(JSON.stringify(doc)).

Only when I request the _changes feed, any map/reduce view or a particular doc, the value
comes out of CouchDB as { "field": 4.200000000002 } in JSON. I wonder if this is a bug in
CouchDB? Or did the log(doc) already deceive me and use different JSON serialisation than
CouchDB uses when processing the update handler’s output?

Sander

[1] http://guide.couchdb.org/draft/json.html#numbers


Mime
View raw message