Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Custom serialization of TCollection
Mon, Jul 29 2019 11:12 AMPermanent Link

Mario Enríquez

Open Consult

Hello everybody,

I've been successfully working the JSON serialize funtionality on TCollection/TCollectionItem classes. I've using it to exchange data with the backend service back and forward.. until now. Frown

There is a different and "improve" JSON library in the backend that is having trouble deserializing some "JSON" data that comes from EWB.

The thing is that EWB TCollection serialize its content in the following format:

{ "Count": 2, Items:"[ {"id":1, "value":"test1"}, {"id":2, "value":"test2"} ]" }  

But the backend requieres the collections to be submited as if the were JSON Arrays, like this:
{[{ "Count": 2, Items:"[ {"id":1, "value":"test1"}, {"id":2, "value":"test2"} ]" }  ]}

Any tips on how to achieve this?

Regards,
Mario
Mon, Jul 29 2019 11:32 AMPermanent Link

Matthew Jones

Mario Enríquez wrote:

> {[{ "Count": 2, Items:"[ {"id":1, "value":"test1"}, {"id":2, "value":"test2"} ]" }  ]}

Neither of those are valid JSON as far as I can see. Item names are supposed to have quotes. At least, they always have when I've seen JSON!

I'd say you could fiddle with the JSON writing code to add an extra set of brackets in, but I'd also question the Count value you have there. Is that actually a member of the object? Why is it needed?

Anyway, this would be my suggestion, based on my usual serialising code:


function TMyObject.GetJSON : String;
var               
   xWriter : TWriter;
begin
   xWriter := TWriter.Create(dtfISO8601);
   try
       xWriter.Initialize;
   
       xWriter.BeginArray; // extra

       Save(xWriter);

       xWriter.EndArray; // extra
   
       Result := xWriter.Output;
   finally
       xWriter.Free;
   end;
end;

--

Matthew Jones
Mon, Jul 29 2019 11:52 AMPermanent Link

Raul

Globestar Systems

Team Elevate Team Elevate

On 7/29/2019 11:12 AM, Mario Enr�quez wrote:

> But the backend requieres the collections to be submited as if the were JSON Arrays, like this:
> {[{ "Count": 2, Items:"[ {"id":1, "value":"test1"}, {"id":2, "value":"test2"} ]" }  ]}
>

Is this what it really expects !?

This is not a valid json.

Raul
Mon, Jul 29 2019 12:38 PMPermanent Link

Mario Enríquez

Open Consult

Sorry folks, I just wrote that JSON string as an example. Both "libraries" are producing valid json.

It is just that EWB does a proper job of representig a TCollection, adding the Count and Items properties. But the backend is getting "confuse" about those properties as it is expecting an array representation, with just the values set enclosed in brackets.

Sorry for the bad example, I'll try to better explain my problem in a moment.

Thank you for taking the time to review my request.

Regards,
Mario
Image