Oodle Json serialization Error converting value “j”

If you are using the Oodle .NET API library – OodleRestApi then you may have encountered an exception that looks like this:

First Exception: Error converting value "j" to type 'Love2Trade.OodleRestApi.Json.OodleResult+ImageData+ImageSizeData'.
Newtonsoft.Json.JsonSerializationException: Error converting value "j" to type 'Love2Trade.OodleRestApi.Json.OodleResult+ImageData+ImageSizeData'. ---> System.Exception: Could not cast or convert from System.String to Love2Trade.OodleRestApi.Json.OodleResult+ImageData+ImageSizeData.
at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Utilities\ConvertUtils.cs:line 460
at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Utilities\ConvertUtils.cs:line 385
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(Object value, Type targetType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 464

I have updated the library and it’s now fixed. You can download it here

Oodle .NET API library – OodleRestApi

So I recently started to focus my effort to switch over from the legacy xml interface with oodle to their new REST-based web service for love2trade.com since it promises to be 20 times faster!   I spent several hours trying to put this library together which should help others as well. Many thanks for Steve Baker from oodle in helping me get some issues sorted out.

We will make this library open source at some point in the near future. For now you are free to use the library & if you really want the code, shoot me an email!

OodleRestApi.NET is a free library that I have developed in my personal time. I would really appreciate your feedback and support for OodleRestApi and its future development.
Getting Started:
1. Download OodleRestApi dll from here (library is compiled in .NET 4.0)
Or get the OodleRestApi .NET 2.0 build from here
2.  In your project, add reference to the OodleRestApi library Love2Trade.OodleRestApi.dll
3. OodleRestApi uses  JSON.NET for deserializing oodle’s response. You will need to download the latests from http://json.codeplex.com/. After that you will need to add reference to Newtonsoft.Json.dll in your project
4. Now you are ready to roll!! The quickest way to learn is to show you a sample client.

//Create the OodleRequest object that will be serialized to the server
OodleRequest oodleRequest = new OodleRequest("TEST");
oodleRequest.Filter.location.Region = Regions.RegionTypes._usa;
oodleRequest.Paging.Start = 1;
oodleRequest.Paging.Num = 50;
oodleRequest.Filter.refinements = Filter.refinementsTypes.none;
oodleRequest.Paging.Sort = Paging.SortKey.ctime;
oodleRequest.Filter.attributes.AddPriceRange(5000, 6000);
oodleRequest.category = "vehicle";
oodleRequest.q = "honda civic";

//Creating the webrequest object - Notice that the oodleRequest is simply a string representation of your query which you can print to the screen for troubleshooting
WebRequest webRequest = WebRequest.Create(oodleRequest.ToString());
using (WebResponse response = webRequest.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        //this will get the JSON result back from oodle
        string output = reader.ReadToEnd();
        //We do a minor alteration in the json string due to a problem oodle is facing with php's representation of objects as arrays
        output = output.Replace("\"location\":[]", "\"location\":{}").Replace("\"user\":[]", "\"user\":{}");
        OodleResult Result = JsonConvert.DeserializeObject<OodleResult>(output);
        //Now you can whatever you want with the results, you can start by iterating through Result.Listings to view all the listings your query returned
    }
}

You will notice that both the OodleRequest & OodleResponse objects represents a very similar structure to the structure defined by oodle in Oodle Api parameters

The Oodle Response is broken down to 5 main parts

  • Current – This contains information about the query that is returned, (ex. Region, Category, Start item #, Num or items returned).
  • Listings – This is a strong typed array of Listings. The maximum you will ever find is 50 items as defined by oodle
  • Meta – Metadata details on the results available. How many total items available for your search query…etc. This is very important if you are implementing paging in your result set.
  • Refinements – by default this is null unless you have requested refinements in your oodle request (ex. oodleRequest.Filter.refinements=none | simple | full)
  • Stat – should always be “ok” if everything runs good.

Everything else is really self explanatory and you can depend on your IDE’s intellisense to guide you. Feel free to comment on this post if you have any Qs.

Let me know your feedback!! :)

Support OodleRestApi
We appreciate your support for OodleRestApi

Simple Regex to Remove Html tags

This is one of the simplest regex to remove html tags from some html text. I know its not the best but i’d argue that it’s one of the simplest. ;)


public static string RemoveHtml(string txt)
{
    return Regex.Replace(txt, @"<[^>]*>", "");
}

Formatting .NET DateTime to display Day suffix (st, nd ,rd , th)

I find it intersting that the MS did not have this by default as part of the Standard DateTime Format Strings for .NET.

So I did my own thing..
(more…)

Backing-up www.love2trade.com SQLServer DB 2005 from ASP.NET

Hi guys,

This is the code you would use to trigger a full sql 2005 backup to a .BK file from a ASPX page, i use that on www.Love2Trade.com 
(more…)

Get notified when your website throws an exception.

I used this code below in my website http://www.love2trade.com.

This will send an email whenever an exception is thrown in your asp.net website, there is a 15 minutes throttle on the frequency of emails.

In Application Start Class

private static int _exceptionCount;
private static DateTime _timeofLastException;
private static object _timeofLastExceptionLock = new object();
private static object _exceptionCountLock = new object();public static DateTime TimeofLastException
{
get { return _timeofLastException; }set { lock(_timeofLastExceptionLock){_timeofLastException = value;} }
}public static int ExceptionCount
{
get { return _exceptionCount; }set { lock(_exceptionCountLock){_exceptionCount = value;} }
}

In Global.asax

void Application_Error(object sender, EventArgs e){
Love2TradeHttpApplication.ExceptionCount++;
if(DateTime.Now > Love2TradeHttpApplication.TimeofLastException.Add(new TimeSpan(0,15,0)))
{
try
{
MailMessage mail = new MailMessage("errors@love2trade.com", "test@example.com");
string ErrorMessage = Love2TradeHttpApplication.ExceptionCount+
 " Exceptions were thrown since the last error email.rnThe error description is as follows : " + Server.GetLastError();
mail.Subject = "Exception thrown in love2trade.com";
mail.Priority = MailPriority.High;
mail.BodyEncoding = Encoding.ASCII;
mail.Body = ErrorMessage;
SmtpClient client = new SmtpClient();// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;client.Send(mail);
Love2TradeHttpApplication.TimeofLastException=DateTime.Now;
Love2TradeHttpApplication.ExceptionCount=0;
}
catch (Exception){
//donothing
}
}
}

Hope some will find this useful.-Ash

Finding Distance between two points using longitude and latitude

This is a method that calulates a distance between two points. Let me know if you have a more acurate formula. (more…)

Follow

Get every new post delivered to your Inbox.