#1 2015-08-07 17:38:18

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

ObjectToJSON/MongoDB/JSONToObject and Double

Stumbled upon a weird issue.
I have a class like this (omitting useless fields):

  TMyClass = class(TPersistent)
  private
    fMyVal: Double;
  published
    property MyVal: Double read fMyVal write fMyVal;
  end;

Say I assign a number with a lot of decimals here:

MyObj.MyVal := 0.234500005841255;

Then ObjectToJSON produces the following output (encoded in scientific notation, no double quotes, and WRONG number by the way):

{
  "MyVal" : 5.2E-0315,
}

Then I call:

fMyCollection.Insert(LGeneratedJSON, []);

After inserting it into my MongoDB, I run RoboMongo to see what the document in MongoDB looks like.
And here's what I see:

{
  "MyVal" : "5.2E-0315",
}

See? MongoDB received or considered that as a String value (added double quotes).
For such reason when I query my MongoDB and then call JSONToObject back again, I get a ACCESS VIOLATION.

VERY IMPORTANT NOTE: The above behavior ONLY happens if you compile at 64 bit, the 32 bit generated EXE behaves properly.

Last edited by BBackSoon (2015-08-07 17:52:45)

Offline

#2 2015-08-07 17:48:19

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: ObjectToJSON/MongoDB/JSONToObject and Double

The same code.
Compiled with the 32 bit compiler calling ObjectToJSON generates this JSON:

{
  "MyVal" : 0.234500005841255,
}

Instead when compiled with the 64 bit compiler, it produces this JSON:

{
  "MyVal" : 5.2E-0315,
}

Offline

#3 2015-08-07 23:05:16

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: ObjectToJSON/MongoDB/JSONToObject and Double

Sorry, errata corrige: the problem occurs when the property is SINGLE, not DOUBLE.
With DOUBLE it works flawlessly.
Thank you.

Offline

Board footer

Powered by FluxBB