joaorobertoct2 / x-superobject

Automatically exported from code.google.com/p/x-superobject
0 stars 0 forks source link

ISO 8601 Zulu time problem #40

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
A API I need to deal with sends dates in ISO 8601 Zulu time.
But x-superobject throws a exception when it sees a Zulu time.
Example:

  TMyObject = class
    public
      someDate: TDateTime;
  end;

  json := '{someDate: "2014-03-30T22:00:00Z"}';
  obj := TMyObject.FromJSON(json);

What is the expected output? What do you see instead?
I see a exception of type 'EVariantTypeCastError',
with message: 'Could not convert variant of type (Unicode) into type (Double)'.

What version of the product are you using? On what operating system?
Delphi XE3. Newest version of x-superobject at this date.

Please provide any additional information below.
Today I updated to the newest version of x-superobject and this started to 
happen today. The version I used before does not have this problem I think.

Original issue reported on code.google.com by Rein.Ver...@gmail.com on 15 Aug 2014 at 4:09

GoogleCodeExporter commented 9 years ago
I looked into the source code of x-superobject to see if I could fix the 
problem.
I isolated the handling of the 'TDateTime' type so that the date string gets 
parsed in a different way.
To parse the ISO 8601 date I used a unit found here: 
http://wiert.me/2011/07/19/iso-8601-delphi-way-to-convert-xml-date-and-time-to-t
datetime-and-back-via-stack-overflow/.
I dont know if this fix is acceptable for x-superobject, it introduces a 
dependency with a (small) unit.
And I dont know if this change has any unknown side effects.

I attached a .rar archive that contains my full code, it contains a form with a 
button that deserializes a object from JSON with a Zulu date time and shows 
that the date gets parsed correctly.

This is what the change looks like in 'XSuperObject.pas' around line 1880:

  if (RType = TypeInfo(TDate)) or (RType = TypeInfo(TTime)) then begin
      SetValue<Typ>(Data, MemberValue, Member, TValue.From<TDateTime>(IJSONData.Ancestor[Member].AsVariant));
  end
  else if (RType = TypeInfo(TDateTime)) then begin
      SetValue<Typ>(
        Data,
        MemberValue,
        Member,
        TValue.From<TDateTime>(
          TIso8601.DateTimeFromIso8601(
            IJSONData.Ancestor[Member].AsVariant
          )
        )
      );
  end
  // Continues with a switch

Original comment by Rein.Ver...@gmail.com on 15 Aug 2014 at 8:40

Attachments:

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r72.

Original comment by onryld...@gmail.com on 18 Aug 2014 at 1:27