2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
26
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
28
using Newtonsoft.Json.Serialization;
29
using System.Globalization;
30
using Newtonsoft.Json.Utilities;
32
namespace Newtonsoft.Json.Converters
34
internal interface IEntityKeyMember
36
string Key { get; set; }
37
object Value { get; set; }
41
/// Converts an Entity Framework EntityKey to and from JSON.
43
public class EntityKeyMemberConverter : JsonConverter
45
private const string EntityKeyMemberFullTypeName = "System.Data.EntityKeyMember";
48
/// Writes the JSON representation of the object.
50
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
51
/// <param name="value">The value.</param>
52
/// <param name="serializer">The calling serializer.</param>
53
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
55
IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(value);
56
Type keyType = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;
58
writer.WriteStartObject();
59
writer.WritePropertyName("Key");
60
writer.WriteValue(entityKeyMember.Key);
61
writer.WritePropertyName("Type");
62
writer.WriteValue((keyType != null) ? keyType.FullName : null);
64
writer.WritePropertyName("Value");
69
if (JsonSerializerInternalWriter.TryConvertToString(entityKeyMember.Value, keyType, out valueJson))
70
writer.WriteValue(valueJson);
72
writer.WriteValue(entityKeyMember.Value);
79
writer.WriteEndObject();
82
private static void ReadAndAssertProperty(JsonReader reader, string propertyName)
84
ReadAndAssert(reader);
86
if (reader.TokenType != JsonToken.PropertyName || reader.Value.ToString() != propertyName)
87
throw new JsonSerializationException("Expected JSON property '{0}'.".FormatWith(CultureInfo.InvariantCulture, propertyName));
90
private static void ReadAndAssert(JsonReader reader)
93
throw new JsonSerializationException("Unexpected end.");
97
/// Reads the JSON representation of the object.
99
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
100
/// <param name="objectType">Type of the object.</param>
101
/// <param name="existingValue">The existing value of object being read.</param>
102
/// <param name="serializer">The calling serializer.</param>
103
/// <returns>The object value.</returns>
104
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
106
IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(Activator.CreateInstance(objectType));
108
ReadAndAssertProperty(reader, "Key");
109
ReadAndAssert(reader);
110
entityKeyMember.Key = reader.Value.ToString();
112
ReadAndAssertProperty(reader, "Type");
113
ReadAndAssert(reader);
114
string type = reader.Value.ToString();
116
Type t = Type.GetType(type);
118
ReadAndAssertProperty(reader, "Value");
119
ReadAndAssert(reader);
120
entityKeyMember.Value = serializer.Deserialize(reader, t);
122
ReadAndAssert(reader);
124
return DynamicWrapper.GetUnderlyingObject(entityKeyMember);
128
/// Determines whether this instance can convert the specified object type.
130
/// <param name="objectType">Type of the object.</param>
132
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
134
public override bool CanConvert(Type objectType)
136
return (objectType.AssignableToTypeName(EntityKeyMemberFullTypeName));
b'\\ No newline at end of file'