#1 2015-10-08 22:36:53

Andy23
Member
Registered: 2015-06-20
Posts: 8

Parsing a Array of Objects

In the following JSON Example we can See "PVList" which is a TObject-Class (List) and i built a custom serializer to write this JSON. The Object of "PVList" Contains a List of other Objects.

Writing see serializer for writing the JSON was not a problem.

{
	"result": {
		"PVList": [{
			"Bezeichnung": "Projekt 1"
		},
		{
			"Bezeichnung": "Projekt 2"
		},
		{
			"Bezeichnung": "Projekt 3"
		},
		{
			"Bezeichnung": "Projekt 4"
		}],
		"Result": {
			"Code": 0,
			"Msg": "",
			"Desc": ""
		}
	}
}

Now i try to develop the custom-reader. Currently i have no idea how to do this.
If my custom Reader Method is called:

class function TMormotSerializer.Reader(const aValue: TObject; aFrom: PUTF8Char; var aValid: Boolean; aOptions: TJSONToObjectOptions): PUTF8Char;

then "aFrom" points to this JSON:

               [{
			"Bezeichnung": "Projekt 1"
		},
		{
			"Bezeichnung": "Projekt 2"
		},
		{
			"Bezeichnung": "Projekt 3"
		},
		{
			"Bezeichnung": "Projekt 4"
		}],
		"Result": {
			"Code": 0,
			"Msg": "",
			"Desc": ""
		}
	}
}

I know exactly the Objects which are contained in "PVList". They are all the same objects.

So my question is:
How to parse the Array Element and create Objects from all the Items. I want to use JsonToObject() or something because i have a lot of similar Data. It is also possible that my "child-object" contains another "Object" (e.g. AnotherPVList) for which i have also a custom reader/writer.

As i mentioned: The writer works perfect and i get my desired JSON result! But how to develop the reader...

Offline

#2 2015-10-08 23:18:03

Andy23
Member
Registered: 2015-06-20
Posts: 8

Re: Parsing a Array of Objects

Oh... it seems to be easier then expected. I searched for hours and now the solution is so simple:

class function TMormotSerializer.Reader(const aValue: TObject; aFrom: PUTF8Char; var aValid: Boolean; aOptions: TJSONToObjectOptions): PUTF8Char;
var
  Wrapper: IApiDataListObjectWrapper;
  NewObject: TObject;
begin
  Result := '';

  if Assigned(aValue) then
  begin
    if aValue is TApiListBase then
    begin
      Wrapper := TApiListBase(AValue).Wrapper;
      if Assigned(Wrapper) then
      begin
        TMormotSerializer.IgnoreChar(aFrom,'[');

        while aFrom^ <> ']' do
        begin
          NewObject := Wrapper.CreateNew;
          aFrom := JSONToObject(NewObject,aFrom,aValid);

          if aValid then
            Wrapper.AddObject(NewObject);

          TMormotSerializer.IgnoreChar(aFrom,',');
        end;

        TMormotSerializer.IgnoreChar(aFrom,']');
      end;
    end;
  end;

  Result := aFrom;

For Explanation for my code:
I created some classes so i am able to use TList<T> for submitting Lists over my REST API. All i had to do is a custom-writer and a custom-reader (see above). Is is very flexible so i can define Lists what ever i want. I just have to make sure T is a kind of TObject.

If you (admin) are interested in. I can send you my units to show you what i have created.

Very good framework! I like it!

Last edited by Andy23 (2015-10-08 23:18:35)

Offline

Board footer

Powered by FluxBB