c# - JToken: Get raw/original JSON value -


is there way raw/original json value jtoken?

the problem:

var data = jobject.parse(@"{     ""simpledate"":""2012-05-18t00:00:00z"",     ""patterndate"":""2012-11-07t00:00:00z"" }");  var value = data["simpledate"].value<string>(); 

the value 05/18/2012 00:00:00 need original string 2012-05-18t00:00:00z.

is there way original value? also, cannot change way how jobject created (e.g. change settings), because passed parameter class...

(reference: the original njsonschema issue)

you cannot original string, date strings recognized , converted datetime structs inside jsonreader itself. can see if do:

console.writeline(((jvalue)data["simpledate"]).value.gettype()); // prints system.datetime 

you can, however, extract dates in iso 8601 format doing:

var value = jsonconvert.serializeobject(data["simpledate"]); // value "2012-05-18t00:00:00z" 

this output jvalue in json-appropriate string format. since original dates in format, may meet needs.

(honestly, i'm surprised jvalue.tostring() outputs dates in non-iso format, given jobject.tostring() output contained dates in iso format.)

if able change settings while reading jobject, use jsonserializersettings.dateparsehandling = dateparsehandling.none:

        var settings = new jsonserializersettings { dateparsehandling = dateparsehandling.none };         var data = jsonconvert.deserializeobject<jobject>(@"{             ""simpledate"":""2012-05-18t00:00:00z"",             ""patterndate"":""2012-11-07t00:00:00z""         }", settings);          var value = data["simpledate"].value<string>();          debug.writeline(value); // outputs 2012-05-18t00:00:00z 

there's no overload jobject.parse() takes jsonserializersettings, need use deserializeobject. setting gets propagated jsonreader.dateparsehandling.


Comments