delphi-rest-client-api icon indicating copy to clipboard operation
delphi-rest-client-api copied to clipboard

Unmarshal to TDictionary

Open Frees opened this issue 9 years ago • 6 comments

I can't unmarshal JSON

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

into class:

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

Should it work?

Frees avatar May 22 '15 18:05 Frees

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.

RobertoSchneiders avatar May 22 '15 19:05 RobertoSchneiders

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;

thomaserlang avatar May 22 '15 21:05 thomaserlang

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

@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

fabriciocolombo avatar May 23 '15 14:05 fabriciocolombo

Ah, cool. That does indeed work.

thomaserlang avatar May 24 '15 15:05 thomaserlang

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

Timothyoverton avatar Apr 03 '18 11:04 Timothyoverton

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

TDictionaryDataObject = class
   data: ISuperObject;
end;

thomaserlang avatar Apr 03 '18 12:04 thomaserlang