fabriciocolombo / delphi-rest-client-api

A Delphi REST client API to consume REST services written in any programming language.
Apache License 2.0
380 stars 182 forks source link

Unmarshal to TDictionary #44

Open Frees opened 9 years ago

Frees commented 9 years ago

I can't unmarshal JSON

{
    "data": {
        "1": "value one",
        "2": "value two"
    }
}

into class:

TDictionaryDataObject = class
   data: TDictionary<string, string>;
end;

Should it work?

RobertoSchneiders commented 9 years ago

I don't think so.

The parser will find for fields and properties to make the mapping. I guess the TDictionary approach is not supported, but, I am not sure.

thomaserlang commented 9 years ago

It's not supported in SuperObject. Would like to see it supported. But then again, how often is it that the fields are of the same type?

Had it been:

{
    "data": {
        "1": "value one",
        "2": 1
    }
}

The TDictionary would have to be defined with a variant, so i don't know how useful it would be.

I've had this problem one time before. Had no need for changing the data so I used my own JSON parser.

It would be cool if you could specify the type as a TSuperObject:

TDictionaryDataObject = class
   data: TSuperObject;
end;
fabriciocolombo commented 9 years ago

@Frees It's currently not supported. But i think it's possible, just using the same approach as for List.

@thomaserlang I believe works using ISuperObject (the interface).It would good write a test for verify this behaviour. https://github.com/fabriciocolombo/delphi-rest-client-api/blob/master/lib/superobject/superobject.pas#L7123-L7125

thomaserlang commented 9 years ago

Ah, cool. That does indeed work.

Timothyoverton commented 6 years ago

Hi @thomaserlang can you give me an example of it working?

thomaserlang commented 6 years ago

What are you referring to @Timothyoverton? SuperObject? Just use ISuperObject instead of TSuperObject in the class definition.

TDictionaryDataObject = class
   data: ISuperObject;
end;