Closed laogao closed 1 year ago
This issue can be circumvented by supplying a custom encoder to jsonEncode
:
jsonEncode(v, toEncodable: (nonEncodable) => encodeDateTime(nonEncodable));
where encodeDateTime
is defined as:
dynamic encodeDateTime(dynamic item) {
if (item is DateTime) {
return item.toIso8601String();
}
return item;
}
This would give us something like:
{"102 visit date":"2023-06-07T00:00:00.000"}
Ideally, we could do better at library level by storing the value in String
in the first place (as surveyjs.io
apparently does) and according to its inputType
(in this particular case, a date
without the time
part).
It's a very common dart issue. Should keep it as DateTime
for further calculation like expression
It's a very common dart issue. Should keep it as
DateTime
for further calculation likeexpression
I understand the thinking behind this. That said, since DateTime
s and String
s are inter-convertible, I see three reasons why we might prefer an underlying string representation (and accept the inconvenience / overhead of converting the values back and forth for calculation):
surveyjs.io
treats date
inputs for text fields.surveyjs.io
before submitting. Within the answer value itself, there's nothing indicating clearly about the type
and inputType
of the survey question. (In our case it's a date, not a timestamp.) He/she'll have to look into the survey definition to find out.surveyjs.io
. The value will come in string format and somehow needs to be converted back to DateTime
, or not. It's usually hard to tell.
Describe the bug When submitting a survey with dates, the answer is currently not
jsonEncode
-able. Instead, the following error will be thrown:To Reproduce SurveyJs json
SurveyJs answer
Expected behavior The answer is properly encoded in JSON:
Here's how
surveyjs.io
handles the same input:Screenshots
Desktop (please complete the following information):