Permalink
Browse files

Fixed issue with query string serialization

  • Loading branch information...
1 parent a2e297d commit 442331244ac5f4b0615c7b5ec388ab6718657bec @desunit desunit committed Dec 25, 2012
@@ -10,5 +10,7 @@ internal static class JsState
[ThreadStatic] internal static bool IsWritingValue = false;
[ThreadStatic] internal static bool IsWritingDynamic = false;
+
+ [ThreadStatic] internal static bool QueryStringMode = false;
}
}
@@ -283,23 +283,26 @@ public static void WriteProperties(TextWriter writer, object value)
public static void WriteQueryString(TextWriter writer, object value)
{
- var i = 0;
- foreach (var propertyWriter in PropertyWriters)
+ try
{
- var propertyValue = propertyWriter.GetterFn((T)value);
- if (propertyValue == null) continue;
- var propertyValueString = propertyValue as string;
- if (propertyValueString != null)
+ JsState.QueryStringMode = true;
+ var i = 0;
+ foreach (var propertyWriter in PropertyWriters)
{
- propertyValue = propertyValueString.UrlEncode();
- }
+ var propertyValue = propertyWriter.GetterFn((T) value);
+ if (propertyValue == null) continue;
- if (i++ > 0)
- writer.Write('&');
+ if (i++ > 0)
+ writer.Write('&');
- Serializer.WritePropertyName(writer, propertyWriter.PropertyName);
- writer.Write('=');
- propertyWriter.WriteFn(writer, propertyValue);
+ Serializer.WritePropertyName(writer, propertyWriter.PropertyName);
+ writer.Write('=');
+ propertyWriter.WriteFn(writer, propertyValue);
+ }
+ }
+ finally
+ {
+ JsState.QueryStringMode = false;
}
}
}
@@ -116,6 +116,7 @@ public static string EncodeJson(this string value)
public static string EncodeJsv(this string value)
{
+ if (JsState.QueryStringMode) value = UrlEncode(value);
return string.IsNullOrEmpty(value) || !JsWriter.HasAnyEscapeChars(value)
? value
: string.Concat
@@ -57,6 +57,15 @@ public void Can_serialize_newline()
Assert.That(QueryStringSerializer.SerializeToString(new {newline = "\r\n"}), Is.EqualTo("newline=%0d%0a"));
}
+ [Test]
+ public void Can_serialize_array_of_strings_with_colon()
+ {
+ var t = new List<string>();
+ t.Add("Foo:Bar");
+ t.Add("Get:Out");
+ Assert.That(QueryStringSerializer.SerializeToString(new { list = t }), Is.EqualTo("list=[Foo%3aBar,Get%3aOut]"));
+ }
+
[Test]
public void Can_serialize_tab()
{

0 comments on commit 4423312

Please sign in to comment.