i'm using newtonsoft json serialize dataset binary json using code below. when de-serializing dataset, field type changes decimal double? know what's going wrong?
sample code:
static void main(string[] args) { var ds = new dataset(); var dt = ds.tables.add(); dt.columns.add("test", typeof(decimal)); dt.rows.add(new object[] { 1.23345m }); var data = datasettobinjson(ds); var ds2 = binjsontodataset(data); console.writeline((ds2.tables[0].columns[0].datatype == typeof(decimal)) ? "passed" : string.format("failed- {0}", ds2.tables[0].columns[0].datatype)); console.readline(); } /// <summary> /// utility function create optimized binary json version of dataset /// </summary> public static byte[] datasettobinjson(dataset dataset) { if (dataset == null || dataset.tables == null || dataset.tables.count == 0) { return null; } using (var ms = new memorystream()) { using (var writer = new bsonwriter(ms)) { var serializer = new jsonserializer(); serializer.serialize(writer, dataset); return ms.toarray(); } } } /// <summary> /// utility function deserialize optimized binary json serialized dataset /// </summary> public static dataset binjsontodataset(byte[] databytes) { if (databytes.length == 0) { return null; } using (var ms = new memorystream(databytes)) { using (var reader = new bsonreader(ms)) { var serializer = new jsonserializer(); return serializer.deserialize<dataset>(reader); } } }
easy way fix set floatparsehandling = floatparsehandling.decimal
example:
public class foo { public object value { get; set; } } foo original = new foo { value = 1.23m }; string json = jsonconvert.serializeobject(original); var settings = new jsonserializersettings { floatparsehandling = floatparsehandling.decimal //hint }; foo actual = jsonconvert.deserializeobject<foo>(json, settings); // actual.value.gettype() == 'decimal'
Comments
Post a Comment