Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8337971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:27:06+00:00 2026-06-09T04:27:06+00:00

This is a question about serialization in general, but in particular I am using

  • 0

This is a question about serialization in general, but in particular I am using ServiceStack’s excellent serializers in my .NET code.

Should the deserializers set null references on properties? It seems like currently it ignores null references and just allows such fields to be set according the class’s defaults. For example, this test fails:

[TestMethod]
public void Deserialize_WithNullCollection_CollectionIsNull() {
    var serializer = new ServiceStack.Text.TypeSerializer<Foo>();
    Foo item = new Foo() { Strings = null };
    Foo result = serializer.DeserializeFromString(serializer.SerializeToString(item));
    Assert.IsNull(result.Strings);
}
public class Foo {
    public Foo() {
        Strings = new List<string>();
    }
    public List<string> Strings { get; set; }
}

I believe this perhaps this test ought to succeed, but it does not–item.Foo is an empty list rather than a null reference. It seems to me that null references are part of an object’s state just like any actual reference is. Am I right or wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:27:09+00:00Added an answer on June 9, 2026 at 4:27 am

    This is by design.

    By default (to save bandwidth) ServiceStack’s text serializers don’t emit null value on the wire. If there is no null in the generated JSON then the property doesn’t get set when its deserialized, which is why it takes the default value given in the constructor.

    You can enable null values with:

    JsConfig.IncludeNullValues = true;
    

    This is a static (Shared) property, so setting it once should apply globally in the process.

    This test passes when using the JsonSerilaizer:

        [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);
        }
    
        public class Foo
        {
            public Foo()
            {
                Strings = new List<string>();
            }
            public List<string> Strings { get; set; }
        }
    

    It doesn’t work in JSV-Format (i.e. TypeSerializer) which doesn’t support null values, since it can’t distinguish it from a "null" string literal. So if you want to use the JSV TypeSerializer you should assume null means the default property values of the type (i.e. as specified in the default constructor).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I previously asked this question about how to make versioned serializaion using boost::serialization::access I
Followed this question about delayed_job and monit Its working on my development machine. But
Note: I originally asked this question about an hour ago but only recently realized
I have seen this question about deploying to WebSphere using the WAS ant tasks.
JD Long helped me with this: question about manual annotation . But is it
I asked a previous question about serialization and validation. Someone mentioned using the JSON
I recently asked a question about using Fluent NHibernate with .NET 4 - I
This question is related to this one but I think should be asked separately.
This question about Timers for windows services got me thinking: Say I have (and
I've this question about pass some instances by ref or not: here is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.