-Line endings
This commit is contained in:
@@ -1,183 +1,183 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
using System.Data.SqlTypes;
|
||||
#endif
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
internal interface IBinary
|
||||
{
|
||||
byte[] ToArray();
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Converts a binary value to and from a base 64 string value.
|
||||
/// </summary>
|
||||
public class BinaryConverter : JsonConverter
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
private const string BinaryTypeName = "System.Data.Linq.Binary";
|
||||
#endif
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] data = GetByteArray(value);
|
||||
|
||||
writer.WriteValue(data);
|
||||
}
|
||||
|
||||
private byte[] GetByteArray(object value)
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
|
||||
if (value.GetType().AssignableToTypeName(BinaryTypeName))
|
||||
{
|
||||
IBinary binary = DynamicWrapper.CreateWrapper<IBinary>(value);
|
||||
return binary.ToArray();
|
||||
}
|
||||
#endif
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
if (value is SqlBinary)
|
||||
return ((SqlBinary) value).Value;
|
||||
#endif
|
||||
throw new Exception("Unexpected value type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullable(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] data;
|
||||
|
||||
if (reader.TokenType == JsonToken.StartArray)
|
||||
{
|
||||
data = ReadByteArray(reader);
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
// current token is already at base64 string
|
||||
// unable to call ReadAsBytes so do it the old fashion way
|
||||
string encodedData = reader.Value.ToString();
|
||||
data = Convert.FromBase64String(encodedData);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected token parsing binary. Expected String or StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
}
|
||||
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
if (t.AssignableToTypeName(BinaryTypeName))
|
||||
return Activator.CreateInstance(t, data);
|
||||
#endif
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
if (t == typeof(SqlBinary))
|
||||
return new SqlBinary(data);
|
||||
#endif
|
||||
throw new Exception("Unexpected object type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
}
|
||||
|
||||
private byte[] ReadByteArray(JsonReader reader)
|
||||
{
|
||||
List<byte> byteList = new List<byte>();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.Integer:
|
||||
byteList.Add(Convert.ToByte(reader.Value, CultureInfo.InvariantCulture));
|
||||
break;
|
||||
case JsonToken.EndArray:
|
||||
return byteList.ToArray();
|
||||
case JsonToken.Comment:
|
||||
// skip
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Unexpected token when reading bytes: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("Unexpected end when reading bytes.");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
if (objectType.AssignableToTypeName(BinaryTypeName))
|
||||
return true;
|
||||
#endif
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
if (objectType == typeof(SqlBinary) || objectType == typeof(SqlBinary?))
|
||||
return true;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
using System.Data.SqlTypes;
|
||||
#endif
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
internal interface IBinary
|
||||
{
|
||||
byte[] ToArray();
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Converts a binary value to and from a base 64 string value.
|
||||
/// </summary>
|
||||
public class BinaryConverter : JsonConverter
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
private const string BinaryTypeName = "System.Data.Linq.Binary";
|
||||
#endif
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] data = GetByteArray(value);
|
||||
|
||||
writer.WriteValue(data);
|
||||
}
|
||||
|
||||
private byte[] GetByteArray(object value)
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
|
||||
if (value.GetType().AssignableToTypeName(BinaryTypeName))
|
||||
{
|
||||
IBinary binary = DynamicWrapper.CreateWrapper<IBinary>(value);
|
||||
return binary.ToArray();
|
||||
}
|
||||
#endif
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
if (value is SqlBinary)
|
||||
return ((SqlBinary) value).Value;
|
||||
#endif
|
||||
throw new Exception("Unexpected value type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullable(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] data;
|
||||
|
||||
if (reader.TokenType == JsonToken.StartArray)
|
||||
{
|
||||
data = ReadByteArray(reader);
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
// current token is already at base64 string
|
||||
// unable to call ReadAsBytes so do it the old fashion way
|
||||
string encodedData = reader.Value.ToString();
|
||||
data = Convert.FromBase64String(encodedData);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected token parsing binary. Expected String or StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
}
|
||||
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
if (t.AssignableToTypeName(BinaryTypeName))
|
||||
return Activator.CreateInstance(t, data);
|
||||
#endif
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
if (t == typeof(SqlBinary))
|
||||
return new SqlBinary(data);
|
||||
#endif
|
||||
throw new Exception("Unexpected object type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
}
|
||||
|
||||
private byte[] ReadByteArray(JsonReader reader)
|
||||
{
|
||||
List<byte> byteList = new List<byte>();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.Integer:
|
||||
byteList.Add(Convert.ToByte(reader.Value, CultureInfo.InvariantCulture));
|
||||
break;
|
||||
case JsonToken.EndArray:
|
||||
return byteList.ToArray();
|
||||
case JsonToken.Comment:
|
||||
// skip
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Unexpected token when reading bytes: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("Unexpected end when reading bytes.");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
if (objectType.AssignableToTypeName(BinaryTypeName))
|
||||
return true;
|
||||
#endif
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
if (objectType == typeof(SqlBinary) || objectType == typeof(SqlBinary?))
|
||||
return true;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +1,64 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="BsonObjectId"/> to and from JSON and BSON.
|
||||
/// </summary>
|
||||
public class BsonObjectIdConverter : 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)
|
||||
{
|
||||
BsonObjectId objectId = (BsonObjectId) value;
|
||||
|
||||
BsonWriter bsonWriter = writer as BsonWriter;
|
||||
if (bsonWriter != null)
|
||||
{
|
||||
bsonWriter.WriteObjectId(objectId.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteValue(objectId.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Bytes)
|
||||
throw new JsonSerializationException("Expected Bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
byte[] value = (byte[])reader.Value;
|
||||
|
||||
return new BsonObjectId(value);
|
||||
}
|
||||
|
||||
/// <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 (BsonObjectId));
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="BsonObjectId"/> to and from JSON and BSON.
|
||||
/// </summary>
|
||||
public class BsonObjectIdConverter : 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)
|
||||
{
|
||||
BsonObjectId objectId = (BsonObjectId) value;
|
||||
|
||||
BsonWriter bsonWriter = writer as BsonWriter;
|
||||
if (bsonWriter != null)
|
||||
{
|
||||
bsonWriter.WriteObjectId(objectId.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteValue(objectId.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Bytes)
|
||||
throw new JsonSerializationException("Expected Bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
byte[] value = (byte[])reader.Value;
|
||||
|
||||
return new BsonObjectId(value);
|
||||
}
|
||||
|
||||
/// <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 (BsonObjectId));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,99 +1,99 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a custom object
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class CustomCreationConverter<T> : 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)
|
||||
{
|
||||
throw new NotSupportedException("CustomCreationConverter should only be used while deserializing.");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return null;
|
||||
|
||||
T value = Create(objectType);
|
||||
if (value == null)
|
||||
throw new JsonSerializationException("No object created.");
|
||||
|
||||
serializer.Populate(reader, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an object which will then be populated by the serializer.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <returns></returns>
|
||||
public abstract T Create(Type objectType);
|
||||
|
||||
/// <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 typeof (T).IsAssignableFrom(objectType);
|
||||
}
|
||||
|
||||
/// <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; }
|
||||
}
|
||||
}
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a custom object
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class CustomCreationConverter<T> : 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)
|
||||
{
|
||||
throw new NotSupportedException("CustomCreationConverter should only be used while deserializing.");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return null;
|
||||
|
||||
T value = Create(objectType);
|
||||
if (value == null)
|
||||
throw new JsonSerializationException("No object created.");
|
||||
|
||||
serializer.Populate(reader, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an object which will then be populated by the serializer.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <returns></returns>
|
||||
public abstract T Create(Type objectType);
|
||||
|
||||
/// <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 typeof (T).IsAssignableFrom(objectType);
|
||||
}
|
||||
|
||||
/// <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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,103 +1,103 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE)
|
||||
using System;
|
||||
using System.Data;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DataSet"/> to and from JSON.
|
||||
/// </summary>
|
||||
public class DataSetConverter : 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)
|
||||
{
|
||||
DataSet dataSet = (DataSet)value;
|
||||
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
|
||||
|
||||
DataTableConverter converter = new DataTableConverter();
|
||||
|
||||
writer.WriteStartObject();
|
||||
|
||||
foreach (DataTable table in dataSet.Tables)
|
||||
{
|
||||
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(table.TableName) : table.TableName);
|
||||
|
||||
converter.WriteJson(writer, table, serializer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
|
||||
DataTableConverter converter = new DataTableConverter();
|
||||
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
DataTable dt = (DataTable)converter.ReadJson(reader, typeof (DataTable), null, serializer);
|
||||
ds.Tables.Add(dt);
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance can convert the specified value type.
|
||||
/// </summary>
|
||||
/// <param name="valueType">Type of the value.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
return (valueType == typeof(DataSet));
|
||||
}
|
||||
}
|
||||
}
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE)
|
||||
using System;
|
||||
using System.Data;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DataSet"/> to and from JSON.
|
||||
/// </summary>
|
||||
public class DataSetConverter : 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)
|
||||
{
|
||||
DataSet dataSet = (DataSet)value;
|
||||
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
|
||||
|
||||
DataTableConverter converter = new DataTableConverter();
|
||||
|
||||
writer.WriteStartObject();
|
||||
|
||||
foreach (DataTable table in dataSet.Tables)
|
||||
{
|
||||
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(table.TableName) : table.TableName);
|
||||
|
||||
converter.WriteJson(writer, table, serializer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
|
||||
DataTableConverter converter = new DataTableConverter();
|
||||
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
DataTable dt = (DataTable)converter.ReadJson(reader, typeof (DataTable), null, serializer);
|
||||
ds.Tables.Add(dt);
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance can convert the specified value type.
|
||||
/// </summary>
|
||||
/// <param name="valueType">Type of the value.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
return (valueType == typeof(DataSet));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,156 +1,156 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
using System;
|
||||
using System.Data;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DataTable"/> to and from JSON.
|
||||
/// </summary>
|
||||
public class DataTableConverter : 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)
|
||||
{
|
||||
DataTable table = (DataTable)value;
|
||||
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
|
||||
|
||||
writer.WriteStartArray();
|
||||
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
foreach (DataColumn column in row.Table.Columns)
|
||||
{
|
||||
if (serializer.NullValueHandling == NullValueHandling.Ignore && (row[column] == null || row[column] == DBNull.Value))
|
||||
continue;
|
||||
|
||||
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(column.ColumnName) : column.ColumnName);
|
||||
serializer.Serialize(writer, row[column]);
|
||||
}
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
DataTable dt;
|
||||
|
||||
if (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
dt = new DataTable((string)reader.Value);
|
||||
reader.Read();
|
||||
}
|
||||
else
|
||||
{
|
||||
dt = new DataTable();
|
||||
}
|
||||
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.StartObject)
|
||||
{
|
||||
DataRow dr = dt.NewRow();
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
string columnName = (string)reader.Value;
|
||||
|
||||
reader.Read();
|
||||
|
||||
if (!dt.Columns.Contains(columnName))
|
||||
{
|
||||
Type columnType = GetColumnDataType(reader.TokenType);
|
||||
dt.Columns.Add(new DataColumn(columnName, columnType));
|
||||
}
|
||||
|
||||
dr[columnName] = reader.Value ?? DBNull.Value;
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
dr.EndEdit();
|
||||
dt.Rows.Add(dr);
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
private static Type GetColumnDataType(JsonToken tokenType)
|
||||
{
|
||||
switch (tokenType)
|
||||
{
|
||||
case JsonToken.Integer:
|
||||
return typeof (long);
|
||||
case JsonToken.Float:
|
||||
return typeof (double);
|
||||
case JsonToken.String:
|
||||
case JsonToken.Null:
|
||||
case JsonToken.Undefined:
|
||||
return typeof (string);
|
||||
case JsonToken.Boolean:
|
||||
return typeof (bool);
|
||||
case JsonToken.Date:
|
||||
return typeof (DateTime);
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance can convert the specified value type.
|
||||
/// </summary>
|
||||
/// <param name="valueType">Type of the value.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
return (valueType == typeof(DataTable));
|
||||
}
|
||||
}
|
||||
}
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !SILVERLIGHT && !NETFX_CORE
|
||||
using System;
|
||||
using System.Data;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DataTable"/> to and from JSON.
|
||||
/// </summary>
|
||||
public class DataTableConverter : 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)
|
||||
{
|
||||
DataTable table = (DataTable)value;
|
||||
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
|
||||
|
||||
writer.WriteStartArray();
|
||||
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
foreach (DataColumn column in row.Table.Columns)
|
||||
{
|
||||
if (serializer.NullValueHandling == NullValueHandling.Ignore && (row[column] == null || row[column] == DBNull.Value))
|
||||
continue;
|
||||
|
||||
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(column.ColumnName) : column.ColumnName);
|
||||
serializer.Serialize(writer, row[column]);
|
||||
}
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
DataTable dt;
|
||||
|
||||
if (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
dt = new DataTable((string)reader.Value);
|
||||
reader.Read();
|
||||
}
|
||||
else
|
||||
{
|
||||
dt = new DataTable();
|
||||
}
|
||||
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.StartObject)
|
||||
{
|
||||
DataRow dr = dt.NewRow();
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
string columnName = (string)reader.Value;
|
||||
|
||||
reader.Read();
|
||||
|
||||
if (!dt.Columns.Contains(columnName))
|
||||
{
|
||||
Type columnType = GetColumnDataType(reader.TokenType);
|
||||
dt.Columns.Add(new DataColumn(columnName, columnType));
|
||||
}
|
||||
|
||||
dr[columnName] = reader.Value ?? DBNull.Value;
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
dr.EndEdit();
|
||||
dt.Rows.Add(dr);
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
private static Type GetColumnDataType(JsonToken tokenType)
|
||||
{
|
||||
switch (tokenType)
|
||||
{
|
||||
case JsonToken.Integer:
|
||||
return typeof (long);
|
||||
case JsonToken.Float:
|
||||
return typeof (double);
|
||||
case JsonToken.String:
|
||||
case JsonToken.Null:
|
||||
case JsonToken.Undefined:
|
||||
return typeof (string);
|
||||
case JsonToken.Boolean:
|
||||
return typeof (bool);
|
||||
case JsonToken.Date:
|
||||
return typeof (DateTime);
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance can convert the specified value type.
|
||||
/// </summary>
|
||||
/// <param name="valueType">Type of the value.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
return (valueType == typeof(DataTable));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,29 +1,29 @@
|
||||
using System;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a base class for converting a <see cref="DateTime"/> to and from JSON.
|
||||
/// </summary>
|
||||
public abstract class DateTimeConverterBase : JsonConverter
|
||||
{
|
||||
/// <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)
|
||||
{
|
||||
if (objectType == typeof(DateTime) || objectType == typeof(DateTime?))
|
||||
return true;
|
||||
#if !PocketPC && !NET20
|
||||
if (objectType == typeof(DateTimeOffset) || objectType == typeof(DateTimeOffset?))
|
||||
return true;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
using System;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a base class for converting a <see cref="DateTime"/> to and from JSON.
|
||||
/// </summary>
|
||||
public abstract class DateTimeConverterBase : JsonConverter
|
||||
{
|
||||
/// <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)
|
||||
{
|
||||
if (objectType == typeof(DateTime) || objectType == typeof(DateTime?))
|
||||
return true;
|
||||
#if !PocketPC && !NET20
|
||||
if (objectType == typeof(DateTimeOffset) || objectType == typeof(DateTimeOffset?))
|
||||
return true;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,140 +1,140 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20 && !NETFX_CORE
|
||||
using System;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
internal interface IEntityKeyMember
|
||||
{
|
||||
string Key { get; set; }
|
||||
object Value { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an Entity Framework EntityKey to and from JSON.
|
||||
/// </summary>
|
||||
public class EntityKeyMemberConverter : JsonConverter
|
||||
{
|
||||
private const string EntityKeyMemberFullTypeName = "System.Data.EntityKeyMember";
|
||||
|
||||
/// <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)
|
||||
{
|
||||
IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(value);
|
||||
Type keyType = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Key");
|
||||
writer.WriteValue(entityKeyMember.Key);
|
||||
writer.WritePropertyName("Type");
|
||||
writer.WriteValue((keyType != null) ? keyType.FullName : null);
|
||||
|
||||
writer.WritePropertyName("Value");
|
||||
|
||||
if (keyType != null)
|
||||
{
|
||||
string valueJson;
|
||||
if (JsonSerializerInternalWriter.TryConvertToString(entityKeyMember.Value, keyType, out valueJson))
|
||||
writer.WriteValue(valueJson);
|
||||
else
|
||||
writer.WriteValue(entityKeyMember.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
private static void ReadAndAssertProperty(JsonReader reader, string propertyName)
|
||||
{
|
||||
ReadAndAssert(reader);
|
||||
|
||||
if (reader.TokenType != JsonToken.PropertyName || reader.Value.ToString() != propertyName)
|
||||
throw new JsonSerializationException("Expected JSON property '{0}'.".FormatWith(CultureInfo.InvariantCulture, propertyName));
|
||||
}
|
||||
|
||||
private static void ReadAndAssert(JsonReader reader)
|
||||
{
|
||||
if (!reader.Read())
|
||||
throw new JsonSerializationException("Unexpected end.");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(Activator.CreateInstance(objectType));
|
||||
|
||||
ReadAndAssertProperty(reader, "Key");
|
||||
ReadAndAssert(reader);
|
||||
entityKeyMember.Key = reader.Value.ToString();
|
||||
|
||||
ReadAndAssertProperty(reader, "Type");
|
||||
ReadAndAssert(reader);
|
||||
string type = reader.Value.ToString();
|
||||
|
||||
Type t = Type.GetType(type);
|
||||
|
||||
ReadAndAssertProperty(reader, "Value");
|
||||
ReadAndAssert(reader);
|
||||
entityKeyMember.Value = serializer.Deserialize(reader, t);
|
||||
|
||||
ReadAndAssert(reader);
|
||||
|
||||
return DynamicWrapper.GetUnderlyingObject(entityKeyMember);
|
||||
}
|
||||
|
||||
/// <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.AssignableToTypeName(EntityKeyMemberFullTypeName));
|
||||
}
|
||||
}
|
||||
}
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20 && !NETFX_CORE
|
||||
using System;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
internal interface IEntityKeyMember
|
||||
{
|
||||
string Key { get; set; }
|
||||
object Value { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an Entity Framework EntityKey to and from JSON.
|
||||
/// </summary>
|
||||
public class EntityKeyMemberConverter : JsonConverter
|
||||
{
|
||||
private const string EntityKeyMemberFullTypeName = "System.Data.EntityKeyMember";
|
||||
|
||||
/// <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)
|
||||
{
|
||||
IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(value);
|
||||
Type keyType = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Key");
|
||||
writer.WriteValue(entityKeyMember.Key);
|
||||
writer.WritePropertyName("Type");
|
||||
writer.WriteValue((keyType != null) ? keyType.FullName : null);
|
||||
|
||||
writer.WritePropertyName("Value");
|
||||
|
||||
if (keyType != null)
|
||||
{
|
||||
string valueJson;
|
||||
if (JsonSerializerInternalWriter.TryConvertToString(entityKeyMember.Value, keyType, out valueJson))
|
||||
writer.WriteValue(valueJson);
|
||||
else
|
||||
writer.WriteValue(entityKeyMember.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
private static void ReadAndAssertProperty(JsonReader reader, string propertyName)
|
||||
{
|
||||
ReadAndAssert(reader);
|
||||
|
||||
if (reader.TokenType != JsonToken.PropertyName || reader.Value.ToString() != propertyName)
|
||||
throw new JsonSerializationException("Expected JSON property '{0}'.".FormatWith(CultureInfo.InvariantCulture, propertyName));
|
||||
}
|
||||
|
||||
private static void ReadAndAssert(JsonReader reader)
|
||||
{
|
||||
if (!reader.Read())
|
||||
throw new JsonSerializationException("Unexpected end.");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(Activator.CreateInstance(objectType));
|
||||
|
||||
ReadAndAssertProperty(reader, "Key");
|
||||
ReadAndAssert(reader);
|
||||
entityKeyMember.Key = reader.Value.ToString();
|
||||
|
||||
ReadAndAssertProperty(reader, "Type");
|
||||
ReadAndAssert(reader);
|
||||
string type = reader.Value.ToString();
|
||||
|
||||
Type t = Type.GetType(type);
|
||||
|
||||
ReadAndAssertProperty(reader, "Value");
|
||||
ReadAndAssert(reader);
|
||||
entityKeyMember.Value = serializer.Deserialize(reader, t);
|
||||
|
||||
ReadAndAssert(reader);
|
||||
|
||||
return DynamicWrapper.GetUnderlyingObject(entityKeyMember);
|
||||
}
|
||||
|
||||
/// <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.AssignableToTypeName(EntityKeyMemberFullTypeName));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,140 +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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@@ -1,144 +1,144 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).
|
||||
/// </summary>
|
||||
public class IsoDateTimeConverter : DateTimeConverterBase
|
||||
{
|
||||
private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
|
||||
|
||||
private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
|
||||
private string _dateTimeFormat;
|
||||
private CultureInfo _culture;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date time styles used when converting a date to and from JSON.
|
||||
/// </summary>
|
||||
/// <value>The date time styles used when converting a date to and from JSON.</value>
|
||||
public DateTimeStyles DateTimeStyles
|
||||
{
|
||||
get { return _dateTimeStyles; }
|
||||
set { _dateTimeStyles = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date time format used when converting a date to and from JSON.
|
||||
/// </summary>
|
||||
/// <value>The date time format used when converting a date to and from JSON.</value>
|
||||
public string DateTimeFormat
|
||||
{
|
||||
get { return _dateTimeFormat ?? string.Empty; }
|
||||
set { _dateTimeFormat = StringUtils.NullEmptyString(value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the culture used when converting a date to and from JSON.
|
||||
/// </summary>
|
||||
/// <value>The culture used when converting a date to and from JSON.</value>
|
||||
public CultureInfo Culture
|
||||
{
|
||||
get { return _culture ?? CultureInfo.CurrentCulture; }
|
||||
set { _culture = value; }
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
string text;
|
||||
|
||||
if (value is DateTime)
|
||||
{
|
||||
DateTime dateTime = (DateTime)value;
|
||||
|
||||
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
|
||||
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
|
||||
dateTime = dateTime.ToUniversalTime();
|
||||
|
||||
text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
|
||||
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
|
||||
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
|
||||
dateTimeOffset = dateTimeOffset.ToUniversalTime();
|
||||
|
||||
text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)));
|
||||
}
|
||||
|
||||
writer.WriteValue(text);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
bool nullable = ReflectionUtils.IsNullableType(objectType);
|
||||
Type t = (nullable)
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullableType(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.Date)
|
||||
{
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset((DateTime)reader.Value);
|
||||
#endif
|
||||
|
||||
return reader.Value;
|
||||
}
|
||||
|
||||
if (reader.TokenType != JsonToken.String)
|
||||
throw new Exception("Unexpected token parsing date. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
string dateText = reader.Value.ToString();
|
||||
|
||||
if (string.IsNullOrEmpty(dateText) && nullable)
|
||||
return null;
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
|
||||
else
|
||||
return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
|
||||
else
|
||||
return DateTime.Parse(dateText, Culture, _dateTimeStyles);
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).
|
||||
/// </summary>
|
||||
public class IsoDateTimeConverter : DateTimeConverterBase
|
||||
{
|
||||
private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
|
||||
|
||||
private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
|
||||
private string _dateTimeFormat;
|
||||
private CultureInfo _culture;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date time styles used when converting a date to and from JSON.
|
||||
/// </summary>
|
||||
/// <value>The date time styles used when converting a date to and from JSON.</value>
|
||||
public DateTimeStyles DateTimeStyles
|
||||
{
|
||||
get { return _dateTimeStyles; }
|
||||
set { _dateTimeStyles = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date time format used when converting a date to and from JSON.
|
||||
/// </summary>
|
||||
/// <value>The date time format used when converting a date to and from JSON.</value>
|
||||
public string DateTimeFormat
|
||||
{
|
||||
get { return _dateTimeFormat ?? string.Empty; }
|
||||
set { _dateTimeFormat = StringUtils.NullEmptyString(value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the culture used when converting a date to and from JSON.
|
||||
/// </summary>
|
||||
/// <value>The culture used when converting a date to and from JSON.</value>
|
||||
public CultureInfo Culture
|
||||
{
|
||||
get { return _culture ?? CultureInfo.CurrentCulture; }
|
||||
set { _culture = value; }
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
string text;
|
||||
|
||||
if (value is DateTime)
|
||||
{
|
||||
DateTime dateTime = (DateTime)value;
|
||||
|
||||
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
|
||||
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
|
||||
dateTime = dateTime.ToUniversalTime();
|
||||
|
||||
text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
|
||||
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
|
||||
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
|
||||
dateTimeOffset = dateTimeOffset.ToUniversalTime();
|
||||
|
||||
text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)));
|
||||
}
|
||||
|
||||
writer.WriteValue(text);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
bool nullable = ReflectionUtils.IsNullableType(objectType);
|
||||
Type t = (nullable)
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullableType(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.Date)
|
||||
{
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset((DateTime)reader.Value);
|
||||
#endif
|
||||
|
||||
return reader.Value;
|
||||
}
|
||||
|
||||
if (reader.TokenType != JsonToken.String)
|
||||
throw new Exception("Unexpected token parsing date. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
string dateText = reader.Value.ToString();
|
||||
|
||||
if (string.IsNullOrEmpty(dateText) && nullable)
|
||||
return null;
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
|
||||
else
|
||||
return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
|
||||
else
|
||||
return DateTime.Parse(dateText, Culture, _dateTimeStyles);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +1,93 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> to and from a JavaScript date constructor (e.g. new Date(52231943)).
|
||||
/// </summary>
|
||||
public class JavaScriptDateTimeConverter : DateTimeConverterBase
|
||||
{
|
||||
/// <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)
|
||||
{
|
||||
long ticks;
|
||||
|
||||
if (value is DateTime)
|
||||
{
|
||||
DateTime dateTime = (DateTime)value;
|
||||
DateTime utcDateTime = dateTime.ToUniversalTime();
|
||||
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTime);
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
|
||||
DateTimeOffset utcDateTimeOffset = dateTimeOffset.ToUniversalTime();
|
||||
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTimeOffset.UtcDateTime);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new Exception("Expected date object value.");
|
||||
}
|
||||
|
||||
writer.WriteStartConstructor("Date");
|
||||
writer.WriteValue(ticks);
|
||||
writer.WriteEndConstructor();
|
||||
}
|
||||
|
||||
/// <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 property value of the JSON that is being converted.</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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullableType(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType != JsonToken.StartConstructor || !string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
|
||||
throw new Exception("Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
|
||||
|
||||
reader.Read();
|
||||
|
||||
if (reader.TokenType != JsonToken.Integer)
|
||||
throw new Exception("Unexpected token parsing date. Expected Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
long ticks = (long)reader.Value;
|
||||
|
||||
DateTime d = JsonConvert.ConvertJavaScriptTicksToDateTime(ticks);
|
||||
|
||||
reader.Read();
|
||||
|
||||
if (reader.TokenType != JsonToken.EndConstructor)
|
||||
throw new Exception("Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset(d);
|
||||
#endif
|
||||
|
||||
return d;
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> to and from a JavaScript date constructor (e.g. new Date(52231943)).
|
||||
/// </summary>
|
||||
public class JavaScriptDateTimeConverter : DateTimeConverterBase
|
||||
{
|
||||
/// <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)
|
||||
{
|
||||
long ticks;
|
||||
|
||||
if (value is DateTime)
|
||||
{
|
||||
DateTime dateTime = (DateTime)value;
|
||||
DateTime utcDateTime = dateTime.ToUniversalTime();
|
||||
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTime);
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
|
||||
DateTimeOffset utcDateTimeOffset = dateTimeOffset.ToUniversalTime();
|
||||
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTimeOffset.UtcDateTime);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new Exception("Expected date object value.");
|
||||
}
|
||||
|
||||
writer.WriteStartConstructor("Date");
|
||||
writer.WriteValue(ticks);
|
||||
writer.WriteEndConstructor();
|
||||
}
|
||||
|
||||
/// <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 property value of the JSON that is being converted.</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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullableType(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType != JsonToken.StartConstructor || !string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
|
||||
throw new Exception("Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
|
||||
|
||||
reader.Read();
|
||||
|
||||
if (reader.TokenType != JsonToken.Integer)
|
||||
throw new Exception("Unexpected token parsing date. Expected Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
long ticks = (long)reader.Value;
|
||||
|
||||
DateTime d = JsonConvert.ConvertJavaScriptTicksToDateTime(ticks);
|
||||
|
||||
reader.Read();
|
||||
|
||||
if (reader.TokenType != JsonToken.EndConstructor)
|
||||
throw new Exception("Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset(d);
|
||||
#endif
|
||||
|
||||
return d;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,108 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="KeyValuePair{TKey,TValue}"/> to and from JSON.
|
||||
/// </summary>
|
||||
public class KeyValuePairConverter : 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)
|
||||
{
|
||||
Type t = value.GetType();
|
||||
PropertyInfo keyProperty = t.GetProperty("Key");
|
||||
PropertyInfo valueProperty = t.GetProperty("Value");
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Key");
|
||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(keyProperty, value));
|
||||
writer.WritePropertyName("Value");
|
||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(valueProperty, value));
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
bool isNullable = ReflectionUtils.IsNullableType(objectType);
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!isNullable)
|
||||
throw new Exception("Could not deserialize Null to KeyValuePair.");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Type t = (isNullable)
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
IList<Type> genericArguments = t.GetGenericArguments();
|
||||
Type keyType = genericArguments[0];
|
||||
Type valueType = genericArguments[1];
|
||||
|
||||
object key = null;
|
||||
object value = null;
|
||||
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
switch (reader.Value.ToString())
|
||||
{
|
||||
case "Key":
|
||||
reader.Read();
|
||||
key = serializer.Deserialize(reader, keyType);
|
||||
break;
|
||||
case "Value":
|
||||
reader.Read();
|
||||
value = serializer.Deserialize(reader, valueType);
|
||||
break;
|
||||
default:
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
return ReflectionUtils.CreateInstance(t, key, value);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (t.IsValueType() && t.IsGenericType())
|
||||
return (t.GetGenericTypeDefinition() == typeof(KeyValuePair<,>));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="KeyValuePair{TKey,TValue}"/> to and from JSON.
|
||||
/// </summary>
|
||||
public class KeyValuePairConverter : 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)
|
||||
{
|
||||
Type t = value.GetType();
|
||||
PropertyInfo keyProperty = t.GetProperty("Key");
|
||||
PropertyInfo valueProperty = t.GetProperty("Value");
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Key");
|
||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(keyProperty, value));
|
||||
writer.WritePropertyName("Value");
|
||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(valueProperty, value));
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
bool isNullable = ReflectionUtils.IsNullableType(objectType);
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!isNullable)
|
||||
throw new Exception("Could not deserialize Null to KeyValuePair.");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Type t = (isNullable)
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
IList<Type> genericArguments = t.GetGenericArguments();
|
||||
Type keyType = genericArguments[0];
|
||||
Type valueType = genericArguments[1];
|
||||
|
||||
object key = null;
|
||||
object value = null;
|
||||
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
switch (reader.Value.ToString())
|
||||
{
|
||||
case "Key":
|
||||
reader.Read();
|
||||
key = serializer.Deserialize(reader, keyType);
|
||||
break;
|
||||
case "Value":
|
||||
reader.Read();
|
||||
value = serializer.Deserialize(reader, valueType);
|
||||
break;
|
||||
default:
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
return ReflectionUtils.CreateInstance(t, key, value);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (t.IsValueType() && t.IsGenericType())
|
||||
return (t.GetGenericTypeDefinition() == typeof(KeyValuePair<,>));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,149 +1,149 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="Regex"/> to and from JSON and BSON.
|
||||
/// </summary>
|
||||
public class RegexConverter : 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)
|
||||
{
|
||||
Regex regex = (Regex) value;
|
||||
|
||||
BsonWriter bsonWriter = writer as BsonWriter;
|
||||
if (bsonWriter != null)
|
||||
WriteBson(bsonWriter, regex);
|
||||
else
|
||||
WriteJson(writer, regex);
|
||||
}
|
||||
|
||||
private bool HasFlag(RegexOptions options, RegexOptions flag)
|
||||
{
|
||||
return ((options & flag) == flag);
|
||||
}
|
||||
|
||||
private void WriteBson(BsonWriter writer, Regex regex)
|
||||
{
|
||||
// Regular expression - The first cstring is the regex pattern, the second
|
||||
// is the regex options string. Options are identified by characters, which
|
||||
// must be stored in alphabetical order. Valid options are 'i' for case
|
||||
// insensitive matching, 'm' for multiline matching, 'x' for verbose mode,
|
||||
// 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode
|
||||
// ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.
|
||||
|
||||
string options = null;
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
|
||||
options += "i";
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.Multiline))
|
||||
options += "m";
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.Singleline))
|
||||
options += "s";
|
||||
|
||||
options += "u";
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
|
||||
options += "x";
|
||||
|
||||
writer.WriteRegex(regex.ToString(), options);
|
||||
}
|
||||
|
||||
private void WriteJson(JsonWriter writer, Regex regex)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Pattern");
|
||||
writer.WriteValue(regex.ToString());
|
||||
writer.WritePropertyName("Options");
|
||||
writer.WriteValue(regex.Options);
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
BsonReader bsonReader = reader as BsonReader;
|
||||
|
||||
if (bsonReader != null)
|
||||
return ReadBson(bsonReader);
|
||||
else
|
||||
return ReadJson(reader);
|
||||
}
|
||||
|
||||
private object ReadBson(BsonReader reader)
|
||||
{
|
||||
string regexText = (string)reader.Value;
|
||||
int patternOptionDelimiterIndex = regexText.LastIndexOf('/');
|
||||
|
||||
string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
|
||||
string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
|
||||
|
||||
RegexOptions options = RegexOptions.None;
|
||||
foreach (char c in optionsText)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'i':
|
||||
options |= RegexOptions.IgnoreCase;
|
||||
break;
|
||||
case 'm':
|
||||
options |= RegexOptions.Multiline;
|
||||
break;
|
||||
case 's':
|
||||
options |= RegexOptions.Singleline;
|
||||
break;
|
||||
case 'x':
|
||||
options |= RegexOptions.ExplicitCapture;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new Regex(patternText, options);
|
||||
}
|
||||
|
||||
private Regex ReadJson(JsonReader reader)
|
||||
{
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
string pattern = (string)reader.Value;
|
||||
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
int options = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
|
||||
|
||||
reader.Read();
|
||||
|
||||
return new Regex(pattern, (RegexOptions) options);
|
||||
}
|
||||
|
||||
/// <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 (Regex));
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="Regex"/> to and from JSON and BSON.
|
||||
/// </summary>
|
||||
public class RegexConverter : 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)
|
||||
{
|
||||
Regex regex = (Regex) value;
|
||||
|
||||
BsonWriter bsonWriter = writer as BsonWriter;
|
||||
if (bsonWriter != null)
|
||||
WriteBson(bsonWriter, regex);
|
||||
else
|
||||
WriteJson(writer, regex);
|
||||
}
|
||||
|
||||
private bool HasFlag(RegexOptions options, RegexOptions flag)
|
||||
{
|
||||
return ((options & flag) == flag);
|
||||
}
|
||||
|
||||
private void WriteBson(BsonWriter writer, Regex regex)
|
||||
{
|
||||
// Regular expression - The first cstring is the regex pattern, the second
|
||||
// is the regex options string. Options are identified by characters, which
|
||||
// must be stored in alphabetical order. Valid options are 'i' for case
|
||||
// insensitive matching, 'm' for multiline matching, 'x' for verbose mode,
|
||||
// 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode
|
||||
// ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.
|
||||
|
||||
string options = null;
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
|
||||
options += "i";
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.Multiline))
|
||||
options += "m";
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.Singleline))
|
||||
options += "s";
|
||||
|
||||
options += "u";
|
||||
|
||||
if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
|
||||
options += "x";
|
||||
|
||||
writer.WriteRegex(regex.ToString(), options);
|
||||
}
|
||||
|
||||
private void WriteJson(JsonWriter writer, Regex regex)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Pattern");
|
||||
writer.WriteValue(regex.ToString());
|
||||
writer.WritePropertyName("Options");
|
||||
writer.WriteValue(regex.Options);
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
BsonReader bsonReader = reader as BsonReader;
|
||||
|
||||
if (bsonReader != null)
|
||||
return ReadBson(bsonReader);
|
||||
else
|
||||
return ReadJson(reader);
|
||||
}
|
||||
|
||||
private object ReadBson(BsonReader reader)
|
||||
{
|
||||
string regexText = (string)reader.Value;
|
||||
int patternOptionDelimiterIndex = regexText.LastIndexOf('/');
|
||||
|
||||
string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
|
||||
string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
|
||||
|
||||
RegexOptions options = RegexOptions.None;
|
||||
foreach (char c in optionsText)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'i':
|
||||
options |= RegexOptions.IgnoreCase;
|
||||
break;
|
||||
case 'm':
|
||||
options |= RegexOptions.Multiline;
|
||||
break;
|
||||
case 's':
|
||||
options |= RegexOptions.Singleline;
|
||||
break;
|
||||
case 'x':
|
||||
options |= RegexOptions.ExplicitCapture;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new Regex(patternText, options);
|
||||
}
|
||||
|
||||
private Regex ReadJson(JsonReader reader)
|
||||
{
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
string pattern = (string)reader.Value;
|
||||
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
int options = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
|
||||
|
||||
reader.Read();
|
||||
|
||||
return new Regex(pattern, (RegexOptions) options);
|
||||
}
|
||||
|
||||
/// <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 (Regex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,201 +1,201 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts an <see cref="Enum"/> to and from its name string value.
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
/// Converts an <see cref="Enum"/> to and from its name string value.
|
||||
/// </summary>
|
||||
public class StringEnumConverter : JsonConverter
|
||||
{
|
||||
private readonly Dictionary<Type, BidirectionalDictionary<string, string>> _enumMemberNamesPerType = new Dictionary<Type, BidirectionalDictionary<string, string>>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the written enum text should be camel case.
|
||||
/// </summary>
|
||||
/// <value><c>true</c> if the written enum text will be camel case; otherwise, <c>false</c>.</value>
|
||||
public bool CamelCaseText { get; set; }
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
return;
|
||||
}
|
||||
|
||||
Enum e = (Enum)value;
|
||||
|
||||
string enumName = e.ToString("G");
|
||||
|
||||
if (char.IsNumber(enumName[0]) || enumName[0] == '-')
|
||||
{
|
||||
writer.WriteValue(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
BidirectionalDictionary<string, string> map = GetEnumNameMap(e.GetType());
|
||||
|
||||
string resolvedEnumName;
|
||||
map.TryGetByFirst(enumName, out resolvedEnumName);
|
||||
resolvedEnumName = resolvedEnumName ?? enumName;
|
||||
|
||||
if (CamelCaseText)
|
||||
{
|
||||
string[] names = resolvedEnumName.Split(',').Select(item => StringUtils.ToCamelCase(item.Trim())).ToArray();
|
||||
resolvedEnumName = string.Join(", ", names);
|
||||
}
|
||||
|
||||
writer.WriteValue(resolvedEnumName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullableType(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
var map = GetEnumNameMap(t);
|
||||
string resolvedEnumName;
|
||||
map.TryGetBySecond(reader.Value.ToString(), out resolvedEnumName);
|
||||
resolvedEnumName = resolvedEnumName ?? reader.Value.ToString();
|
||||
|
||||
return Enum.Parse(t, resolvedEnumName, true);
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.Integer)
|
||||
return ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, t);
|
||||
|
||||
throw new Exception("Unexpected token when parsing enum. Expected String or Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A cached representation of the Enum string representation to respect per Enum field name.
|
||||
/// </summary>
|
||||
/// <param name="t">The type of the Enum.</param>
|
||||
/// <returns>A map of enum field name to either the field name, or the configured enum member name (<see cref="EnumMemberAttribute"/>).</returns>
|
||||
private BidirectionalDictionary<string, string> GetEnumNameMap(Type t)
|
||||
{
|
||||
BidirectionalDictionary<string, string> map;
|
||||
|
||||
if (!_enumMemberNamesPerType.TryGetValue(t, out map))
|
||||
{
|
||||
lock (_enumMemberNamesPerType)
|
||||
{
|
||||
if (_enumMemberNamesPerType.TryGetValue(t, out map))
|
||||
return map;
|
||||
|
||||
map = new BidirectionalDictionary<string, string>(
|
||||
StringComparer.OrdinalIgnoreCase,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (FieldInfo f in t.GetFields())
|
||||
{
|
||||
string n1 = f.Name;
|
||||
string n2;
|
||||
|
||||
#if !NET20
|
||||
n2 = f.GetCustomAttributes(typeof (EnumMemberAttribute), true)
|
||||
.Cast<EnumMemberAttribute>()
|
||||
.Select(a => a.Value)
|
||||
.SingleOrDefault() ?? f.Name;
|
||||
#else
|
||||
n2 = f.Name;
|
||||
#endif
|
||||
|
||||
string s;
|
||||
if (map.TryGetBySecond(n2, out s))
|
||||
{
|
||||
throw new Exception("Enum name '{0}' already exists on enum '{1}'."
|
||||
.FormatWith(CultureInfo.InvariantCulture, n2, t.Name));
|
||||
}
|
||||
|
||||
map.Add(n1, n2);
|
||||
}
|
||||
|
||||
_enumMemberNamesPerType[t] = map;
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
return t.IsEnum();
|
||||
}
|
||||
}
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts an <see cref="Enum"/> to and from its name string value.
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
/// Converts an <see cref="Enum"/> to and from its name string value.
|
||||
/// </summary>
|
||||
public class StringEnumConverter : JsonConverter
|
||||
{
|
||||
private readonly Dictionary<Type, BidirectionalDictionary<string, string>> _enumMemberNamesPerType = new Dictionary<Type, BidirectionalDictionary<string, string>>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the written enum text should be camel case.
|
||||
/// </summary>
|
||||
/// <value><c>true</c> if the written enum text will be camel case; otherwise, <c>false</c>.</value>
|
||||
public bool CamelCaseText { get; set; }
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
return;
|
||||
}
|
||||
|
||||
Enum e = (Enum)value;
|
||||
|
||||
string enumName = e.ToString("G");
|
||||
|
||||
if (char.IsNumber(enumName[0]) || enumName[0] == '-')
|
||||
{
|
||||
writer.WriteValue(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
BidirectionalDictionary<string, string> map = GetEnumNameMap(e.GetType());
|
||||
|
||||
string resolvedEnumName;
|
||||
map.TryGetByFirst(enumName, out resolvedEnumName);
|
||||
resolvedEnumName = resolvedEnumName ?? enumName;
|
||||
|
||||
if (CamelCaseText)
|
||||
{
|
||||
string[] names = resolvedEnumName.Split(',').Select(item => StringUtils.ToCamelCase(item.Trim())).ToArray();
|
||||
resolvedEnumName = string.Join(", ", names);
|
||||
}
|
||||
|
||||
writer.WriteValue(resolvedEnumName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
if (!ReflectionUtils.IsNullableType(objectType))
|
||||
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
var map = GetEnumNameMap(t);
|
||||
string resolvedEnumName;
|
||||
map.TryGetBySecond(reader.Value.ToString(), out resolvedEnumName);
|
||||
resolvedEnumName = resolvedEnumName ?? reader.Value.ToString();
|
||||
|
||||
return Enum.Parse(t, resolvedEnumName, true);
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.Integer)
|
||||
return ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, t);
|
||||
|
||||
throw new Exception("Unexpected token when parsing enum. Expected String or Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A cached representation of the Enum string representation to respect per Enum field name.
|
||||
/// </summary>
|
||||
/// <param name="t">The type of the Enum.</param>
|
||||
/// <returns>A map of enum field name to either the field name, or the configured enum member name (<see cref="EnumMemberAttribute"/>).</returns>
|
||||
private BidirectionalDictionary<string, string> GetEnumNameMap(Type t)
|
||||
{
|
||||
BidirectionalDictionary<string, string> map;
|
||||
|
||||
if (!_enumMemberNamesPerType.TryGetValue(t, out map))
|
||||
{
|
||||
lock (_enumMemberNamesPerType)
|
||||
{
|
||||
if (_enumMemberNamesPerType.TryGetValue(t, out map))
|
||||
return map;
|
||||
|
||||
map = new BidirectionalDictionary<string, string>(
|
||||
StringComparer.OrdinalIgnoreCase,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (FieldInfo f in t.GetFields())
|
||||
{
|
||||
string n1 = f.Name;
|
||||
string n2;
|
||||
|
||||
#if !NET20
|
||||
n2 = f.GetCustomAttributes(typeof (EnumMemberAttribute), true)
|
||||
.Cast<EnumMemberAttribute>()
|
||||
.Select(a => a.Value)
|
||||
.SingleOrDefault() ?? f.Name;
|
||||
#else
|
||||
n2 = f.Name;
|
||||
#endif
|
||||
|
||||
string s;
|
||||
if (map.TryGetBySecond(n2, out s))
|
||||
{
|
||||
throw new Exception("Enum name '{0}' already exists on enum '{1}'."
|
||||
.FormatWith(CultureInfo.InvariantCulture, n2, t.Name));
|
||||
}
|
||||
|
||||
map.Add(n1, n2);
|
||||
}
|
||||
|
||||
_enumMemberNamesPerType[t] = map;
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
return t.IsEnum();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,106 +1,106 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="Version"/> to and from a string (e.g. "1.2.3.4").
|
||||
/// </summary>
|
||||
public class VersionConverter : 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)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
else if (value is Version)
|
||||
{
|
||||
writer.WriteValue(value.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Expected Version object value");
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 property value of the JSON that is being converted.</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)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
try
|
||||
{
|
||||
Version v = new Version((string) reader.Value);
|
||||
return v;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error parsing version string: {0}".FormatWith(CultureInfo.InvariantCulture, reader.Value), ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected token or value when parsing version. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 (Version);
|
||||
}
|
||||
}
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="Version"/> to and from a string (e.g. "1.2.3.4").
|
||||
/// </summary>
|
||||
public class VersionConverter : 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)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
else if (value is Version)
|
||||
{
|
||||
writer.WriteValue(value.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Expected Version object value");
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 property value of the JSON that is being converted.</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)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
try
|
||||
{
|
||||
Version v = new Version((string) reader.Value);
|
||||
return v;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error parsing version string: {0}".FormatWith(CultureInfo.InvariantCulture, reader.Value), ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected token or value when parsing version. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 (Version);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user