AFAIK no,
at least not via configuration.
I think that the point is that many of the developers that use this
library
use it as a one way method to pass objects from the server to the
client.
and know what to expect there.
You can create your own custom converter that will not emit the _type.
pesonally I have created
the following class:
public class RestrictedCustomConverter
:AjaxPro.IJavaScriptConverter {
private string m_fieldsToSerialize;
private string[] m_arrayOfFieldsToSerialize;
public RestrictedCustomConverter(Type[] serializableTypes,
string fieldsToSerialize):this(serializableTypes,new
Type[0]{},fieldsToSerialize) {
}
public RestrictedCustomConverter(Type[] serializableTypes,
Type[] deserializableTypes, string fieldsToSerialize) {
base.m_serializableTypes = serializableTypes;
base.m_deserializableTypes = deserializableTypes;
this.m_fieldsToSerialize = fieldsToSerialize;
this.m_arrayOfFieldsToSerialize =
fieldsToSerialize.Split(',');
}
private bool CanDeserialize(Type t) {
return Array.IndexOf(this.DeserializableTypes,t) !=-1 ;
}
public override string GetClientScript() {
StringBuilder sb = new StringBuilder();
foreach (Type t in SerializableTypes) {
sb.AppendFormat(@"var nsParts = '{0}'.split('.');
var root = window;
for(var i=0; i<nsParts.length; i++) {{
if(typeof root[nsParts[i]] == 'undefined')
root[nsParts[i]] = {{}};
root = root[nsParts[i]];
}}
",t.Namespace);
sb.AppendFormat(@"{0}=function({1})
{{",t.FullName,this.m_fieldsToSerialize);
if (this.CanDeserialize(t)) {
sb.AppendFormat(@"this.__type = ""{0}"";",
t.FullName,
AjaxPro.JavaScriptSerializer.Serialize(t.AssemblyQualifiedName));
}
sb.AppendFormat(@"
var flds='{0}'.split(',');
var fld;
for(var i=0;fld=flds[i];i++) {{
eval('this.'+fld+'='+fld);
}}
}}",this.m_fieldsToSerialize);
}
return sb.ToString();
}
public override string Serialize(object o) {
Type t = o.GetType();
StringBuilder builder1 = new StringBuilder();
bool flag1 = true;
builder1.AppendFormat("new {0}(",t.FullName);
foreach (string fieldName in
this.m_arrayOfFieldsToSerialize) {
System.Reflection.FieldInfo fi= null;
fi=
t.GetField(fieldName,System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance ); //FieldInfo
if (fi != null) {
if (flag1) { flag1 = false; } else {
builder1.Append(','); }
builder1.Append(AjaxPro.JavaScriptSerializer.Serialize(fi.GetValue(o)));
}
else {
System.Reflection.PropertyInfo pi =
t.GetProperty(fieldName, System.Reflection.BindingFlags.GetProperty |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance); // PropertyInfo
if (pi != null) {
System.Reflection.MethodInfo mi =
pi.GetGetMethod();
if (mi.GetParameters().Length <= 0) {
if (flag1) { flag1 = false; } else {
builder1.Append(','); }
builder1.Append(AjaxPro.JavaScriptSerializer.Serialize(mi.Invoke(o,
null)));
}
}
else {
throw new
ArgumentException("RestrictedCustomConverter:Serialize can not find
field " + fieldName + " in type " + t.FullName);
}
}
}
builder1.Append(')');
return builder1.ToString();
}
}
you can public class RestrictedCustomConverter
:AjaxPro.IJavaScriptConverter {
private string m_fieldsToSerialize;
private string[] m_arrayOfFieldsToSerialize;
public RestrictedCustomConverter(Type[] serializableTypes,
string fieldsToSerialize):this(serializableTypes,new
Type[0]{},fieldsToSerialize) {
}
public RestrictedCustomConverter(Type[] serializableTypes,
Type[] deserializableTypes, string fieldsToSerialize) {
base.m_serializableTypes = serializableTypes;
base.m_deserializableTypes = deserializableTypes;
this.m_fieldsToSerialize = fieldsToSerialize;
this.m_arrayOfFieldsToSerialize =
fieldsToSerialize.Split(',');
}
private bool CanDeserialize(Type t) {
return Array.IndexOf(this.DeserializableTypes,t) !=-1 ;
}
public override string GetClientScript() {
StringBuilder sb = new StringBuilder();
foreach (Type t in SerializableTypes) {
sb.AppendFormat(@"var nsParts = '{0}'.split('.');
var root = window;
for(var i=0; i<nsParts.length; i++) {{
if(typeof root[nsParts[i]] == 'undefined')
root[nsParts[i]] = {{}};
root = root[nsParts[i]];
}}
",t.Namespace);
sb.AppendFormat(@"{0}=function({1})
{{",t.FullName,this.m_fieldsToSerialize);
if (this.CanDeserialize(t)) {
sb.AppendFormat(@"this.__type = ""{0}"";",
t.FullName,
AjaxPro.JavaScriptSerializer.Serialize(t.AssemblyQualifiedName));
}
sb.AppendFormat(@"
var flds='{0}'.split(',');
var fld;
for(var i=0;fld=flds[i];i++) {{
eval('this.'+fld+'='+fld);
}}
}}",this.m_fieldsToSerialize);
}
return sb.ToString();
}
public override string Serialize(object o) {
Type t = o.GetType();
StringBuilder builder1 = new StringBuilder();
bool flag1 = true;
builder1.AppendFormat("new {0}(",t.FullName);
foreach (string fieldName in
this.m_arrayOfFieldsToSerialize) {
System.Reflection.FieldInfo fi= null;
fi=
t.GetField(fieldName,System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance ); //FieldInfo
if (fi != null) {
if (flag1) { flag1 = false; } else {
builder1.Append(','); }
builder1.Append(AjaxPro.JavaScriptSerializer.Serialize(fi.GetValue(o)));
}
else {
System.Reflection.PropertyInfo pi =
t.GetProperty(fieldName, System.Reflection.BindingFlags.GetProperty |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance); // PropertyInfo
if (pi != null) {
System.Reflection.MethodInfo mi =
pi.GetGetMethod();
if (mi.GetParameters().Length <= 0) {
if (flag1) { flag1 = false; } else {
builder1.Append(','); }
builder1.Append(AjaxPro.JavaScriptSerializer.Serialize(mi.Invoke(o,
null)));
}
}
else {
throw new
ArgumentException("RestrictedCustomConverter:Serialize can not find
field " + fieldName + " in type " + t.FullName);
}
}
}
builder1.Append(')');
return builder1.ToString();
}
}
it will allow you to quickly create other converters that emit only a
partial set of the properties
of the object begin converted.
I then use it by creating a class like :
namespace NameSpace.Web.Ajax.JSONConverters {
public class UserConverter:RestrictedCustomConverter {
public UserConverter() : base(new Type[] {
typeof(NameSpace.BLL.User)
}, "UserID,Name,UserName,DisplayName,LastLogin") { }
}
}
this way I acheive two things:
1) Efficiancy when serializing array's of object by not serializing
thair property names but using a constructor instead.
2) The ability to serialize only the required properties by the client.
I would like though to make this integrated with the system so that I
could specify on each method what fileds are required.
so I could from two different methods return the same object type by
serialize different parts of it.
Eyal
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ajax.NET Professional" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/ajaxpro
The latest downloads of Ajax.NET Professional can be found at
http://www.ajaxpro.info/
Don't forget to read my blog at http://weblogs.asp.net/mschwarz/
-~----------~----~----~----~------~----~------~--~---