Hi Albert,
yes, this was in an older version wrong, but fixed since the Pro
version, not sure which version number.
The DeserializePrimitive method is now replaced by a
PrimitiveConverter, but char will be implemented in the
StringConverter, which is new, too.
So, I hope to release the new version soon...!
Regards,
Michael
On 5/31/06, Albert Weinert <[EMAIL PROTECTED]> wrote:
>
> Michael Schwarz schrieb:
> > Hi,
> >
> > it will not have any sideeffect for the JavaScriptSerializer, because
> > these parts are written using the JavaScript notation. If you want to
>
> Ok, i take a look at the latest source release, now you are using the
> separators from System.Globalization.NumberFormatInfo, in an older
> version i saw that you are using hard coded ","<>"." replacements.
>
> Sorry for my answer, but i used "may" :)
>
> But i saw i little bug in
>
> JavaScriptDeserializer.DeserializePrimitive(IJavaScriptObject n, Type type)
>
> You are replacing the "." everytime with the current decimal separator,
> even if the type is a char.
>
> Maybe you dev-version has already fixed this.
>
> --
>
> Freundliche Grüße
>
> Albert Weinert
>
> http://der-albert.com
>
> >
>
--
Best regards | Schöne Grüße
Michael
Microsoft MVP - Most Valuable Professional
Microsoft MCAD - Certified Application Developer
http://weblogs.asp.net/mschwarz/
http://www.schwarz-interactive.de/
mailto:[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
/*
* MS 06-05-24 initial version
* allowNumberBooleanAsString
*
*/
using System;
using System.Text;
using System.Data;
namespace AjaxPro
{
/// <summary>
/// Provides methods to serialize and deserialize a String object.
/// </summary>
public class StringConverter : IJavaScriptConverter
{
public StringConverter()
: base()
{
m_serializableTypes = new Type[] {
typeof(String),
typeof(Char)
};
m_deserializableTypes = m_serializableTypes;
}
public override string Serialize(object o)
{
return JavaScriptUtil.QuoteString(o.ToString());
}
public override object Deserialize(IJavaScriptObject o, Type t)
{
if
(!Utility.Settings.OldStyle.Contains("allowNumberBooleanAsString"))
{
if (o is JavaScriptNumber)
return
JavaScriptDeserializer.Deserialize(o, typeof(Int64));
else if (o is JavaScriptBoolean)
return
JavaScriptDeserializer.Deserialize(o, typeof(Boolean));
}
if (t == typeof(char))
{
string s = o.ToString();
if (s.Length == 0)
return '\0';
return s[0];
}
return o.ToString();
}
public override bool TryDeserializeValue(IJavaScriptObject jso,
Type t, out object o)
{
if (t.IsAssignableFrom(typeof(string)))
{
o = this.Deserialize(jso, t);
return true;
}
return base.TryDeserializeValue(jso, t, out o);
}
}
}
/*
* MS 06-05-24 initial version
*
*/
using System;
using System.Text;
using System.Data;
namespace AjaxPro
{
/// <summary>
/// Provides methods to serialize and deserialize a primitive object.
/// </summary>
public class PrimitiveConverter : IJavaScriptConverter
{
public PrimitiveConverter()
: base()
{
m_serializableTypes = new Type[] {
typeof(Boolean),
typeof(Byte), typeof(SByte),
typeof(Int16), typeof(UInt16), typeof(Int32),
typeof(UInt32), typeof(Int64), typeof(UInt64),
// typeof(Char), // Char is
handeled by StringConverter
typeof(Double), typeof(Single)
};
m_deserializableTypes = m_serializableTypes;
}
public override string Serialize(object o)
{
if (o is Boolean)
{
return ((bool)o == true ? "true" : "false");
}
if (o is Byte || o is SByte)
{
return o.ToString();
}
if (o is Single && Single.IsNaN((Single)o))
{
throw new ArgumentException("object must be a
valid number (float.NaN)", "o");
}
if (o is Double && Double.IsNaN((Double)o))
{
throw new ArgumentException("object must be a
valid number (double.NaN)", "o");
}
// Shave off trailing zeros and decimal point, if
possible
string s =
o.ToString().ToLower().Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator,
".");
if (s.IndexOf('e') < 0 && s.IndexOf('.') > 0)
{
while (s.EndsWith("0"))
{
s.Substring(0, s.Length - 1);
}
if (s.EndsWith("."))
{
s.Substring(0, s.Length - 1);
}
}
return s;
}
public override object Deserialize(IJavaScriptObject o, Type t)
{
if (!t.IsPrimitive)
throw new NotSupportedException();
string s = o.ToString();
// TODO: return the default value for this primitive
data type
s = s.Replace(".",
System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator);
if (t == typeof(Boolean) && o is JavaScriptBoolean)
return (bool)((JavaScriptBoolean)o);
if (t == typeof(Byte))
return Byte.Parse(s);
else if (t == typeof(SByte))
return SByte.Parse(s);
else if (t == typeof(Int16))
return Int16.Parse(s);
else if (t == typeof(UInt16))
return UInt16.Parse(s);
else if (t == typeof(Int32))
return Int32.Parse(s);
else if (t == typeof(UInt32))
return UInt32.Parse(s);
else if (t == typeof(Int64))
return Int64.Parse(s);
else if (t == typeof(UInt64))
return UInt64.Parse(s);
else if (t == typeof(Double))
return Double.Parse(s);
else if (t == typeof(Single))
return Single.Parse(s);
throw new NotImplementedException("This primitive data
type '" + t.FullName + "' is not implemented.");
}
}
}