stalek
12 Feb 2010, 5:07 AM
I use Direct.NET and on client side I very often use DirectFn instead of API configuration object with read method (in such case the params are encoded as an array, so they are embeded in [] brackets). In such case everything works ok.
When I started using API configuration object (DirectProxy) to implement full CRUD, the params started encoding without [] brackets so as a naked object with {} brackets. This way Newtonsoft.Json is trying to decode simple object as a list/array and throws en exception (version 3.5 of Newtosoft.Json). Earlier version of Newtonsoft.Json just returns empty Object as a result of deserialization (also incorrect behavior). To correct this strange behavior I did something like below (file DirectProcessor.cs, method Execute, line 44 - I corrected else clause and added try/catch clause):
//...
else
{
UTF8Encoding encoding = new UTF8Encoding();
string json = encoding.GetString(httpRequest.BinaryRead(httpRequest.TotalBytes));
List<DirectRequest> requests = new List<DirectRequest>();
try
{
requests = JsonConvert.DeserializeObject<List<DirectRequest>>(json);
}
catch
{
}
if (requests.Count > 0)
{
foreach (DirectRequest request in requests)
{
responses.Add(DirectProcessor.ProcessRequest(provider, request));
}
}
else
{
DirectRequest req = JsonConvert.DeserializeObject<DirectRequest>(json);
responses.Add(DirectProcessor.ProcessRequest(provider, req));
}
}
Maybe there is some configuration value to embed params for read method on DirectProxy to encode the params as an array with [] again?
I hope it helps if you encounter problems similar to mine ... :)
Alek
When I started using API configuration object (DirectProxy) to implement full CRUD, the params started encoding without [] brackets so as a naked object with {} brackets. This way Newtonsoft.Json is trying to decode simple object as a list/array and throws en exception (version 3.5 of Newtosoft.Json). Earlier version of Newtonsoft.Json just returns empty Object as a result of deserialization (also incorrect behavior). To correct this strange behavior I did something like below (file DirectProcessor.cs, method Execute, line 44 - I corrected else clause and added try/catch clause):
//...
else
{
UTF8Encoding encoding = new UTF8Encoding();
string json = encoding.GetString(httpRequest.BinaryRead(httpRequest.TotalBytes));
List<DirectRequest> requests = new List<DirectRequest>();
try
{
requests = JsonConvert.DeserializeObject<List<DirectRequest>>(json);
}
catch
{
}
if (requests.Count > 0)
{
foreach (DirectRequest request in requests)
{
responses.Add(DirectProcessor.ProcessRequest(provider, request));
}
}
else
{
DirectRequest req = JsonConvert.DeserializeObject<DirectRequest>(json);
responses.Add(DirectProcessor.ProcessRequest(provider, req));
}
}
Maybe there is some configuration value to embed params for read method on DirectProxy to encode the params as an array with [] again?
I hope it helps if you encounter problems similar to mine ... :)
Alek