1

I have an object with notation:

public class CompanyTeam
{
    public string companyGuid { get; set; }
    public string companyId { get; set; }
}

public class Team
{
    public string teamGuid { get; set; }
    public string teamName { get; set; }
    public CompanyTeam company { get; set; }
}

The Team object have data except CompanyTeam. When serialize my object

json = new JavaScriptSerializer().Serialize(teamObject);

return

{
    "teamGuid": "GUIDdata",
    "teamName": "nameData",
    "company": null,
}

I try instance the CompanyTeam Object but return object with null data:

{
    "teamGuid": "GUIDdata",
    "teamName": "nameData",
    "company": {
                  "companyGuid" : null,
                  "companyId" : null
               },
}

How could you get this result? any ideas?

{
    "teamGuid": "GUIDdata",
    "teamName": "nameData",
    "company": {},
}
5
  • I got a little lost on what you want 1/2 way through. What one represents what you are expecting/wanting? Commented Jun 19, 2017 at 18:08
  • I don't know how or even if it's possible to do this with the JavaScriptSerializer, but if you use JSON.net (nuget.org/packages/Newtonsoft.Json), you can omit properties with null values from being serialized (newtonsoft.com/json/help/html/…). Also you can tweak basically anything, so it's often the preference choice when serializing JSON. Commented Jun 19, 2017 at 18:10
  • @Trey I need the serialized result have "company" : { } , Its represent empty object in JSON Commented Jun 19, 2017 at 18:11
  • You are using JavaScriptSerializer and want to completely omit the properties of CompanyTeam when they are null. Unfortunately there is no built-in functionality for this, instead you will need to use a JavaScriptConverter as shown in Can JavaScriptSerializer exclude properties with null/default values?. Commented Jun 19, 2017 at 18:55
  • 1
    it the company is null, I know of no JSON serializer that would output what you expect; I would expect either to see "company":null, or not see anything at all. Could you just make it ... not be null ? if(obj.company == null) ob.company = new CompanyTeam(); should fix this, no? Commented Jun 19, 2017 at 19:08

1 Answer 1

3

You may try the following in order to achieve what you want and to keep using JavaScriptSerializer:

public class Team
{
    public Team()
    {
        teamGuid = "I have a value!";
        teamName = "me too!";
    }

    public Team(CompanyTeam company) : this()
    {
        this.company = company;
    }
    public string teamGuid { get; set; }
    public string teamName { get; set; }
    public CompanyTeam company { get; set; }

    public dynamic GetSerializeInfo() => new
    {
        teamGuid,
        teamName,
        company = company ?? new object()
    };
}

And your company class

  public class CompanyTeam
    {
        public CompanyTeam()
        {
            companyGuid = "someGuid";
            companyId = "someId";
        }
        public string companyGuid { get; set; }
        public string companyId { get; set; }
    }

You can write a method returning a dynamic where you can either return company if it is not null or a new object. Testing:

static void Main(string[] args)
        {
            var teamObj = new Team();
            var json = new JavaScriptSerializer().Serialize(teamObj.GetSerializeInfo());
            Console.WriteLine(json);
            Console.ReadLine();
        }

And the output:

{"teamGuid":"I have a value!","teamName":"me too!","company":{}}

If you use the constructur providing a not null company then you get:

{"teamGuid":"I have a value!","teamName":"me too!","company":{"companyGuid":"someGuid","companyId":"someId"}}

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.