I have the following class which is used to serialize an IDictionary
object to either XML or JSON.
When serializing to JSON, it seems the AjaxNonSerializable attribute is
ignored, so both the
Table and Items[] properties get serialized.
Am I do something wrong or is this a bug?
Tried this with AjaxPro.dll 6.9.22.2 and 6.10.6.2
<snip psuedocode>
Hashtable obj = new Hashtable(); // or ListDictionary..
// populate hashtable
if xml
string output = ((SimpleDictionary)obj).Serialize();
else
string output = AjaxPro.JavaScriptSerializer.Serialize(obj);
</snip>
[XmlInclude(typeof(string))]
[XmlInclude(typeof(bool))]
[XmlInclude(typeof(short))]
[XmlInclude(typeof(int))]
[XmlInclude(typeof(long))]
[XmlInclude(typeof(float))]
[XmlInclude(typeof(double))]
[XmlInclude(typeof(DateTime))]
[XmlInclude(typeof(char))]
[XmlInclude(typeof(decimal))]
public class SimpleDictionary
{
private IDictionary _items;
/// <summary>Get the underlying IDictionary</summary>
[XmlIgnore]
[AjaxPro.AjaxNonSerializable()]
public IDictionary Table {get {return _items;}}
/// <summary>Get/set the object items as a simple array</summary>
[XmlElement(ElementName="Item")]
public Item[] Items
{
get
{
Item[] items = new Item[_items.Keys.Count];
int i = 0;
foreach (DictionaryEntry de in _items)
{
items[i++] = new Item(de.Key.ToString(),
de.Value.ToString());
}
return items;
}
set
{
Item[] items = value;
_items.Clear();
for (int i = 0; i < items.Length; i++)
{
_items.Add(items[i].Key, items[i].Value);
}
}
}
/// <summary>Construct an empty object</summary>
public SimpleDictionary() {}
/// <summary>Construct an object from an <see cref="IDictionary"
/></summary>
/// <param name="items"><see cref="ListDictionary" /> used to populate
object</param>
public SimpleDictionary(IDictionary items)
{
_items = items;
}
/// <summary>Serialize the object to a string</summary>
/// <remarks>Output looks like:
/// <![CDATA[<Items><Item Key="1" Value="One" /><Item Key="2"
Value="Two" /></Items>]]>
/// </remarks>
/// <returns>Object content as a string</returns>
public string Serialize()
{
// Override the root element name to be 'Items'
XmlSerializer xs = new XmlSerializer(GetType(), new
XmlAttributeOverrides(), new Type[]{}, new XmlRootAttribute("Items"),
"");
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, this);
return Util.StreamToString(ms);
}
}
/// <summary>
/// Helper class to serialize/deserialize the content of a IDictionary
for simple string Keys/Values
/// </summary>
[Serializable]
public class Item
{
/// <summary>Key</summary>
[XmlAttribute]
public string Key;
/// <summary>Value</summary>
[XmlAttribute]
public string Value;
/// <summary>Default constructor for the XmlSerializer</summary>
public Item() {}
/// <summary>Alernate constructor</summary>
/// <param name="key">Item key as string</param>
/// <param name="val">Item value as string</param>
public Item(string key, string val)
{
Key = key;
Value = val;
}
}
// JSON output
{"__type":"AGWebServices.SimpleDictionary, AGWebServices,
Version=3.0.0.30213, Culture=neutral, PublicKeyToken=null",
"Table":[{"__type":"System.Collections.DictionaryEntry, mscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Key":1,"Value":"Agency"},{"__type":"System.Collections.DictionaryEntry,
mscorlib, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089",
"Key":11,"Value":"Customer Service"},
{"__type":"System.Collections.DictionaryEntry, mscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Key":14,"Value":"Billing and Premiums"}],
"Items":[{"__type":"AGWebServices.Item, AGWebServices,
Version=3.0.0.30213, Culture=neutral, PublicKeyToken=null",
"Key":"1","Value":"Agency"},
{"__type":"AGWebServices.Item, AGWebServices, Version=3.0.0.30213,
Culture=neutral, PublicKeyToken=null",
"Key":"11","Value":"Customer Service"},
{"__type":"AGWebServices.Item, AGWebServices, Version=3.0.0.30213,
Culture=neutral, PublicKeyToken=null",
"Key":"14","Value":"Billing and Premiums"}]}
// XML output
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/"><?xml version="1.0"?><Items
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Item Key="1"
Value="Agency" /><Item Key="11" Value="Customer Service" /><Item
Key="14" Value="Billing and Premiums" /></Items></string>
--~--~---------~--~----~------------~-------~--~----~
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/
The open source project is now located at
http://www.codeplex.com/Wiki/View.aspx?ProjectName=AjaxPro
-~----------~----~----~----~------~----~------~--~---