Monday, 15 March 2010

c# - Serialize NodaTime JSON -



c# - Serialize NodaTime JSON -

i making prototype project of nodatime compared bcl's datetime, executing result gives me recursionlimit exceeded error.

this function using jsonify viewmodel. error happens after function returns.

[httppost] public jsonresult getdates(int numofdatestoretrieve) { list<datetimemodel> datetimemodellist = builddatetimemodellist(numofdatestoretrieve); jsonresult result = json(datetimemodellist, jsonrequestbehavior.allowget); homecoming result; }

my view model built when inspected it. here code view model.

public class datetimemodel { public int id; public localdatetime nodalocaldatetimeutc; public localdatetime nodalocaldatetime { { datetimezone datetimezone = datetimezoneproviders.bcl.getzoneornull(bcltimezoneid); //zoneddatetime zoneddatetime = nodalocaldatetimeutc.inutc().withzone(datetimezone); offsetdatetime offsetdatetime = new offsetdatetime(nodalocaldatetimeutc, offset.zero); zoneddatetime zoneddatetime = new zoneddatetime(offsetdatetime.toinstant(), datetimezone); homecoming zoneddatetime.localdatetime; } } public offsetdatetime nodaoffsetdatetime; public datetime bcldatetimeutc; public datetime bcllocaldatetime { { datetime utcdatetime = datetime.specifykind(bcldatetimeutc, datetimekind.utc); timezoneinfo nztimezone = timezoneinfo.findsystemtimezonebyid(bcltimezoneid); datetime result = timezoneinfo.converttimefromutc(utcdatetime, nztimezone); homecoming result; } } public datetimeoffset bcldatetimeoffset; //public int offset; public string originaldatestring; public string bcltimezoneid; }

i sure nodatime objects not serializing correctly because when comment code viewmodel jsonresult able execute.

i read off page nodatime api reference

code in namespace not included in noda time nuget packages; still deemed "experimental". utilize these serializers, please download , build noda time source code project home page.

so downloaded , built source code , replaced dll's project references don't know how implement jsonserialization classes.

can explain me how utilize nodatime.serialization.jsonnet classes create nodatime objects serializable?

we don't have back upwards javascriptserializer: suspect you'll have utilize json.net all json serialization. user guide page on serialization gives little bit more information, assume know json.net.

the news json.net pretty easy utilize - may find it's simple as:

var settings = new jsonserializersettings(); settings.configurefornodatime(); string json = jsonconvert.serializeobject(model, settings);

(or utilize jsonserializer.)

as aside, way you're using noda time types little odd to the lowest degree - may worth asking question details of you're trying achieve, , can work out more idiomatic way of doing :)

c# datetime nodatime

No comments:

Post a Comment