6

I have got incomming Json in format:

{
    "Type": "value",
    "Name": "MeteoStation",
    "UniqueAdress": "2C:3A:E8:0F:10:76",
    "valuesList": [{
        "Value": 23.00,
        "Unit": "C",
        "Type": "temperature",
        "SourceUniqAdress": "2C:3A:E8:0F:10:76",
        "TimeCaptured": "2018-03-26T09:36:13.200Z"
    }]
}

In my program, I want to create object IValuePacket that is one value in list of values.

JObject jobject = JObject.Parse(incomingJson);
var settings = new JsonSerializerSettings {
    NullValueHandling = NullValueHandling.Ignore,
    MissingMemberHandling = MissingMemberHandling.Ignore
};
var incommingMessage = JsonConvert.DeserializeObject<MessageEncapsulation>(incomingJson);
string Type = incommingMessage.Type;
string name = incommingMessage.Name;

if (string.IsNullOrWhiteSpace(name))
    name = "no name";

if (Type.ToLower().Equals("value")) {
    var values = JsonConvert.DeserializeObject<List<IValuePacket>>(jobject["valuesList"].ToString());
}

Everything works fine untill I recieved exact this json as mention above. JObject.Parse modifies value TimeCaptured and jobject looks like:

{
"Type": "value",
"Name": "Meteostation",
"UniqueAdress": "2C:3A:E8:0F:10:76",
"valuesList": [{
    "Value": 23.00,
    "Unit": "C",
    "Type": "temperature",
    "SourceUniqAdress": "2C:3A:E8:0F:10:76",
    "TimeCaptured": "2018-03-26T09:36:13.2Z"
}]}

Thats not so much difference, but DateTime.ParseExact(value, "yyyy-MM-ddThh:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture); cannot parse new value. Actually, I am sending 201 ms instead of 200 ms. It works, but I want to have good time for future reasons.

Is there any way how to avoid changing in Json during parsing?

7
  • there is an overload for DateTime.ParseExact, which allows you to pass multiple formats msdn.microsoft.com/en-us/library/332de853(v=vs.110).aspx Commented Mar 26, 2018 at 12:17
  • 1
    So wait, are you actually losing precision, or just zeroes? Commented Mar 26, 2018 at 12:17
  • @Nyerguds just zeroes. Precision is the same Commented Mar 26, 2018 at 12:27
  • Then, doesn't that mean that it is already parsed by something at that point? Otherwise it can't know those zeroes are superfluous. Commented Mar 26, 2018 at 12:28
  • I dont understand what are you asking. There is only one parsing. From correct string into incorrect jsonobject. Then, I am trying to parse DateTime from incorect jsonobject that throws incorrect format excepton. Commented Mar 26, 2018 at 12:31

1 Answer 1

5

It does not really modify your string, it just parses your date string into DateTime object when you call JObject.Parse. If you do this:

var obj = JObject.Parse(json);
var values = (JArray) obj["valuesList"];
var time = (JValue) values[0]["TimeCaptured"];
Console.WriteLine(time.Value.GetType());

You notice that time.Value is of type DateTime. Then you do this:

JsonConvert.DeserializeObject<List<IValuePacket>>(jobject["valuesList"].ToString());

By doing that you convert valueList back to json, but now TimeCaptured is DateTime and not a string, so that DateTime object is converted to json string using whatever date time format is used by JSON.NET by default.

You can avoid parsing strings that look like dates to .NET DateTime objects by parsing json to JObject like this:

JObject obj;
using (var reader = new JsonTextReader(new StringReader(json))) {
    // DateParseHandling.None is what you need
    reader.DateParseHandling = DateParseHandling.None;
    obj = JObject.Load(reader);
}

Then type of TimeCaptured will be string, as you expect.

Side note: there is no need to convert JToken back to string and then call JsonConvert.Deserialize on that string. Instead, do this:

var values = obj["valuesList"].ToObject<List<IValuePacket>>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.