Permalink
Browse files

Ignore JsConfig.IncludeNullValues for JSV, added test for JSON

  • Loading branch information...
mythz committed Aug 2, 2012
1 parent 31245a2 commit fa46723fe0ea93f04c676aab33d4edc16944193a
@@ -6,7 +6,8 @@ namespace ServiceStack.Text.Common
{
internal interface ITypeSerializer
{
- string TypeAttrInObject { get; }
+ bool IncludeNullValues { get; }
+ string TypeAttrInObject { get; }
WriteObjectDelegate GetWriteFn<T>();
WriteObjectDelegate GetWriteFn(Type type);
@@ -42,7 +42,7 @@ static JsWriter()
{
EscapeCharFlags[escapeChar] = true;
}
- var loadConfig = JsConfig.IncludeNullValues;
+ var loadConfig = JsConfig.EmitCamelCaseNames; //force load
}
public static void WriteDynamic(Action callback)
@@ -107,7 +107,7 @@ public static void WriteIDictionary(TextWriter writer, object oMap)
var dictionaryValue = map[key];
var isNull = (dictionaryValue == null);
- if (isNull && !JsConfig.IncludeNullValues) continue;
+ if (isNull && !Serializer.IncludeNullValues) continue;
if (writeKeyFn == null)
{
@@ -190,7 +190,7 @@ internal static class ToStringDictionaryMethods<TKey, TValue, TSerializer>
foreach (var kvp in map)
{
var isNull = (kvp.Value == null);
- if (isNull && !JsConfig.IncludeNullValues) continue;
+ if (isNull && !Serializer.IncludeNullValues) continue;
JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);
@@ -203,7 +203,7 @@ public static void WriteProperties(TextWriter writer, object value)
if ((propertyValue == null
|| (propertyWriter.DefaultValue != null && propertyWriter.DefaultValue.Equals(propertyValue)))
- && !JsConfig.IncludeNullValues) continue;
+ && !Serializer.IncludeNullValues) continue;
if (i++ > 0)
writer.Write(JsWriter.ItemSeperator);
@@ -21,8 +21,13 @@ namespace ServiceStack.Text.Json
internal class JsonTypeSerializer
: ITypeSerializer
{
- public static ITypeSerializer Instance = new JsonTypeSerializer();
-
+ public static ITypeSerializer Instance = new JsonTypeSerializer();
+
+ public bool IncludeNullValues
+ {
+ get { return JsConfig.IncludeNullValues; }
+ }
+
public string TypeAttrInObject { get { return "{\"__type\":"; } }
public static readonly bool[] WhiteSpaceFlags = new bool[(int)' ' + 1];
@@ -21,9 +21,14 @@ namespace ServiceStack.Text.Jsv
internal class JsvTypeSerializer
: ITypeSerializer
{
- public static ITypeSerializer Instance = new JsvTypeSerializer();
-
- public string TypeAttrInObject { get { return "{__type:"; } }
+ public static ITypeSerializer Instance = new JsvTypeSerializer();
+
+ public bool IncludeNullValues
+ {
+ get { return false; } //Doesn't support null values, treated as "null" string literal
+ }
+
+ public string TypeAttrInObject { get { return "{__type:"; } }
public WriteObjectDelegate GetWriteFn<T>()
{
@@ -117,6 +117,32 @@ public void Can_handle_null_in_quotes_in_Answer()
Assert.That(fromJson.tag_name, Is.EqualTo("null"));
}
+ [Test]
+ public void Deserialize_WithNullCollection_CollectionIsNull()
+ {
+ JsConfig.IncludeNullValues = true;
+
+ var item = new Foo { Strings = null };
+ var json = JsonSerializer.SerializeToString(item);
+ var result = JsonSerializer.DeserializeFromString<Foo>(json);
+ Assert.IsNull(result.Strings);
+
+ var jsv = TypeSerializer.SerializeToString(item);
+ result = TypeSerializer.DeserializeFromString<Foo>(jsv);
+ Assert.IsEmpty(result.Strings); //JSV doesn't support setting null values explicitly
+
+ JsConfig.IncludeNullValues = false;
+ }
+
+ public class Foo
+ {
+ public Foo()
+ {
+ Strings = new List<string>();
+ }
+ public List<string> Strings { get; set; }
+ }
+
}
}

0 comments on commit fa46723

Please sign in to comment.