Git Product home page Git Product logo

Comments (4)

yfakariya avatar yfakariya commented on July 18, 2024

It is by design.
Because Msgpack-CLI prefers interoperability, it serializes DateTime object as unix epoc. The precision of it is milliseconds while FCL DateTime has 100-nano seconds precision, so you observed some digits were lost.

If you simply want to workaround this (because you do not have to consider interoperability with other language bindings, for example), you can use custom serializer like following:

// Custom Serializer. Note you must implement it to be stateless object.
public class NetDateTimeSerializer : MessagePackSerializer<DateTime>
{
    protected override void PackToCore(Packer packer, DateTime value)
    {
          // Note: if you want to preserve timezone info, you should use DateTimeOffset.
          packer.Pack(value.ToUniversalTime().Tick);
    }

    protected override DateTime UnpackFromCore(Unpacker unpacker)
    {
        return new DateTime((long)packer.Data.Value);
    }
}
...
// In your application initialization code
// Create your global context which will be used in all serialization (or, you can overwrite SerializationContext.Default).
s_context = new SerializationContext();
// Register your custom serializer.
s_context.Serializers.Register(new NetDateTimeSerializer());
...
// In application code
// Create serializer with above context which knows NetDateTimeSerializer.
var serializer = MessagePackSerializer.Create<Foo>(s_context);

// Let's test custom serializer.
var expected = new DateTime(1234L); // This has micro-seconds values.
using(var buffer = new MemoryStream())
{
    serializer.Pack(expected, buffer);
    buffer.Position = 0;
    var actual = serializer.Unpack(buffer);

    // Are they equal?
    Assert.AreEqual(expected, actual);
}

from msgpack-cli.

yfakariya avatar yfakariya commented on July 18, 2024

Close because it have been opened a long time and no response.
It is welcome to reopen this or create new issue for an additional question.

from msgpack-cli.

ikorkhov avatar ikorkhov commented on July 18, 2024

Is there actually a way to replace a built-in serializer for a specific type (e.g. DateTime) with a custom one? The code above does't actually register NetDateTimeSerializer because of this line (from SerializerRepository.cs):

    return this._repository.Register( typeof( T ), serializer, allowOverwrite: false );

My understanding is that there's a serializer for DateTime in a collection of serilaizers already and allowOverride set to false prevents replacing it with a new one.

Am I missing something?

from msgpack-cli.

yfakariya avatar yfakariya commented on July 18, 2024

Sorry, it is spec bug.
I opened new issue #24 now, and will fix it in 0.4 branch.

from msgpack-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.