Commits

Greg...@GregDennis-PC.home  committed d15861e

Fixed bug stripping spaces during JsonValue.Parse.
Added JsonMapToAttribute.
Updated documentation.

  • Participants
  • Parent commits da5e464

Comments (0)

Files changed (12)

 Json/obj/
 _ReSharper.Json/
 Json.5.1.ReSharper.user
+Json.6.0.ReSharper.user
+Json.sln.DotSettings.user

File Json.Tests/Json/JsonObjectTest.cs

 			var s = "{\"bool\":false,\"int\":42,\"string\":\"a string\"";
 			var actual = new JsonObject(s);
 		}
+		[TestMethod]
+		public void Parse_StringFromSourceForge()
+		{
+			var s = @"{
+  ""self"": ""self"",
+  ""name"": ""name"",
+  ""emailAddress"": ""test at test dot com"",
+  ""avatarUrls"": {
+	""16x16"": ""http://smallUrl"",
+	""48x48"": ""https://largeUrl""
+  },
+  ""displayName"": ""Display Name"",
+  ""active"": true,
+  ""timeZone"": ""Europe"",
+  ""groups"": {
+	""size"": 1,
+	""items"": [
+	  {
+		""name"": ""users""
+	  }
+	]
+  },
+  ""expand"": ""groups""
+}";
+			var actual = new JsonObject(s);
+			var newString = actual.ToString();
+		}
 	}
 }

File Json.Tests/Json/Serialization/JsonSerializerTest.cs

 			           		{"IntProp", 42},
 			           		{"DoubleProp", 6},
 			           		{"BoolProp", true},
-							{"EnumProp", 2}
+							{"EnumProp", 2},
+							{"MapToMe", 4}
 			           	};
 			var expected = new ObjectWithBasicProps
 			               	{
 			               		IntProp = 42,
 			               		DoubleProp = 6.0,
 			               		BoolProp = true,
-			               		EnumProp = JsonValueType.Boolean
+			               		EnumProp = JsonValueType.Boolean,
+								MappedProp = 4
 							};
 			var actual = _serializer.Deserialize<ObjectWithBasicProps>(json);
 			Assert.AreEqual(expected, actual);
 							IntProp = 42,
 							DoubleProp = 6.0,
 							BoolProp = true,
-							EnumProp = JsonValueType.Boolean
+							EnumProp = JsonValueType.Boolean,
+							MappedProp = 4
 						};
 			JsonValue expected = new JsonObject
 									{
 										{"IntProp", 42},
 										{"DoubleProp", 6},
 										{"BoolProp", true},
-										{"EnumProp", 2}
+										{"EnumProp", 2},
+										{"MapToMe", 4}
 									};
 			var actual = _serializer.Serialize(obj);
 			Assert.AreEqual(expected, actual);
 										{"StringProp", "stringValue"},
 										{"IntProp", 42},
 										{"DoubleProp", 0},
-										{"BoolProp", true}
+										{"BoolProp", true},
+										{"EnumProp", 0},
+										{"MapToMe", 0}
 									};
 			var actual = _serializer.Serialize(obj);
 			_serializer.Options = null;

File Json.Tests/Test References/ObjectWithBasicProps.cs

 		public double DoubleProp { get; set; }
 		public bool BoolProp { get; set; }
 		public JsonValueType EnumProp { get; set; }
+		[JsonMapTo("MapToMe")]
+		public int MappedProp { get; set; }
 		#endregion
 
 		#region Nonserializable Instance Properties
 			       Equals(other.ReadOnlyProp, ReadOnlyProp) &&
 			       Equals(other.WriteOnlyProp, WriteOnlyProp) &&
 			       Equals(other.IgnoreProp, IgnoreProp) &&
-				   Equals(other.EnumProp, EnumProp);
+				   Equals(other.EnumProp, EnumProp) &&
+				   Equals(other.MappedProp, MappedProp);
 		}
 		public override int GetHashCode()
 		{
 				result = (result * 397) ^ (WriteOnlyProp != null ? WriteOnlyProp.GetHashCode() : 0);
 				result = (result * 397) ^ (IgnoreProp != null ? IgnoreProp.GetHashCode() : 0);
 				result = (result * 397) ^ EnumProp.GetHashCode();
+				result = (result * 397) ^ MappedProp;
 				return result;
 			}
 		}

File Json.sln.docstates.suo

Binary file modified.

File Json.suo

Binary file modified.

File Json/Attributes/JsonMapToAttribute.cs

+/***************************************************************************************
+
+	Copyright 2012 Greg Dennis
+
+	   Licensed under the Apache License, Version 2.0 (the "License");
+	   you may not use this file except in compliance with the License.
+	   You may obtain a copy of the License at
+
+		 http://www.apache.org/licenses/LICENSE-2.0
+
+	   Unless required by applicable law or agreed to in writing, software
+	   distributed under the License is distributed on an "AS IS" BASIS,
+	   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+	   See the License for the specific language governing permissions and
+	   limitations under the License.
+ 
+	File Name:		JsonMapToAttribute.cs
+	Namespace:		Manatee.Json.Attributes
+	Class Name:		JsonMapToAttribute
+	Purpose:		Applied to properties to customize how they are to be
+					serialized.
+
+***************************************************************************************/
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Manatee.Json.Attributes
+{
+	/// <summary>
+	/// Allows the user to specify how a property is mapped during serialization.
+	/// </summary>
+	[AttributeUsage(AttributeTargets.Property)]
+	public class JsonMapToAttribute : Attribute
+	{
+		public string MapToKey { get; set; }
+
+		public JsonMapToAttribute(string key)
+		{
+			MapToKey = key;
+		}
+	}
+}

File Json/JsonObject.cs

 		public JsonObject(string s)
 			: this()
 		{
-			var inString = false;
-			source = "";
-			for (var i = 0; i < s.Length; i++)
-			{
-				if (s[i] == '"') inString = !inString;
-				if (inString || (!inString && !IsWhiteSpace(s[i])))
-					source += s[i];
-			}
+			source = StripExternalSpaces(s);
 			Parse(0);
 		}
 
 		{
 			return (c == 10) || (c == 13) || (c == 32) || (c == 9);
 		}
+		private static string StripExternalSpaces(string s)
+		{
+			var inString = false;
+			var ret = "";
+			for (var i = 0; i < s.Length; i++)
+			{
+				if (s[i] == '"') inString = !inString;
+				if (inString || !IsWhiteSpace(s[i]))
+					ret += s[i];
+			}
+			return ret;
+		}
 		private int Parse(int i)
 		{
 			if (stream == null)

File Json/JsonValue.cs

 		public static JsonValue Parse(string source)
 		{
 			var i = 1;
-			return Parse(source, ref i);
+			return Parse(StripExternalSpaces(source), ref i);
 		}
 
 		/// <summary>
 			}
 			return s;
 		}
+
+		private static bool IsWhiteSpace(char c)
+		{
+			return (c == 10) || (c == 13) || (c == 32) || (c == 9);
+		}
+		private static string StripExternalSpaces(string s)
+		{
+			var inString = false;
+			var ret = "";
+			for (var i = 0; i < s.Length; i++)
+			{
+				if (s[i] == '"') inString = !inString;
+				if (inString || !IsWhiteSpace(s[i]))
+					ret += s[i];
+			}
+			return ret;
+		}
 	}
 }

File Json/Manatee.Json.csproj

     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Attributes\JsonMapToAttribute.cs" />
     <Compile Include="Helpers\PropertyReference.cs" />
     <Compile Include="Helpers\SerializerReferenceCache.cs" />
     <Compile Include="Helpers\SerializerReferenceRecord.cs" />

File Json/Serialization/JsonSerializer.cs

 				var serialize = SerializerCache.Instance.GetSerializer(type);
 				var jsonProp = (JsonValue) serialize.Invoke(this, new[] {value});
 				if ((jsonProp == JsonValue.Null) && !Options.EncodeDefaultValues) continue;
-				json.Add(propertyInfo.Name,
+				string name = propertyInfo.Name;
+				var mapper = (JsonMapToAttribute)propertyInfo.GetCustomAttributes(typeof(JsonMapToAttribute), false).FirstOrDefault();
+				if (mapper != null)
+					name = mapper.MapToKey;
+				json.Add(name,
 				         type == propertyInfo.PropertyType
 				         	? jsonProp
 				         	: new JsonObject {{TypeKey, type.AssemblyQualifiedName}, {ValueKey, jsonProp}});
 			var obj = Activator.CreateInstance<T>();
 			foreach (var propertyInfo in propertyInfoList)
 			{
-				if (json.Object.ContainsKey(propertyInfo.Name))
+				string name = propertyInfo.Name;
+				var mapper = (JsonMapToAttribute)propertyInfo.GetCustomAttributes(typeof(JsonMapToAttribute), false).FirstOrDefault();
+				if (mapper != null)
+					name = mapper.MapToKey;
+				if (json.Object.ContainsKey(name))
 				{
-					var value = json.Object[propertyInfo.Name];
+					var value = json.Object[name];
 					if ((value.Type == JsonValueType.Object) && (value.Object.ContainsKey(TypeKey)))
 					{
 						var instanceType = Type.GetType(value.Object[TypeKey].String);
 					else
 					{
 						var deserialize = SerializerCache.Instance.GetDeserializer(propertyInfo.PropertyType);
-						propertyInfo.SetValue(obj, deserialize.Invoke(this, new object[] {json.Object[propertyInfo.Name]}), null);
+						propertyInfo.SetValue(obj, deserialize.Invoke(this, new object[] {json.Object[name]}), null);
 					}
-					json.Object.Remove(propertyInfo.Name);
+					json.Object.Remove(name);
 				}
 			}
 			if ((json.Object.Count > 0) && (Options.InvalidPropertyKeyBehavior == InvalidPropertyKeyBehavior.ThrowException))

File Manatee.Json - ReadMe.docx

Binary file modified.