Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ignore JsConfig.IncludeNullValues for JSV, added test for JSON

  • Loading branch information...
commit fa46723fe0ea93f04c676aab33d4edc16944193a 1 parent 31245a2
@mythz mythz authored
View
3  src/ServiceStack.Text/Common/ITypeSerializer.cs
@@ -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);
View
2  src/ServiceStack.Text/Common/JsWriter.cs
@@ -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)
View
4 src/ServiceStack.Text/Common/WriteDictionary.cs
@@ -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);
View
2  src/ServiceStack.Text/Common/WriteType.cs
@@ -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);
View
9 src/ServiceStack.Text/Json/JsonTypeSerializer.cs
@@ -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];
View
11 src/ServiceStack.Text/Jsv/JsvTypeSerializer.cs
@@ -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>()
{
View
26 tests/ServiceStack.Text.Tests/NullableTypesTests.cs
@@ -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; }
+ }
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.