JSON bug

93 views
Skip to first unread message

Óscar Hernández Suárez

unread,
Nov 11, 2012, 7:19:13 AM11/11/12
to harbou...@googlegroups.com
---8<---
PROCEDURE Main()
   LOCAL a

   hb_JsonDecode( '{ "N": 12.123456 }', @a )
   ? Str( a[ "N" ], 12, 6 ), "Good"
   ? hb_JsonEncode( a ), "Bad!"

   a := { "N" => 12.123456 }
   ? Str( a[ "N" ], 12, 6 ), "Good"
   ? hb_JsonEncode( a ), "Good"

   hb_JsonDecode( '{ "N": 5e2 }', @a )

   ? Str( a[ "N" ], 18, 6 ), "Bad!"
   ? hb_JsonEncode( a ), "Bad!"

   hb_JsonDecode( '{ "N": 5e-2 }', @a )

   ? Str( a[ "N" ], 18, 6 ), "Bad!"
   ? hb_JsonEncode( a ), "Bad!"
   RETURN
--->8---

The output is:
---8<---
   12.123456 Good                                                                                                                                                         
{"N":12.12} Bad! 
   12.123456 Good
{"N":12.123456} Good
          5.000000 Bad!
{"N":5.00} Bad!        
          5.000000 Bad!
{"N":5.00} Bad!  
--->8---

The following patch fixes it:
---8<---
Index: hbjson.c
===================================================================
--- hbjson.c (revision 18510)
+++ hbjson.c (working copy)
@@ -481,6 +481,7 @@
       HB_MAXINT nValue = 0;
       double dblValue = 0;
       HB_BOOL fNeg, fDbl = HB_FALSE;
+      int iDec = 0;
 
       fNeg = *szSource == '-';
       if( fNeg )
@@ -503,6 +504,7 @@
             mult /= 10;
             dblValue += ( ( double ) ( *szSource - '0' ) ) * mult;
             szSource++;
+            iDec++;
          }
       }
       if( *szSource == 'e' || *szSource == 'E' )
@@ -515,7 +517,6 @@
          if( fNegExp )
             szSource++;
 
-         szSource++;
          while( *szSource >= '0' && *szSource <= '9' )
          {
             iExp = iExp * 10 + *szSource - '0';
@@ -527,10 +528,12 @@
             fDbl = HB_TRUE;
          }
          dblValue *= pow( 10.0, ( double ) ( fNegExp ? -iExp : iExp ) );
+         if ( fNegExp )
+            iDec += iExp;
       }
 
       if( fDbl )
-         hb_itemPutND( pValue, fNeg ? -dblValue : dblValue );
+         hb_itemPutNDDec( pValue, fNeg ? -dblValue : dblValue, iDec );
       else
          hb_itemPutNInt( pValue, fNeg ? -nValue : nValue );
       return szSource;
--->8---

Regards.

Reply all
Reply to author
Forward
0 new messages