2

i use a json and the class definition is described below

how do i set a default value of null for

[JsonProperty("Time2ATR")]
   public DateTime time2atr { get; set; }

so i dont get a default value of "0001-01-01T00:00:00" if i dont assign any value to it?

full code and datasample below

 using Newtonsoft.Json;
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.STSClasses
{
    /// <summary>
    /// This file holds all user defined indicator methods.
    /// </summary>
   public class STSmyTrade
    {
        [JsonProperty("direction")]
        public string direction { get; set; }

        [JsonProperty("entryprice")]
        public double entry { get; set; }

        [JsonProperty("exitprice")]
        public double exit { get; set; }

        [JsonProperty("potentialtarget")]
        public double potentialtarget { get; set; }

        [JsonProperty("entrytime")]
        public DateTime entrytime { get; set; }

        [JsonProperty("exittime")]
        public DateTime exittime { get; set; }

        [JsonProperty("maxfavourable")]
        public double mfe { get; set; }

        [JsonProperty("maxagainst")]
        public double mae { get; set; }


        [JsonProperty("maxagainst1ATR")]
        public double mae1atr { get; set; }

        [JsonProperty("Time1ATR")]
        public DateTime time1atr { get; set; }

        [JsonProperty("maxagainst2ATR")]
        public double mae2atr { get; set; }
        [JsonProperty("Time2ATR")]
       public DateTime time2atr { get; set; }

        [JsonProperty("gains")]
        public double gains { get; set; }

        [JsonProperty("signal")]
        public string signal { get; set; }

        [JsonProperty("instrument")]
        public string instrument { get; set; }

        [JsonProperty("account")]
        public string account { get; set; }
         [JsonProperty("quantity")]
        public int quantity { get; set; }
         [JsonProperty("hitedge")]
        public bool hitedge { get; set; }
         [JsonProperty("RealizedProfitLoss")]
        public double RealizedProfitLoss { get; set; }
        [JsonProperty("CashValue")]
        public double CashValue { get; set; }
        [JsonProperty("BuyingPower")]
        public double BuyingPower { get; set; }
    }
}

    {
      "direction": "Long",
      "entryprice": 1.13211,
      "exitprice": 1.13197,
      "potentialtarget": 0.00025,
      "entrytime": "2016-08-22T13:46:31.7459655-04:00",
      "exittime": "2016-08-22T15:46:22.3955682-04:00",
      "maxfavourable": 0.00055,
      "maxagainst": -0.00035,
      "maxagainst1ATR": -0.00035,
      "Time1ATR": "0001-01-01T00:00:00",
      "maxagainst2ATR": -0.00035,
      "Time2ATR": "0001-01-01T00:00:00",
      "gains": -0.00015,
      "signal": "EnterLongBounce",
      "instrument": "$EURUSD",
      "account": "InteractiveBrokersindicatorbased",
      "quantity": 1,
      "hitedge": false,
      "RealizedProfitLoss": 0.0,
      "CashValue": 0.0,
      "BuyingPower": 0.0
    }');
junkone
  • 1,427
  • 1
  • 20
  • 45
  • Did you try this? [JsonProperty("Time2ATR")] public DateTime? time2atr { get; set; } – genericuser Aug 22 '16 at 21:17
  • Make the `time2atr` property be nullable as [this answer](https://stackoverflow.com/questions/39088824/how-to-set-default-value-to-null-for-datetime-variable-in-json/39088899#39088899) says, or use `NullToDefaultConverter` from [this answer](http://stackoverflow.com/questions/31747712/json-net-deserialization-null-guid-case/31750851#31750851). – dbc Aug 22 '16 at 21:38
  • Do you want the value to appear as `null` in the JSON, or do you want to be completely skipped? – dbc Aug 22 '16 at 21:41

2 Answers2

10

If you want null you have to use Nullable DateTime DateTime?, DateTime is struct and it's default value is "0001-01-01T00:00:00" so it won't ever be null.

CrudaLilium
  • 654
  • 7
  • 18
0

Check this answer from another question Using DefaultValueHandling

I havent check it myself but you can give it a try

Community
  • 1
  • 1
obito1406
  • 355
  • 1
  • 4
  • 10