#1 2015-11-25 04:45:53

avista
Member
Registered: 2014-01-06
Posts: 63

Exception when converting record with extended type to JSON

I'm getting an exception when trying to serialize a record with an extended type to JSON:

type
  TMyRecord = packed record
    Value: Extended;
  end;

var
  MyRec: TMyRecord;
  json: RawUTF8;
begin
  MyRec.Value := 12.34;
  json := RecordSaveJSON(MyRec, TypeInfo(TMyRecord));
end.

Exception: TJSONCustomParserRTTI.CreateFromRTTI("EXTENDED")

This occurs in SynCommons.pas in class function TJSONCustomParserRTTI.CreateFromRTTI():

class function TJSONCustomParserRTTI.CreateFromRTTI(
  const PropertyName: RawUTF8; Info: pointer; ItemSize: integer): TJSONCustomParserRTTI;
var Item: PDynArrayTypeInfo absolute Info;
    ItemType: TJSONCustomParserRTTIType;
    ItemTypeName: RawUTF8;
    ndx: integer;
begin
  if Item=nil then // no RTTI -> stored as hexa string
    result := TJSONCustomParserCustomSimple.CreateFixedArray(PropertyName,ItemSize) else begin
    ItemType := TypeNameToSimpleRTTIType(PUTF8Char(@Item.NameLen)+1,Item.NameLen,ItemTypeName);
    if ItemType=ptCustom then
      ItemType := TypeInfoToSimpleRTTIType(Info,ItemSize);
    if ItemType=ptCustom then
      if Item^.kind in [tkEnumeration,tkArray,tkDynArray] then
        result := TJSONCustomParserCustomSimple.Create(
          PropertyName,ItemTypeName,Item) else begin
        ndx := GlobalJSONCustomParsers.RecordSearch(Item);
        if ndx<0 then
          ndx := GlobalJSONCustomParsers.RecordSearch(ItemTypeName);
        if ndx<0 then
          raise ESynException.CreateUTF8('%.CreateFromRTTI("%")',  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            [self,ItemTypeName]);
        result := TJSONCustomParserCustomRecord.Create(PropertyName,ndx);
      end else
      result := TJSONCustomParserRTTI.Create(PropertyName,ItemType);
  end;
  if ItemSize<>0 then
    result.fDataSize := ItemSize;
end;

We need the extended precision, because the number stored in the database has a precision that requires the extended type: SQL Server: decimal(21, 10)

Thanks!

Offline

#2 2015-11-29 09:02:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Exception when converting record with extended type to JSON

Please try http://synopse.info/fossil/info/a0bc6fe136

I've included extended support for record JSON serialization.
But not fully tested yet.

Feedback is welcome!

Offline

#3 2015-12-01 05:12:16

avista
Member
Registered: 2014-01-06
Posts: 63

Re: Exception when converting record with extended type to JSON

Thanks!

Offline

Board footer

Powered by FluxBB