#1 2017-03-24 13:04:23

Del
Member
Registered: 2017-03-24
Posts: 20

Consume non-Mormot REST service JSON to collection of objects

I would like to consume the following JSON result from a REST service into a collection of Category objects. What would be the correct way to go about configuring Mormot to do just that?

{
  "Categories": [
    {
      "CategoryId": 0,
      "CategoryName": "Undefined",
	  "LastModified": "2016-01-15 08:00",
	  "Isactive": 1
    },
    {
      "UnitCategoryId": 1,
      "CategoryName": "Acceleration",	  
	  "LastModified": "2016-01-15 08:00",
	  "Isactive": 1
    },
    {
      "UnitCategoryId": 2,
      "CategoryName": "Angle",	  
	  "LastModified": "2016-01-15 08:00",
	  "Isactive": 1
    },
    {
      "UnitCategoryId": 3,
      "CategoryName": "Area",	  
	  "LastModified": "2016-01-15 08:00",
	  "Isactive": 1
    }    
  ]
}

I tried the following, (a bit dirty as I have been trying to sort this)

TUOM = class(TPersistent)
  protected
    fuomcid: string;
    fuomcn: string;
  published
    property CategoryId: string read fuomcid write fuomcid;
    property CategoryName: string read fuomcn write fuomcn;
  end;

function TMUOMCategoryClient_REST1.Get: Boolean;
var
  fuom: TUOM;
  v: Boolean;
  ll: TList;
  l: TObjectList<TUOM>;
  res, uomc: string;
  cnt: integer;
  cp: TJSONCustomParserCustom;
  r: RawUTF8;
begin

 // cp := TJSONCustomParserCustom.Create('UOMCategoryId', 'CategoryId');


  fd := fclient.Get('http://localhost:3002/uomc', chdr);
  r := RawUTF8(fd); //valid json here
  l := TObjectList<TUOM>.Create(True);
  Res := JSONToObject(l, @r[1], v, TUOM, []); //object list contains nothing
  cnt := l.Count;
end;

Any assistance appreciated.

Last edited by Del (2017-03-24 14:41:03)

Offline

#2 2017-03-24 15:36:02

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

Re: Consume non-Mormot REST service JSON to collection of objects

Two main issues:
- Is it CategoryId or UnitCategoryId ?
- There is a "Categories" object containing the list.

Offline

#3 2017-03-24 19:40:59

Del
Member
Registered: 2017-03-24
Posts: 20

Re: Consume non-Mormot REST service JSON to collection of objects

Thanks will resolve name for categoryid. I want to have my object's property names deviate from the rest response fields
How do I 'map' this? A simple example would be appreciated. Many thanks

Offline

#4 2017-03-25 11:00:33

Del
Member
Registered: 2017-03-24
Posts: 20

Re: Consume non-Mormot REST service JSON to collection of objects

Sorted this, had a couple of issues besides for what was mentioned (JSON issues) ... main one being using TObjectlist<> which is not supported. For custom mapping from JSON to object
TJSONSerializer.RegisterCustomSerializer specifying a reader function should be used, correct? Are there any samples available outlining use of such a scenario?

Thanks for an AWESOME Framework!:)

Offline

Board footer

Powered by FluxBB