How to detect of using Tlist in TjsonProxyLoader

Hi,

I'm getting an error when using Deserializer.FromJson(JsonObject, TObject);
JsonObject is a list of objects.

I check now  if JsonObject is TJSONArray and then use Result := Deserializer.FromJson(JsonObject, TList<TObject>);

Is this the right way to do?


FDeserializer.ProxyLoader := TJsonProxyLoader.Create(
      function(ProxyInfo: IProxyInfo): TObject
      var
        Serializer: TDataSnapJsonSerializer;
        Deserializer: TDataSnapJsonDeserializer;
        JsonObject: TJsonValue;
      begin
        Serializer:= TDataSnapJsonSerializer.Create;
        Deserializer := TDataSnapJsonDeserializer.Create;

        try
          JsonObject := FClient.RemoteProxyLoad(Serializer.ToJson(ProxyInfo));
          if JsonObject is TJSONArray then
          begin
            Result := Deserializer.FromJson(JsonObject, TList<TObject>);
          end
          else
          begin
            Result := Deserializer.FromJson(JsonObject, TObject);
          end;
      finally
        Deserializer.Free;
        Serializer.Free;
      end;
    end
    );




Yes, that's usually the way to do it. What is the error you get? 

The error was:

---------------------------
Debugger Exception Notification
---------------------------
Project Client.exe raised exception class EIncompatibleJsonValueType with message 'Incompatible json value. Cannot serialize to the proper type.
(Instance): Not a JSON Object nor null'.
---------------------------
Break   Continue   Help  
---------------------------

But now I check if JsonObject is a list like:

          if JsonObject is TJSONArray then
          begin
            Result := Deserializer.FromJson(JsonObject, TList<TObject>);
          end
          else
          begin
            Result := Deserializer.FromJson(JsonObject, TObject);
          end;

Thanks to know that this is the way to do it.

Your error message indicates that it's expecting a JSON object, but it's not. Are you sure your JSON Array contains objects (and not other JSON types)? You just need to check your JSON to see if it matches what the framework is expecting.

The JsonObject  can be one object or more objects.

'[{"$type":"Entities.CustomerToAddress.TCustomerToAddress1",
   "$id":1,
   "FCustomerID":1,
   "FAddressID":501,
   "FAddressTypeID":1,
   "FAddress":{"$proxy":"single",
                "key":501,
                "class":"TCustomerToAddress",
                "member":"FAddress"}}]'

or can be

'[{"$type":"Entities.CustomerToAddress.TCustomerToAddress1","$id":1,"FCustomerID":104,"FAddressID":604,"FAddressTypeID":1,"FAddress":{"$proxy":"single","key":604,"class":"TCustomerToAddress","member":"FAddress"}},{"$type":"Entities.CustomerToAddress.TCustomerToAddress3","$id":2,"FCustomerID":104,"FAddressID":605,"FAddressTypeID":3,"FAddress":{"$proxy":"single","key":605,"class":"TCustomerToAddress","member":"FAddress"}}]'