setting all protected methods on WizardStepPage as virtual
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / NewtonsoftJSONSerializer.cs
blobe33c4df7e340ced628b3f55665fcd9fe8ee48d27
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Services
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using Newtonsoft.Json;
22 /// <summary>
23 /// Pendent
24 /// </summary>
25 public class NewtonsoftJSONSerializer : IJSONSerializer
27 /// <summary>
28 /// Serializes the specified object.
29 /// </summary>
30 /// <param name="target">The object.</param>
31 /// <returns></returns>
32 public string Serialize(object target)
34 return JavaScriptConvert.SerializeObject(target);
37 /// <summary>
38 /// Serializes the specified object.
39 /// </summary>
40 /// <param name="target">The object.</param>
41 /// <param name="converters">The converters.</param>
42 /// <returns></returns>
43 public string Serialize(object target, params IJSONConverter[] converters)
45 List<JsonConverter> adapters = new List<JsonConverter>();
47 if (converters != null)
49 foreach(IJSONConverter converter in converters)
51 adapters.Add(new JsonConverterAdapter(converter));
55 return JavaScriptConvert.SerializeObject(target, adapters.ToArray());
58 /// <summary>
59 /// Serializes the specified object.
60 /// </summary>
61 /// <param name="target">The target.</param>
62 /// <param name="writer">The writer.</param>
63 /// <param name="converters">The converters.</param>
64 public void Serialize(object target, TextWriter writer, params IJSONConverter[] converters)
66 List<JsonConverter> adapters = new List<JsonConverter>();
68 if (converters != null)
70 foreach(IJSONConverter converter in converters)
72 adapters.Add(new JsonConverterAdapter(converter));
76 #if DOTNET35
77 writer.Write(JavaScriptConvert.SerializeObject(target, adapters.ToArray()));
78 #else
79 JavaScriptConvert.SerializeObject(target, writer, adapters.ToArray());
80 #endif
83 /// <summary>
84 /// Deserializes the specified object.
85 /// </summary>
86 /// <param name="jsonString">The json representation of an object string.</param>
87 /// <param name="expectedType">The expected type.</param>
88 /// <returns></returns>
89 public object Deserialize(string jsonString, Type expectedType)
91 return JavaScriptConvert.DeserializeObject(jsonString, expectedType);
94 /// <summary>
95 /// Deserializes the specified object.
96 /// </summary>
97 /// <typeparam name="T"></typeparam>
98 /// <param name="jsonString">The json representation of an object string.</param>
99 /// <returns></returns>
100 public T Deserialize<T>(string jsonString)
102 return (T) JavaScriptConvert.DeserializeObject(jsonString, typeof(T));
105 /// <summary>
106 /// Deserializes the specified object.
107 /// </summary>
108 /// <typeparam name="T"></typeparam>
109 /// <param name="jsonString">The json representation of an object string.</param>
110 /// <returns></returns>
111 public T[] DeserializeArray<T>(string jsonString)
113 return (T[]) JavaScriptConvert.DeserializeObject(jsonString, typeof(T));
116 class JsonConverterAdapter : JsonConverter
118 private readonly IJSONConverter converter;
120 public JsonConverterAdapter(IJSONConverter converter)
122 this.converter = converter;
125 public override void WriteJson(JsonWriter writer, object value)
127 converter.Write(new JSONWriterAdapter(writer), value);
130 public override bool CanConvert(Type objectType)
132 return converter.CanHandle(objectType);
135 #if DOTNET35
136 public override object ReadJson(JsonReader reader, Type objectType)
138 return converter.ReadJson(new JSONReaderAdapter(reader), objectType);
140 #endif
143 class JSONWriterAdapter : IJSONWriter
145 private readonly JsonWriter writer;
147 public JSONWriterAdapter(JsonWriter writer)
149 this.writer = writer;
152 public JsonWriter Writer
154 get { return writer; }
157 public void WriteValue(object value)
159 new JsonSerializer().Serialize(writer, value);
162 public void WriteStartObject()
164 writer.WriteStartObject();
167 public void WriteEndObject()
169 writer.WriteEndObject();
172 public void WriteStartArray()
174 writer.WriteStartArray();
177 public void WriteEndArray()
179 writer.WriteEndArray();
182 public void WritePropertyName(string name)
184 writer.WritePropertyName(name);
187 public void WriteEnd()
189 writer.WriteEnd();
192 public void WriteNull()
194 writer.WriteNull();
197 public void WriteUndefined()
199 writer.WriteUndefined();
202 public void WriteRaw(string javaScript)
204 writer.WriteRaw(javaScript);
207 public void WriteValue(string value)
209 writer.WriteValue(value);
212 public void WriteValue(int value)
214 writer.WriteValue(value);
217 public void WriteValue(long value)
219 writer.WriteValue(value);
222 public void WriteValue(float value)
224 writer.WriteValue(value);
227 public void WriteValue(bool value)
229 writer.WriteValue(value);
232 public void WriteValue(short value)
234 writer.WriteValue(value);
237 public void WriteValue(decimal value)
239 writer.WriteValue(value);
242 public void WriteValue(DateTime value)
244 writer.WriteValue(value);
248 #if DOTNET35
249 class JSONReaderAdapter : IJSONReader
251 private readonly JsonReader reader;
253 public JSONReaderAdapter(JsonReader reader)
255 this.reader = reader;
258 public JsonReader Reader
260 get { return reader; }
263 public char QuoteChar
265 get { return reader.QuoteChar; }
268 public object Value
270 get { return reader.Value; }
273 public Type ValueType
275 get { return reader.ValueType; }
278 public int Depth
280 get { return reader.Depth; }
283 public bool Read()
285 return reader.Read();
288 public void Skip()
290 reader.Skip();
293 public void Close()
295 reader.Close();
298 #endif