1 // Copyright 2004-2006 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Reflection
;
19 using Castle
.MonoRail
.Framework
;
20 using Newtonsoft
.Json
;
23 /// Extends <see cref="DataBindAttribute"/> with
24 /// the <see cref="JavaScriptConvert"/> functionality.
26 [AttributeUsage(AttributeTargets
.Parameter
)]
27 public class JSONBinderAttribute
: Attribute
, IParameterBinder
29 private readonly string entryKey
;
32 /// Initializes a new instance of the <see cref="JSONBinderAttribute"/> class.
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
);