Added ProxyOptions to Windsor so allow facilities to easily add proxy interfaces...
[castle.git] / MonoRail / Castle.Monorail.JSONSupport / JSONBinderAttribute.cs
blob2fd593250983597863b8cc3079e60fdb05e039b3
1 // Copyright 2004-2006 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.JSONSupport
17 using System;
18 using System.Reflection;
19 using Castle.MonoRail.Framework;
20 using Newtonsoft.Json;
22 /// <summary>
23 /// Extends <see cref="DataBindAttribute"/> with
24 /// the <see cref="JavaScriptConvert"/> functionality.
25 /// </summary>
26 [AttributeUsage(AttributeTargets.Parameter)]
27 public class JSONBinderAttribute : Attribute, IParameterBinder
29 private readonly string entryKey;
31 /// <summary>
32 /// Initializes a new instance of the <see cref="JSONBinderAttribute"/> class.
33 /// </summary>
34 /// <param name="entryKey">The entry key, which is the form or
35 /// querystring key that identifies the JSON persisted content</param>
36 public JSONBinderAttribute(string entryKey)
38 if (entryKey == null) throw new ArgumentNullException("entryKey");
40 this.entryKey = entryKey;
43 public int CalculateParamPoints(SmartDispatcherController controller, ParameterInfo parameterInfo)
45 return controller.Params[entryKey] != null ? 1 : 0;
48 public object Bind(SmartDispatcherController controller, ParameterInfo parameterInfo)
50 string entryValue = controller.Params[entryKey];
52 return Bind(entryValue, parameterInfo.ParameterType);
55 public static object Bind(string entryValue, Type parameterType)
57 return JavaScriptConvert.DeserializeObject(entryValue, parameterType);