PDA

View Full Version : [CLOSED] How to send dictionary(of string, string) as clientconfig?



jchau
Nov 10, 2008, 4:45 PM
I created a DialogWindow class that inherits from Coolite.Window that has an associated clientside Ext.ux.DialogWindow class. In the client class, I have a params property that's just an object. How can I create a clientconfig serverside property in my DialogWindow class that will serialize correctly? I tried:



Private _URLParams As New Dictionary(Of String, String)
''' <summary>
''' Query string parameters to send to dialog
''' </summary>
<ClientConfig("urlParams", JsonMode.UnrollCollection)> _
<DefaultValue(CType(Nothing, Object))> _
<Category("Config Options")> _
Public Property URLParams() As Dictionary(Of String, String)
Get
Return _URLParams
End Get
Set(ByVal value As Dictionary(Of String, String))
_URLParams = value
End Set
End Property


However, I don't see it as part of the rendered script. Is there anything special I need to do? In ASP.NET, dictionary(of string, string) is serialized as a javascript object.

jchau
Nov 10, 2008, 5:12 PM
Argh...I always find the answer AFTER I make a post about it. Please let me know if this is the right way to do it. I basically created my own StringDictionaryJsonConverter:



Public Class StringDictionaryJsonConverter
Inherits JsonConverter

Public Overloads Overrides Function CanConvert(ByVal valueType As Type) As Boolean
Return GetType(Dictionary(Of String, String)).IsAssignableFrom(valueType)
End Function

Public Overloads Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal value As Object)
Dim dictionary As Dictionary(Of String, String) = TryCast(value, Dictionary(Of String, String))
If dictionary IsNot Nothing Then
Dim iterator As Dictionary(Of String, String).Enumerator = dictionary.GetEnumerator()

writer.WriteStartObject()
While iterator.MoveNext()
writer.WritePropertyName(iterator.Current.Key)
writer.WriteValue(iterator.Current.Value)
End While
writer.WriteEndObject()

End If
End Sub

Public Overloads Overrides Function ReadJson(ByVal reader As Newtonsoft.Json.JsonReader, ByVal objectType As Type) As Object
Throw New NotImplementedException()
End Function

End Class

jchau
Nov 10, 2008, 5:26 PM
I originally used LoadConfig.Params as an example so is that a bug?



Dictionary<string, string> paramItems = new Dictionary<string, string>();

[ClientConfig(JsonMode.UnrollCollection)]
[DefaultValue(null)]
[NotifyParentProperty(true)]
[Description("Custom list of parameters passed to the url.")]
public virtual Dictionary<string, string> Params
{
get
{
return this.paramItems;
}
set
{
this.paramItems = value;
}
}

Vladimir
Nov 12, 2008, 6:16 AM
Hi,

I don't think that is a bug. With Mode Json.UnrollCollection the ClientConfig builder try to serialize each item of collection to one object. The item of Dictionary is KeyValuePair which don't have properties with ClientConfig attribute (please note that only such properties can handled by ClientConfig builder). Therefore serialization for dictionary is empty

I think that your solution with JsonConverter is good.

Hope this help