I hate to add to the 5,000+ JSON.NET deserialisation errors, but can anyone see anything that might be causing a problem in the following JSON below? Both JSONLint and JSONViewer state that the string is valid, indeed it has already been serialised by JSON.NET without any problem! The class definition for the Tariff object it represents is below too.
This has got me totally stumped – I have the exact same problem when I use .NET’s JavascriptSerialiser class too. It serialises fine, but on deserialisation it throws the following error:
Error converting value [JSON string below]
’emAPI.ClassLibrary.Tariff’. Path ”, line 1, position 374.
Line 1 pos 374 is between the 0 and the period in 0.5 at the end of the string, which represents double StandingChargeValue in the class.
Anyone any ideas on how to resolve?
Thanks, David
JSON String
{ "StandingChargePeriod": { "Id": 4, "Length": "Weekly", "NumbDays": 7 }, "Bands": [ { "Id": 24, "UpperkWhLimit": 23, "LowerkWhLimit": 0, "UnitRate": 2.4 }, { "Id": 25, "UpperkWhLimit": 0, "LowerkWhLimit": 24, "UnitRate": 5.8 }, { "Id": 26, "UpperkWhLimit": -1, "LowerkWhLimit": 0, "UnitRate": 5.8 } ], "Id": 10, "StartDate": "2012-12-07T00:00:00", "StandingChargeValue": 0.5 }
Tariff Class def
public class Tariff
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
public DateTime StartDate { get; set; }
[Required]
[Range(0, 999999999)]
public double StandingChargeValue { get; set; }
public virtual Period StandingChargePeriod { get; set; }
public virtual ICollection<TariffBand> Bands { get; set; }
}
OK so I finally worked it out after an all-night debug session…
Like this problem about WCF serialisation, the problem revolved around trying to reserialise to an EF4.3 proxy object. To solve it, instead of returning the object from EF context directly:
I created a new object, copied over the attributes and returned the new object instead:
Works like a charm. I’m not sure if this is designed behaviour or not, and it’s a bit of a pain as I need to repeat this pattern for a number of objects, but at least it works!
Hopefully this will help someone else!