0

whats the equivalent for creating json serialized data for this javascript code:

chartData.push({date: "bla",value1: "bla1",value2: "bla2"});
chartData.push({date: "blx",value2: "blax2"});

The json will look something like this:

[{
       "date": bla,
       "value1": bla1,
       "value2": bla3,
   }, {
       "date": blx,
       "value2": blax2
   }]

I tried creating a list of class, but when i dont assign the value1 property, the value will just be zero. But i dont want the property to be displayed in the json at all, when not assigned.

 List <Models.HistoClass> HistoData= new List<Models.HistoClass>();
            HistoData.Add(new Models.HistoClass {date="bla",value1="bla1",value2="bla3"});
            HistoData.Add(new Models.HistoClass { date= "blx", value2="blax2" });

How should i create the datamodel to have a json like that?

Thanks!

Raunts
  • 76
  • 1
  • 10
  • Maybe this is helpful http://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net – Tasos K. Jan 06 '16 at 21:04
  • go to this link to validate your json it's not valid format by the way http://json2csharp.com/ – MethodMan Jan 06 '16 at 21:04
  • "I tried creating a list of class, but when i dont assign the value1 property, the value will just be zero." For properties that are non-nullable, their default value is set when a value is ommitted during initialization. Try using int? or Nullable, then accessing it like if (value1.HasValue) { /* do something with value1.Value */ } – Justin Ryder Jan 06 '16 at 21:58

2 Answers2

2

If you're creating data manually in the way you show, you could use anonymous types to construct data the looks exactly how you want:

 List <object> HistoData= new List<object>();
 HistoData.Add(new {date="bla",value1="bla1",value2="bla3"});
 HistoData.Add(new { date= "blx", value2="blax2" });

However, if you want to use a strong type and/or your code is initializing the values in the same way every time, but sometimes values just aren't present, then you can make the values nullable and tell JSON.NET to ignore null properties when serializing:

public class HistoClass
{
    public string date; 
    [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
    public int? value1;
    [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
    public int? value2;
}
...
List <Models.HistoClass> HistoData= new List<Models.HistoClass>();
HistoData.Add(new Models.HistoClass {date="bla",value1="bla1",value2="bla3"});
HistoData.Add(new Models.HistoClass { date= "blx", value2="blax2" });
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0

You can use Json.NET's ShouldSerialize:

Public class DateAndValues
{
   public DateTime Date {get; set;}
   public Int32 Index {get; set;}
   public Int32 Value1 {get; set;}
   public Int16 Value2 {get; set;}

   public bool ShouldSerializeValue1 ()
   {
      // your condition for serialization, for example even objects:
      this.Index % 2 != 0;
   }
}

This description:

To conditionally serialize a property, add a method that returns boolean with the same name as the property and then prefix the method name with ShouldSerialize. The result of the method determines whether the property is serialized. If the method returns true then the property will be serialized, if it returns false then the property will be skipped.

is taken from this link.

To actually serialize, of course, use:

var firstObj = new DateAndValues { Index = 1, .. };
var secondObj = new DateAndValues { Index = 2, .. };
string json = 
  JsonConvert.SerializeObject(new[] {firstObj, secondObj}, Formatting.Indented);

and according to the condition above, secondObj (even Index) will not have Value1.

guysigner
  • 2,822
  • 1
  • 19
  • 23