eredo / dartson

Dartson is a Dart library that can be used to convert Dart objects into a JSON string.
MIT License
79 stars 29 forks source link

Using double in num properties causing Stack Overflow #27

Closed eredo closed 6 years ago

eredo commented 9 years ago

As mentioned in #26, the following code will fail:

import 'package:dartson/dartson.dart';

@Entity
class Example {
  num takeDouble;
}

void main() {
  var dson = new Dartson.JSON();
  var jsonStr = dson.encode(new Example()..takeDouble = 1.11);

  // this will work
  var jsonStr2 = dson.encode(new Example()..takeDouble = 1);
}
rightisleft commented 9 years ago

I'm seeing this on 0.2.5 as well

rightisleft commented 9 years ago

the work around in #26 of using double also results in a stack overflow

  double ccv;
  double bZip;
FINER: 2015-10-31 15:45:25.734: Serializing field: isInfinite
FINER: 2015-10-31 15:45:25.734: Start serializing field: isNaN
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: isNaN
FINER: 2015-10-31 15:45:25.734: Start serializing field: isFinite
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: isFinite
FINER: 2015-10-31 15:45:25.734: Start serializing field: sign
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: sign
FINER: 2015-10-31 15:45:25.734: Start serializing field: runtimeType
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: runtimeType
FINER: 2015-10-31 15:45:25.734: Serialization completed.
FINER: 2015-10-31 15:45:25.734: Start serializing field: isNegative
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: isNegative
FINER: 2015-10-31 15:45:25.734: Start serializing field: isInfinite
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: isInfinite
FINER: 2015-10-31 15:45:25.734: Start serializing field: isNaN
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: isNaN
FINER: 2015-10-31 15:45:25.734: Start serializing field: isFinite
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: isFinite
FINER: 2015-10-31 15:45:25.734: Start serializing field: sign
FINER: 2015-10-31 15:45:25.734: Property: null
FINER: 2015-10-31 15:45:25.734: Serializing field: sign
Unhandled exception:
Uncaught Error: Stack Overflow
Stack Trace:
#0      DefaultSegmentRouter.handle.<handle_async_body> (package:shelf_route/src/default_segment_router.dart)
#1      _asyncCatchHelper.<anonymous closure> (dart:core-patch/core_patch.dart:14)
#2      _rootRunBinary (dart:async/zone.dart:923)
#3      _CustomZone.runBinary (dart:async/zone.dart:819)
#4      _Future._propagateToListeners.handleError (dart:async/future_impl.dart:521)
#5      _Future._propagateToListeners (dart:async/future_impl.dart:580)
#6      _Future._completeWithValue (dart:async/future_impl.dart:368)
#7      _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:422)
#8      _rootRun (dart:async/zone.dart:904)
#9      _CustomZone.run (dart:async/zone.dart:803)
#10     _CustomZone.runGuarded (dart:async/zone.dart:709)
#11     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:734)
#12     _microtaskLoop (dart:async/schedule_microtask.dart:43)
#13     _microtaskLoopEntry (dart:async/schedule_microtask.dart:52)
#14     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:96)
#15     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:149)
#16     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:96)
#17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:149)

#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:895)
#1      _microtaskLoop (dart:async/schedule_microtask.dart:43)
#2      _microtaskLoopEntry (dart:async/schedule_microtask.dart:52)
#3      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:96)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:149)

Process finished with exit code 255
rightisleft commented 9 years ago

This is kind of problematic. Angular 2 accepts input types as either text or numbers. Because All numbers in JavaScript are floating point, i'm getting only floating point numbers back as a response.

rightisleft commented 9 years ago

I also just pulled down HEAD and it seems like the unit tests are not passing:

00:00 +11 -1: parse: parser simple Cannot instantiate abstract class double: _url 'null' line null dart:mirrors _LocalClassMirror.newInstance lib/dartson.dart 320:25 Dartson._initiateClass lib/dartson.dart 227:17 Dartson._convertValue lib/dartson.dart 189:15 Dartson._fillObject. dart:collection MapView.forEach lib/dartson.dart 160:30 Dartson._fillObject lib/dartson.dart 62:7 Dartson.map lib/dartson.dart 85:12 Dartson.decode test/dartson_test.dart 87:28 main.

rightisleft commented 9 years ago

I updated the PR to also include a fix for an infinite loop caused by trying to serialize a double

https://github.com/eredo/dartson/pull/32

mnordine commented 8 years ago

Any update on this? Still seeing bug with 0.2.5

eredo commented 6 years ago

Closed in 1.0.0-alpha