2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
26
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
28
using System.Globalization;
29
using System.Runtime.Serialization;
30
using Newtonsoft.Json.Utilities;
31
using Newtonsoft.Json.Linq;
33
namespace Newtonsoft.Json.Serialization
35
internal class JsonFormatterConverter : IFormatterConverter
37
private readonly JsonSerializer _serializer;
39
public JsonFormatterConverter(JsonSerializer serializer)
41
ValidationUtils.ArgumentNotNull(serializer, "serializer");
43
_serializer = serializer;
46
private T GetTokenValue<T>(object value)
48
ValidationUtils.ArgumentNotNull(value, "value");
50
JValue v = (JValue)value;
51
return (T)System.Convert.ChangeType(v.Value, typeof(T), CultureInfo.InvariantCulture);
54
public object Convert(object value, Type type)
56
ValidationUtils.ArgumentNotNull(value, "value");
58
JToken token = value as JToken;
60
throw new ArgumentException("Value is not a JToken.", "value");
62
return _serializer.Deserialize(token.CreateReader(), type);
65
public object Convert(object value, TypeCode typeCode)
67
ValidationUtils.ArgumentNotNull(value, "value");
70
value = ((JValue) value).Value;
72
return System.Convert.ChangeType(value, typeCode, CultureInfo.InvariantCulture);
75
public bool ToBoolean(object value)
77
return GetTokenValue<bool>(value);
80
public byte ToByte(object value)
82
return GetTokenValue<byte>(value);
85
public char ToChar(object value)
87
return GetTokenValue<char>(value);
90
public DateTime ToDateTime(object value)
92
return GetTokenValue<DateTime>(value);
95
public decimal ToDecimal(object value)
97
return GetTokenValue<decimal>(value);
100
public double ToDouble(object value)
102
return GetTokenValue<double>(value);
105
public short ToInt16(object value)
107
return GetTokenValue<short>(value);
110
public int ToInt32(object value)
112
return GetTokenValue<int>(value);
115
public long ToInt64(object value)
117
return GetTokenValue<long>(value);
120
public sbyte ToSByte(object value)
122
return GetTokenValue<sbyte>(value);
125
public float ToSingle(object value)
127
return GetTokenValue<float>(value);
130
public string ToString(object value)
132
return GetTokenValue<string>(value);
135
public ushort ToUInt16(object value)
137
return GetTokenValue<ushort>(value);
140
public uint ToUInt32(object value)
142
return GetTokenValue<uint>(value);
145
public ulong ToUInt64(object value)
147
return GetTokenValue<ulong>(value);
b'\\ No newline at end of file'