diff options
author | JamesNK <james@newtonking.com> | 2011-03-26 01:08:41 +0300 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2011-03-26 01:08:41 +0300 |
commit | 39056de2389eb65c2c92f98ce0787fd29d6d1d6c (patch) | |
tree | b5ab380317361bfe28f652c8999de5c15e8f2276 /Src/Newtonsoft.Json | |
parent | 6a4942526704f2d60783cfce130ac6f46591d7de (diff) |
-Fixed error when reflecting over type that has multiple index properties inherited
-Added ExpandoObjectConverter that deserializes to primitive values rather than LINQ to JSON objects for ExpandObjects
Diffstat (limited to 'Src/Newtonsoft.Json')
9 files changed, 156 insertions, 1 deletions
diff --git a/Src/Newtonsoft.Json/Bson/BsonReader.cs b/Src/Newtonsoft.Json/Bson/BsonReader.cs index fa18d61..83ad3cb 100644 --- a/Src/Newtonsoft.Json/Bson/BsonReader.cs +++ b/Src/Newtonsoft.Json/Bson/BsonReader.cs @@ -242,6 +242,9 @@ namespace Newtonsoft.Json.Bson }
}
+ /// <summary>
+ /// Changes the <see cref="JsonReader.State"/> to Closed.
+ /// </summary>
public override void Close()
{
base.Close();
diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index 4260c90..7975070 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -156,6 +156,9 @@ namespace Newtonsoft.Json.Bson _propertyName = name;
}
+ /// <summary>
+ /// Closes this stream and the underlying stream.
+ /// </summary>
public override void Close()
{
base.Close();
diff --git a/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs b/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs new file mode 100644 index 0000000..5d88542 --- /dev/null +++ b/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs @@ -0,0 +1,140 @@ +#if !(NET35 || NET20 || WINDOWS_PHONE)
+
+using System;
+using System.Collections.Generic;
+using System.Dynamic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using Newtonsoft.Json.Utilities;
+
+namespace Newtonsoft.Json.Converters
+{
+ /// <summary>
+ /// Converts an ExpandoObject to and from JSON.
+ /// </summary>
+ public class ExpandoObjectConverter : JsonConverter
+ {
+ /// <summary>
+ /// Writes the JSON representation of the object.
+ /// </summary>
+ /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
+ /// <param name="value">The value.</param>
+ /// <param name="serializer">The calling serializer.</param>
+ public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ {
+ // can write is set to false
+ }
+
+ /// <summary>
+ /// Reads the JSON representation of the object.
+ /// </summary>
+ /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
+ /// <param name="objectType">Type of the object.</param>
+ /// <param name="existingValue">The existing value of object being read.</param>
+ /// <param name="serializer">The calling serializer.</param>
+ /// <returns>The object value.</returns>
+ public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ {
+ return ReadValue(reader);
+ }
+
+ private object ReadValue(JsonReader reader)
+ {
+ while (reader.TokenType == JsonToken.Comment)
+ {
+ if (!reader.Read())
+ throw new Exception("Unexpected end.");
+ }
+
+ switch (reader.TokenType)
+ {
+ case JsonToken.StartObject:
+ return ReadObject(reader);
+ case JsonToken.StartArray:
+ return ReadList(reader);
+ default:
+ if (JsonReader.IsPrimitiveToken(reader.TokenType))
+ return reader.Value;
+
+ throw new Exception("Unexpected token when converting ExpandoObject: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
+ }
+ }
+
+ private object ReadList(JsonReader reader)
+ {
+ IList<object> list = new List<object>();
+
+ while (reader.Read())
+ {
+ switch (reader.TokenType)
+ {
+ case JsonToken.Comment:
+ break;
+ default:
+ object v = ReadValue(reader);
+
+ list.Add(v);
+ break;
+ case JsonToken.EndArray:
+ return list;
+ }
+ }
+
+ throw new Exception("Unexpected end.");
+ }
+
+ private object ReadObject(JsonReader reader)
+ {
+ IDictionary<string, object> expandoObject = new ExpandoObject();
+
+ while (reader.Read())
+ {
+ switch (reader.TokenType)
+ {
+ case JsonToken.PropertyName:
+ string propertyName = reader.Value.ToString();
+
+ if (!reader.Read())
+ throw new Exception("Unexpected end.");
+
+ object v = ReadValue(reader);
+
+ expandoObject[propertyName] = v;
+ break;
+ case JsonToken.Comment:
+ break;
+ case JsonToken.EndObject:
+ return expandoObject;
+ }
+ }
+
+ throw new Exception("Unexpected end.");
+ }
+
+ /// <summary>
+ /// Determines whether this instance can convert the specified object type.
+ /// </summary>
+ /// <param name="objectType">Type of the object.</param>
+ /// <returns>
+ /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool CanConvert(Type objectType)
+ {
+ return (objectType == typeof (ExpandoObject));
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.
+ /// </value>
+ public override bool CanWrite
+ {
+ get { return false; }
+ }
+ }
+}
+
+#endif
\ No newline at end of file diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj index 3c7fe56..7e590cc 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj @@ -97,6 +97,7 @@ <Compile Include="Converters\CustomCreationConverter.cs" />
<Compile Include="Converters\DateTimeConverterBase.cs" />
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
+ <Compile Include="Converters\ExpandoObjectConverter.cs" />
<Compile Include="Converters\KeyValuePairConverter.cs" />
<Compile Include="Converters\BsonObjectIdConverter.cs" />
<Compile Include="Converters\RegexConverter.cs" />
diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj index 4a8cd1f..54f5f07 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj @@ -95,6 +95,7 @@ <Compile Include="Converters\DataTableConverter.cs" />
<Compile Include="Converters\DateTimeConverterBase.cs" />
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
+ <Compile Include="Converters\ExpandoObjectConverter.cs" />
<Compile Include="Converters\IsoDateTimeConverter.cs" />
<Compile Include="Converters\JavaScriptDateTimeConverter.cs" />
<Compile Include="Converters\JsonDateTimeSerializationMode.cs" />
diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj index 4e194ce..0ef846e 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj @@ -68,6 +68,7 @@ <Compile Include="Converters\DataTableConverter.cs" />
<Compile Include="Converters\DateTimeConverterBase.cs" />
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
+ <Compile Include="Converters\ExpandoObjectConverter.cs" />
<Compile Include="Converters\IsoDateTimeConverter.cs" />
<Compile Include="Converters\JavaScriptDateTimeConverter.cs" />
<Compile Include="Converters\JsonDateTimeSerializationMode.cs" />
diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index 2959062..3f6d026 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -96,6 +96,7 @@ <Compile Include="Converters\CustomCreationConverter.cs" />
<Compile Include="Converters\DateTimeConverterBase.cs" />
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
+ <Compile Include="Converters\ExpandoObjectConverter.cs" />
<Compile Include="Converters\KeyValuePairConverter.cs" />
<Compile Include="Converters\BsonObjectIdConverter.cs" />
<Compile Include="Converters\RegexConverter.cs" />
diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index dc23e38..c93ffa3 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -84,6 +84,9 @@ namespace Newtonsoft.Json.Serialization #if !PocketPC && !SILVERLIGHT && !NET20
new EntityKeyMemberConverter(),
#endif
+#if !(NET35 || NET20 || WINDOWS_PHONE)
+ new ExpandoObjectConverter(),
+#endif
new BinaryConverter(),
new KeyValuePairConverter(),
#if !SILVERLIGHT || WINDOWS_PHONE
diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index d5f557a..69a1571 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -896,7 +896,9 @@ namespace Newtonsoft.Json.Utilities PropertyInfo member = propertyInfos[i];
if (member.DeclaringType != targetType)
{
- PropertyInfo declaredMember = member.DeclaringType.GetProperty(member.Name, bindingAttr);
+ Type[] types = member.GetIndexParameters().Select(p => p.ParameterType).ToArray();
+
+ PropertyInfo declaredMember = member.DeclaringType.GetProperty(member.Name, bindingAttr, null, member.PropertyType, types, null);
propertyInfos[i] = declaredMember;
}
}
|