Can't deserialize JSON.

Created at 01 Nov 2016, 22:59
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
UC

ucpva_reg

Joined 03.09.2016

Can't deserialize JSON.
01 Nov 2016, 22:59


Hello.

I had serialized data of position in JSON, but can't deserialize it back.
This line

Position pos = JsonConvert.DeserializeObject<Position>(responseData);

return error "Could not create an instance of type cAlgo.API.Position. Type is an interface or abstract class and cannot be instantiated. Path 'SymbolCode', line 2, position 15."
That I do wrong ?
Thanks.


@ucpva_reg
Replies

firemyst
30 Dec 2023, 10:07 ( Updated at: 31 Dec 2023, 16:08 )

You're doing nothing wrong. “Position” is defined as an abstract interface in cAlgo:

https://help.ctrader.com/ctrader-automate/references/Trading/Positions/Position/

In the process of Deserialization, it involves creating an instance, and you can't create instances of interfaces.

Here's a blog article with a long way around the issue if you really need to do that:

https://dotnetfalcon.com/deserialize-into-interface/

 


@firemyst