Commits

Aarni Koskela committed 6bfab1b

More cleanup.
decimal-to-JsonValue support (encoded as strings to avoid loss of precision)
float-to-JsonValue support.

Comments (0)

Files changed (2)

JsonParser/JsonParser.cs

 
 		// http://stackoverflow.com/a/457708/51685
 		private static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
+			
 			while (toCheck != typeof (object)) {
+				if (toCheck == null || generic == null) return false;
 				var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
 				if (generic == cur) {
 					return true;
 		}
 
 		public static JsonValue FromObject(object value) {
+			var fromObject = value as JsonValue;
+			if (fromObject != null) {
+				return fromObject;
+			}
 			if (value == null) {
 				return Null();
 			}
 			if (value is double) {
 				return Double((double) value);
 			}
+			if (value is float) {
+				return Double((float) value);
+			}
 			if (value is int) {
 				return Integer((int) value);
 			}
+			if(value is decimal) {
+				return String(((decimal) value).ToString(CultureInfo.InvariantCulture));
+			}
 			if (value is bool) {
 				return Boolean((bool) value);
 			}
 		private static Dictionary<object, object> UpcastDictionary(object value) {
 			var noArguments = new object[] {};
 			var d2 = new Dictionary<object, object>();
-			foreach (object currentKvp in (value as IEnumerable)) {
+			foreach (object currentKvp in ((IEnumerable) value)) {
 				var kvpType = currentKvp.GetType();
 				object dKey = kvpType.InvokeMember("Key", BindingFlags.GetProperty, null, currentKvp, noArguments);
 				object dValue = kvpType.InvokeMember("Value", BindingFlags.GetProperty, null, currentKvp, noArguments);
 			return new JsonValue {_type = JsonValueType.Double, _doubleValue = d};
 		}
 
+		public static JsonValue Double(float f) {
+			return new JsonValue { _type = JsonValueType.Double, _doubleValue = f };
+		}
+
 		public static JsonValue Integer(int i) {
 			return new JsonValue {_type = JsonValueType.Integer, _intValue = i};
 		}

JsonParser/Program.cs

 using System;
+using System.Globalization;
 using System.IO;
 using System.Text;
 using System.Collections.Generic;
 
-namespace JsonParser
-{
-	static class Program {
-		static void Main(string[] args) {
+namespace JsonParser {
+	internal static class Program {
+		private static void Main(string[] args) {
 			FeatureTest();
 			Console.Write("\n\n\n======\n\n\n");
 			FromObjectTest();
 				Console.Write(fi.Name.PadRight(16) + " ... ");
 				using (var file = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read)) {
 					var tr = new StreamReader(file, Encoding.UTF8);
-					
+
 					JsonValue obj;
 					try {
 						obj = JsonParser.Parse(tr);
-					} catch(Exception exc) {
-						if(!fi.Name.Contains("fail")) {
+					}
+					catch (Exception exc) {
+						if (!fi.Name.Contains("fail")) {
 							Console.WriteLine("Shouldn't have failed at this file.");
 							throw;
 						}
 			Console.WriteLine("double = {0}", div.Get("double").DoubleValue);
 			Console.WriteLine("count = {0}", div.Count);
 			Console.WriteLine("stringstringdict:");
-			foreach(var kvp in div.GetStringStringDict()) {
+			foreach (var kvp in div.GetStringStringDict()) {
 				Console.WriteLine("  {0} : {1}", kvp.Key.PadRight(30), kvp.Value);
 			}
 			var entry = div.ResolvePath("GlossList", "GlossEntry");
 			Console.WriteLine("glossee: {0}", entry.ResolvePath("GlossSee").StrValue);
 			Console.WriteLine("Deleted: {0}", root.ResolvePath("glossary.GlossDiv.deleted").BoolValue);
 
-			using(var ms = new MemoryStream()) {
+			using (var ms = new MemoryStream()) {
 				var sw = new StreamWriter(ms, Encoding.UTF32);
 				root.ToJSON(sw);
 				sw.Flush();
 			var jv = JsonValue.FromObject(new List<object> {
 				"foo",
 				3.141,
+				10.1f,
+				JsonValue.Double(640.33f),
 				-1024,
+				null,
 				new Dictionary<string, bool> {
 					{"hello", true},
 					{"yes", false},
+				},
+				new Dictionary<int, List<object>> {
+					{120, new List<object> {15, 16, 18}},
+					{140, new List<object> {"yes", true, "false", null, 650.50, Decimal.Parse("548120.123106333", NumberStyles.Currency, CultureInfo.InvariantCulture)}},
 				}
 			});
 			Console.Write(jv.ToJSON());
 		}
 	}
-}
+}