You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Sander Dijkhuis <ma...@sanderdijkhuis.nl> on 2013/04/16 13:34:48 UTC
Decimal values in JSON
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
Re: Decimal values in JSON
Posted by Sander Dijkhuis <ma...@sanderdijkhuis.nl>.
Hi Elisiano, thanks for the link!
Just for reference, the discussion seems to have resulted in this explanation in the CouchDB docs:
http://couchdb.readthedocs.org/en/latest/json-structure.html#number-handling
On Apr 16, 2013, at 14:17 , Elisiano Petrini <el...@gmail.com> wrote:
> Hi Sander,
> there has been an extended discussion on this list a couple of months
> ago about float numbers and their representation.
>
> Have a look at
> http://mail-archives.apache.org/mod_mbox/couchdb-user/201302.mbox/%
> 3Ckfvbob$c6p$1@ger.gmane.org%3E and follow the thread from there (sorry
> I was unable to find a better view/link for that), it might answer some
> of your questions.
>
> Best Regards,
> Elisiano Petrini
>
>
> On Tue, 2013-04-16 at 13:34 +0200, Sander Dijkhuis wrote:
>> 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
>>
>
>
Re: Decimal values in JSON
Posted by Elisiano Petrini <el...@gmail.com>.
Hi Sander,
there has been an extended discussion on this list a couple of months
ago about float numbers and their representation.
Have a look at
http://mail-archives.apache.org/mod_mbox/couchdb-user/201302.mbox/%
3Ckfvbob$c6p$1@ger.gmane.org%3E and follow the thread from there (sorry
I was unable to find a better view/link for that), it might answer some
of your questions.
Best Regards,
Elisiano Petrini
On Tue, 2013-04-16 at 13:34 +0200, Sander Dijkhuis wrote:
> 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
>