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": {},
}
dbc
  • 104,963
  • 20
  • 228
  • 340
CMedina
  • 4,034
  • 3
  • 24
  • 39
  • I got a little lost on what you want 1/2 way through. What one represents what you are expecting/wanting? – Trey Jun 19 '17 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 (https://www.nuget.org/packages/Newtonsoft.Json/), you can omit properties with null values from being serialized (http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm). Also you can tweak basically anything, so it's often the preference choice when serializing JSON. – ckuri Jun 19 '17 at 18:10
  • @Trey I need the serialized result have "company" : { } , Its represent empty object in JSON – CMedina Jun 19 '17 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?](https://stackoverflow.com/q/1387755/3744182). – dbc Jun 19 '17 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? – Marc Gravell Jun 19 '17 at 19:08

1 Answers1

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!

taquion
  • 2,667
  • 2
  • 18
  • 29