I am serializing the F # record type for Json using Newtonsoft.Json.FSharp.
type Prices = Dictionary<string, decimal>
type PriceList = {Id:string; Name:string; Date:DateTime; CurrencySymbol:string; Status:Status; Prices:Prices}
let private converters : JsonConverter array =
[| BigIntConverter();
GuidConverter();
ListConverter();
OptionConverter();
MapConverter();
TupleArrayConverter();
UnionConverter();
UriConverter();
CultureInfoConverter() |]
let private settings = JsonSerializerSettings (
Converters = converters,
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore)
let serialize obj = JsonConvert.SerializeObject(obj, settings)
The result in JSON is this:
{
"Id": "PriceList20140201",
"Name": "PriceList",
"Date": "2014-02-01T00:00:00+01:00",
"CurrencySymbol": "€",
"Status": 0,
"Prices": {
"ItemCodeA": 512.4,
"ItemCodeB": 471.0
}
}
And if I deserialize it, it works great
JsonConvert.DeserializeObject<PriceList>(text)
Result in F # Interactive:
val y : PriceList =
{Id = "PriceList20140201";
Name = "PriceList";
Date = 01.04.2014 00:00:00;
CurrencySymbol = "€";
Status = Sale;
Prices =
dict
[("ItemCodeA", 512.4M); ("ItemCodeB", 471.0M);...];}
Now I want to deserialize it in C # using Newtonsoft.Json
public class PriceList
{
public string Id { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public string CurrencySymbol { get; set; }
public Status Status { get; set; }
public Prices Prices { get; set; }
}
public class Prices : Dictionary<string, decimal>
{
}
...
JsonConvert.DeserializeObject<PriceList>(json)
The result is a JsonSerializationException:
JSON (, [1,2,3]) 'Halep.Logic.OfferManagement.Contracts.DataClasses.Pricing.Prices' JSON (, { "name": "value" }), . , JSON JSON (, { "name": "value" }) , (, ICollection, IList), List, JSON . JsonArrayAttribute , JSON. "", 7, 13.
, CustomConverter . , 6.0.? . 8.0.3.
F #, #? # ?
PriceList , .
, :
public class PriceList
{
public string Id { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public string CurrencySymbol { get; set; }
public Status Status { get; set; }
public Dictionary<string, decimal> Prices { get; set; }
}
:
JSON (, [1,2,3]) 'System.Collections.Generic.Dictionary`2 [System.String, System.Decimal] JSON (, { "name": "value" }), . , JSON JSON (, { "name": "value" }) , (, ICollection, IList), List, JSON . JsonArrayAttribute , JSON. "", 7, 13.
2:
. @JustinNiessner . , , [] {}:
{
"Id": "PriceList20140201",
"Name": "PriceList",
"Date": "2014-02-01T00:00:00+01:00",
"CurrencySymbol": "€",
"Status": 0,
"Prices": [
"ItemCodeA": 512.4,
"ItemCodeB": 471.0
]
}