using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public float Value { get; set; }
public static void Test()
var bytes = new byte[] { 0xF9, 0xFF, 0x4F, 0xC1 };
Test(new byte[] { 0xF9, 0xFF, 0x4F, 0xC1 } , null);
Test(BitConverter.GetBytes(0.0f), null);
Test(BitConverter.GetBytes(-0.0f), null);
public static void Test(byte[] bytes, JsonSerializerSettings settings)
Console.WriteLine("\nInput bytes: {0}.", string.Join(",", bytes.Select(b => "0x" + b.ToString("X"))));
float myFloat = BitConverter.ToSingle(bytes, 0);
Console.WriteLine("Input float: {0}.", myFloat.ToString("R"));
var root = new RootObject { Value = myFloat };
var json = JsonConvert.SerializeObject(root, settings);
var root2 = JsonConvert.DeserializeObject<RootObject>(json, settings);
Console.WriteLine("Deserialized float: {0}.", root2.Value.ToString("R"));
Assert.IsTrue(root.Value == root2.Value, "root.Value == root2.Value");
var bytes2 = BitConverter.GetBytes(root2.Value);
Assert.IsTrue(bytes.SequenceEqual(bytes2), "bytes.SequenceEqual(bytes2)");
Console.WriteLine("Initial and round-tripped float values are identical.");
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");