#1 2014-02-11 11:15:14

Celso
Member
Registered: 2013-05-14
Posts: 55

Class to JSON

I have my class :

TIndex = class ( TRemotable )
  private
    fField : string ;
    fCond : string ;
    FVal : WideString ;
  published
    property Afield : String read write fField fField / / Field Name
    ACOND property : String read write fCond fCond / / Condition
    Aval property : WideString read FVal FVal write / / Value (s )
  end ;

  TIndexStructure = array of TIndex ;

.....

code to initialize :

var
   v_index : TIndexStructure ;
   v_count : Integer;
begin
   try
      SetLength ( v_index , cdsIndex.RecordCount ) ;
      cdsIndex.First ;
      v_count : = 0;
      while not the cdsIndex.Eof
      begin
         v_index [ v_count ] : = tIndex.Create ;
         . v_index [ v_count ] Afield : cdsIndex.FieldByName = ( ' AINDEX ' ) Value; .
         . v_index [ v_count ] ACOND : cdsIndex.FieldByName = ( ' ACOND ' ) AsString ; [1] .
         . v_index [ v_count ] Surety : cdsIndex.FieldByName = ( ' AVAL ' ) Value; .
         Inc ( v_count , 1);
         cdsIndex.Next ;
      end ;
   except
   ...
   end ;
end ;

question :
HOW DO I TURN THIS IN CLASS JSON ?

Offline

#2 2014-02-11 16:06:26

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

Re: Class to JSON

Did you try the ObjectToJSON() function in mORMot.pas?

Note that all your code is not at all mORMot-based.
This TRemotable class is not handled at all by the framework.
But ObjectToJSON() is able to serialize any class with published properties.

Offline

#3 2014-02-12 10:09:51

Celso
Member
Registered: 2013-05-14
Posts: 55

Re: Class to JSON

ok.
I am developing a webservice (dll).

I changed my code:

TIndex= class(TPersistent)
  private
    fField:string;
    fCond:string;
    fVal:WideString;
  published
    property AField:string read fField  write fField; //Nome do campo
    property ACond:string read fCond write fCond;     //Condicao
    property AVal:WideString read fVal write fVal;    //Valor(es)
  end;

  TIndexStructure = array of TIndex;

  TInsReg = class(TPersistent)
  private
    fIdent   : RawUTF8;
    fIdSetup : RawUTF8;
    fObs     : TSQLRawBlob;
    fPriority: Integer;
    fRefCond : RawUTF8;
    fRefVal  : RawUTF8;
    FIndex   : TIndexStructure;
  published
    property AIdent   : RawUTF8 read fIdent write fIdent;
    property AIdSetup : RawUTF8 read fIdSetup write fIdSetup;
    property AObs     : TSQLRawBlob read fObs write fObs;
    property APriority: Integer read fPriority write fPriority;
    property ARefCond : RawUTF8 read fRefCond write fRefCond;
    property ARefVal  : RawUTF8 read fRefVal write fRefVal;
    property AIndex   : TIndexStructure read FIndex write FIndex;
  end;

------

I need to send (client side) and receive (webservice side) class TInsReg.

I thought about doing this with the function call:
fn_InsReq function (AInsRegSend: RawJSON): RawUTF8;

-------------------

I already do it by delphi but I'm wanting to use mormot.

In your opinion, how should I proceed for this?

How should I serialize (JSON), the client side, and to send and receive in webservice side and unserialize?

Thanks foe your hep.

Offline

#4 2014-02-12 12:17:16

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

Re: Class to JSON

Dynamic array of classes is not handled by the RTTI, nor mORMot.
Use a dynamic array of records instead.

Then call ObjectToJSON() and JSONToObject() from mORMot.pas.

Ensure you took a look at record serialization.
Could be even easier than classes in your case.
See http://blog.synopse.info/post/2013/12/1 … ialization and the "Text-based definition" paragraph.

Offline

#5 2014-02-12 23:43:06

Celso
Member
Registered: 2013-05-14
Posts: 55

Re: Class to JSON

Wonder. Thanks for the tips. See how it fits:

type
  TInsReg = packed record
    fIdent   : RawUTF8;
    fIdSetup : RawUTF8;
    fObs     : TSQLRawBlob;
    fPriority: Integer;
    fRefCond : RawUTF8;
    fRefVal  : RawUTF8;
    fIndex   : array of record
       fField: RawUTF8;
       fCond : RawUTF8;
       fVal  : TSQLRawBlob;
    end;
  end;

const
   __TInsReg = 'fIdent RawUTF8 fIdSetup RawUTF8 fObs RawUTF8 fPriority Integer fRefCond RawUTF8 fRefVal RawUTF8 fIndex[fField RawUTF8 fCond RawUTF8 fVal RawUTF8]';

   TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TInsReg),__TInsReg);

..............

It worked perfectly. My last question in this context. How do I define the TSQLRawBlob const __ TInsReg?

When I set so I get an error that is TSQLRawBlob not recognized.

   __TInsReg = 'fIdent RawUTF8 fIdSetup RawUTF8 fObs TSQLRawBlob fPriority Integer fRefCond RawUTF8 fRefVal TSQLRawBlob fIndex[fField RawUTF8 fCond RawUTF8 fVal TSQLRawBlob]';

Offline

#6 2014-02-13 13:40:41

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

Re: Class to JSON

Oups... this is missing...

I've just added RawByteString support for text-based record serialization, using Base-64 encoding.
See http://synopse.info/fossil/info/5dc231900e

Offline

#7 2014-02-14 11:38:27

Celso
Member
Registered: 2013-05-14
Posts: 55

Re: Class to JSON

Thank you very much.

Now, one more question. In my webservice (dll) I can use, or has the logs?

If yes, tell me how to do.

Offline

#8 2014-02-14 13:14:32

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

Re: Class to JSON

Take a look at the samples.

In short, you can enable logging via:

  TSQLLog.Family.Level := LOG_VERBOSE;

Then you can tune the logging process by setting TSQLLog.Family properties.

Offline

Board footer

Powered by FluxBB