CAlgo objects failing to serialize via ToJson

Created at 14 Dec 2024, 18: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!
CT

ctid402288

Joined 06.02.2016

CAlgo objects failing to serialize via ToJson
14 Dec 2024, 18:59


Line of code

Print("[OnStart] Account \n: " + Account.ToJSON());

Error message 

14/12/2024 15:40:13.003 | Info | ******************************************************
14/12/2024 15:40:13.066 | Info | [OnStart] {FormatException} Exception : Input string was not in a correct format. Failure to parse near offset 22. Expected an ASCII digit.

CTrader Desktop

OS - MACOS Apple Silicon M2

APP: Ctrader.Automate Version 1.0.9

Desktop Version - 5.0.22


@ctid402288
Replies

EDG777
14 Dec 2024, 19:34 ( Updated at: 14 Dec 2024, 19:38 )

I think the error is due to the presence of curly braces that the formatting system interprets as placeholders. To fix it, escape your braces or ensure the string passed to the logging/printing function doesn’t contain unintended formatting tokens.
Maybe it will help:

var jsonString = Account.ToJSON();
jsonString = jsonString.Replace("{", "{{").Replace("}", "}}");
Print("[OnStart] Account:\n" + jsonString);

@EDG777

ctid402288
15 Dec 2024, 08:41

RE: CAlgo objects failing to serialize via ToJson

EDG777 said: 

I think the error is due to the presence of curly braces that the formatting system interprets as placeholders. To fix it, escape your braces or ensure the string passed to the logging/printing function doesn’t contain unintended formatting tokens.
Maybe it will help:

var jsonString = Account.ToJSON();jsonString = jsonString.Replace("{", "{{").Replace("}", "}}");Print("[OnStart] Account:\n" + jsonString);

Thank you for the tip. It worked fine for the serialising case. But l am finding deserializing the resulting json string back to the object errors with the following:-

15/12/2024 08:35:50.035 | Info | [OnStart] {JsonReaderException} Exception : Invalid property identifier character: {. Path '', line 1, position 1.

This error occurs for both Newtonsoft.json and system.text.json


@ctid402288

ctid402288
16 Dec 2024, 10:50 ( Updated at: 16 Dec 2024, 10:52 )

RE: RE: CAlgo objects failing to serialize via ToJson

ctid402288 said: 

EDG777 said: 

I think the error is due to the presence of curly braces that the formatting system interprets as placeholders. To fix it, escape your braces or ensure the string passed to the logging/printing function doesn’t contain unintended formatting tokens.
Maybe it will help:

var jsonString = Account.ToJSON();jsonString = jsonString.Replace("{", "{{").Replace("}", "}}");Print("[OnStart] Account:\n" + jsonString);

Thank you for the tip. It worked fine for the serialising case. But l am finding deserializing the resulting json string back to the object errors with the following:-

15/12/2024 08:35:50.035 | Info | [OnStart] {JsonReaderException} Exception : Invalid property identifier character: {. Path '', line 1, position 1.

This error occurs for both Newtonsoft.json and system.text.json

I finally got this to work. It turns out that the string from ToJSON is not a valid Json. To make it a valid json you also have to convert all double quotes in the string into single quotes as below:-

jsonString = jsonString.Replace("{", "{{").Replace("}", "}}").Replace("\"", "'");

This would transform the string into a valid json which can be deserialized or sent over http. [Always a good etiquette to provide the solution so other can benefit from it]


@ctid402288