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 !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
29
using System.Collections.Generic;
31
using System.Globalization;
34
using Newtonsoft.Json.Utilities;
36
namespace Newtonsoft.Json.Converters
39
/// Converts an ExpandoObject to and from JSON.
41
public class ExpandoObjectConverter : JsonConverter
44
/// Writes the JSON representation of the object.
46
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
47
/// <param name="value">The value.</param>
48
/// <param name="serializer">The calling serializer.</param>
49
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
51
// can write is set to false
55
/// Reads the JSON representation of the object.
57
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
58
/// <param name="objectType">Type of the object.</param>
59
/// <param name="existingValue">The existing value of object being read.</param>
60
/// <param name="serializer">The calling serializer.</param>
61
/// <returns>The object value.</returns>
62
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
64
return ReadValue(reader);
67
private object ReadValue(JsonReader reader)
69
while (reader.TokenType == JsonToken.Comment)
72
throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
75
switch (reader.TokenType)
77
case JsonToken.StartObject:
78
return ReadObject(reader);
79
case JsonToken.StartArray:
80
return ReadList(reader);
82
if (JsonReader.IsPrimitiveToken(reader.TokenType))
85
throw JsonSerializationException.Create(reader, "Unexpected token when converting ExpandoObject: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
89
private object ReadList(JsonReader reader)
91
IList<object> list = new List<object>();
95
switch (reader.TokenType)
97
case JsonToken.Comment:
100
object v = ReadValue(reader);
104
case JsonToken.EndArray:
109
throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
112
private object ReadObject(JsonReader reader)
114
IDictionary<string, object> expandoObject = new ExpandoObject();
116
while (reader.Read())
118
switch (reader.TokenType)
120
case JsonToken.PropertyName:
121
string propertyName = reader.Value.ToString();
124
throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
126
object v = ReadValue(reader);
128
expandoObject[propertyName] = v;
130
case JsonToken.Comment:
132
case JsonToken.EndObject:
133
return expandoObject;
137
throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
141
/// Determines whether this instance can convert the specified object type.
143
/// <param name="objectType">Type of the object.</param>
145
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
147
public override bool CanConvert(Type objectType)
149
return (objectType == typeof (ExpandoObject));
153
/// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
156
/// <c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.
158
public override bool CanWrite
160
get { return false; }
b'\\ No newline at end of file'