1 // Copyright 2004-2008 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
.Framework
18 using System
.Reflection
;
19 using Castle
.MonoRail
.Framework
;
20 using Castle
.MonoRail
.Framework
.Services
;
23 /// Enables binding of JSON formatted values on POCO objects.
27 /// The following demonstrates how to bind a JSON querystring value representing a Car object instance
28 /// to a POCO Car object instance:
32 /// car={Wheels=4,Year=2007,Model='Cheap'}
34 /// And you want to bind those values to a instance of yours Car class, which looks like this:
38 /// private int wheels, year;
39 /// private string model;
43 /// get { return wheels; }
44 /// set { wheels = value; }
49 /// get { return year; }
50 /// set { year = value; }
53 /// public string Model
55 /// get { return model; }
56 /// set { model = value; }
60 /// <para>Using the <see cref="JSONBinderAttribute"/> and the <see cref="SmartDispatcherController"/>, all you have to
61 /// do is to mark the method parameter with the attribute, like the following example:</para>
63 /// public void MyAction([JSONBinder("car")] Car car)
66 [AttributeUsage(AttributeTargets
.Parameter
, AllowMultiple
= false, Inherited
= false)]
67 public class JSONBinderAttribute
: Attribute
, IParameterBinder
69 private string entryKey
;
72 /// Initializes a new instance of the <see cref="JSONBinderAttribute"/> class.
74 public JSONBinderAttribute()
79 /// Initializes a new instance of the <see cref="JSONBinderAttribute"/> class.
81 /// <param name="entryKey">The entry key, which is the form or
82 /// querystring key that identifies the JSON persisted content</param>
83 public JSONBinderAttribute(string entryKey
)
85 if (entryKey
== null) throw new ArgumentNullException("entryKey");
87 this.entryKey
= entryKey
;
91 /// Calculates the param points. Implementors should return value equals or greater than
92 /// zero indicating whether the parameter can be bound successfully. The greater the value (points)
93 /// the more successful the implementation indicates to the framework
95 /// <param name="context">The context.</param>
96 /// <param name="controller">The controller.</param>
97 /// <param name="controllerContext">The controller context.</param>
98 /// <param name="parameterInfo">The parameter info.</param>
99 /// <returns></returns>
100 public int CalculateParamPoints(IEngineContext context
, IController controller
,
101 IControllerContext controllerContext
, ParameterInfo parameterInfo
)
103 EnsureValidEntryKey(parameterInfo
.Name
);
105 return context
.Request
.Params
[entryKey
] != null ? 1 : 0;
109 /// Binds the specified parameter for the action.
111 /// <param name="context">The context.</param>
112 /// <param name="controller">The controller.</param>
113 /// <param name="controllerContext">The controller context.</param>
114 /// <param name="parameterInfo">The parameter info.</param>
116 /// A instance based on the JSON values present in the <c>EntryKey</c> or the parameter name.
118 public object Bind(IEngineContext context
, IController controller
, IControllerContext controllerContext
, ParameterInfo parameterInfo
)
120 EnsureValidEntryKey(parameterInfo
.Name
);
122 string entryValue
= context
.Request
.Params
[entryKey
];
124 IJSONSerializer serializer
= context
.Services
.JSONSerializer
;
126 return serializer
.Deserialize(entryValue
, parameterInfo
.ParameterType
);
129 private void EnsureValidEntryKey(string name
)
131 if (entryKey
== null)