I need to serialize objects to JSON. If I install mono from apt-get, serializing with JSON.net does not work.
I added this line into /etc/apt/sources.list:
deb http://debian.meebey.net/experimental/mono /
After this, i ran
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install mono-complete
This version of mono supports serializing into JSON. If you dont add the line into sources.list, all serializations will fail, not only those with double values. When I have a double in a dictionary or in a list, this exception always gets thrown thy JSON.net.
Input string was not in the correct format
Here is some sample code, so you can try (you need to add a reference to JSON.net):
try { Console.WriteLine(3.14159265359f.ToString()); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(3.14159265359d.ToString()); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(Convert.ToDouble("3.14159265359").ToString()); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(Convert.ToDouble("3,14159265359").ToString()); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(JsonConvert.SerializeObject(3.14159265359f)); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(JsonConvert.SerializeObject(3.14159265359d)); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(JsonConvert.SerializeObject(new List<object>() { "test", DateTime.Now, 3.14159265359f, 3.14159265359d})); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(JsonConvert.SerializeObject(new Dictionary<object, object>() {
{"test", DateTime.Now},
{DateTime.Now, 3.14159265359f},
{3.14159265359f, 3.14159265359d},
{3.14159265359d, "test"},
})); } catch (Exception ex) { Console.WriteLine(ex.Message); }
In my case, this results in
3,141593
3,14159265359
314159265359
Input string was not in the correct format
Input string was not in the correct format
Input string was not in the correct format
Input string was not in the correct format
Input string was not in the correct format
I need to serialize and dictionary or an object where some properties are double numbers, but I dont know how to fix this error.
CultureInfo.InvariantCulture
to yourToDouble
conversions to see if they parse properly. i.e.Convert.ToDouble("3.14159265359", CultureInfo.InvariantCulture)
andConvert.ToDouble("3,14159265359",CultureInfo.InvariantCulture)
– HeatfanJohn Apr 8 '14 at 16:16JsonConvert.SerializeObject(3.14159265359d)
– feedc0de Apr 10 '14 at 5:45