Nodatime:Serialization/Desalinization Duration as Ticks

69 views
Skip to first unread message

br.vla...@gmail.com

unread,
Jan 31, 2020, 5:53:58 AM1/31/20
to Noda Time
Hi there,
We.re moving NodaTime version from 1.4.x to 2.4.x and I need to serialize duration as ticks for maintain existing data format
I made simple code for try that 
            Duration duration = Duration.FromTicks(216000000000);
            var DurationConverter = new NodaPatternConverter<Duration>(DurationPattern.CreateWithInvariantCulture("SS"));
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
            jsonSerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Serialization);
            jsonSerializerSettings.Converters.Clear();
            jsonSerializerSettings.Converters.Add(DurationConverter);
            File.WriteAllText("file.json", JsonConvert.SerializeObject(duration, jsonSerializerSettings));
            var readFileStr = File.ReadAllText("file.json");
            var item = JsonConvert.DeserializeObject<Duration>(readFileStr, jsonSerializerSettings);
            Console.WriteLine(item.TotalTicks);


What pattern should I use for serialize / desalinize 216000000000 ticks ?  if I use "SS" it will be "21600", S.fffffffff - "21600.000000000"
I check your examples, unit tests but didn't found  solution for it
 

Jon Skeet

unread,
Jan 31, 2020, 11:00:12 AM1/31/20
to Noda Time
I don't think we have a format for "the total number of ticks" - I think as this is a pretty specialized situation, you're probably best off writing your own custom JSON serializer for that. You could potentially derive from NodaConverterBase<Duration> to take advantage of the common code that's already in there. It's not obvious to me at the moment whether you're trying to serialize/deserialize to JSON where the value is a string (containing an integer) or a number directly - but either should be feasible in that approach.

Jon

--

---
You received this message because you are subscribed to the Google Groups "Noda Time" group.
To unsubscribe from this group and stop receiving emails from it, send an email to noda-time+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/noda-time/a2ee9bac-806a-4c64-bba9-d8a56f8ed18e%40googlegroups.com.

br.vla...@gmail.com

unread,
Feb 2, 2020, 9:51:33 PM2/2/20
to Noda Time
NodaTime v. 1.4 Duration had property - ticks and constructor for it but now in versions 2.x + the ticks replaced to BclCompatibleTicks and not taking part in storing as I see.
But, we have a lot of legacy data in dif. storages and I have to care about backward compatibility, probably u can suggest some solution for my case.
Thank's you
Best Regards, Vlad


пятница, 31 января 2020 г., 12:53:58 UTC+2 пользователь br.vl...@gmail.com написал:

Jon Skeet

unread,
Feb 4, 2020, 11:10:43 AM2/4/20
to Noda Time
It's hard to give a solution when we don't really know what you need to do.

Use Duration.FromTicks to create a Duration from a number of ticks, and retrieve it with BclCompatibleTicks. What problems remain when you've done that?

--

---
You received this message because you are subscribed to the Google Groups "Noda Time" group.
To unsubscribe from this group and stop receiving emails from it, send an email to noda-time+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages