Git Product home page Git Product logo

Comments (5)

yfakariya avatar yfakariya commented on July 18, 2024

Sorry, I have written a sample of simple custom serializer, and posted it to the wiki page.
https://github.com/msgpack/msgpack-cli/wiki/Custom-serialization

Is it help you?

from msgpack-cli.

Evgenus avatar Evgenus commented on July 18, 2024

Thank you very much, it clears a bit. I'd like to see some example about

public class A 
{
     public List<string> Strings;
}

public class B
{
     public List<A> Lists;
}

How to write external manual(custom) serializers for A and B? Where should be Read invocation for A's head? I'm trying to use UnpackSubtree a lot. But it seems to be not a good idea. Objects inside other object, that is what I'm looking for.

Also best practice for polymorphic serialization will be very helpful. For something like that.

public abstract class A 
{
     public string ValueInBaseClass;
}

public class B: A
{
     public float ValueInDerivedClass;
}

public class C: A
{
     public int ValueInDerivedClass;
}

public class L
{
    public List<A> PolymorphicData;
}

In that case I need to serialize L instances, also I want common part for A field serialization. As above, here everything is manual(custom) serialized.

I'm very glad that you are still watching this project.

from msgpack-cli.

Evgenus avatar Evgenus commented on July 18, 2024

About that remark:

        // If the target objects has complex (non-primitive) objects,
        // you can get serializers which can handle complex type fields.
        // And then, you can cache them to instance fields of this custom serializer.

Can I store Context inside serializer and then run unpacker.Unpack<subtype>(Context)?

It is also unclear what is the purpose of subtree mode and ReadSubtree functionality and in what cases it should be used and in what should not.

from msgpack-cli.

yfakariya avatar yfakariya commented on July 18, 2024

As you know, built-in mechanism does not support polymorphic objects, so
you must use manual serializer. You have 2 choices:

  1. If you can know all possible derived class like your example, you might
    be able to implement serializer with determining first field type.
// in the YourSerializer.UnpackCore
string firstField;
if(!unpacker.ReadString(out firstField)){ ... }
// Get a second field.
if(!unpacker.Read()){...}
MessagePackObject secondField = unpacker.Data.Value;
if(secondField.IsTypeOf<float>())
{
    return new B(firstField, (float)secondField);
}
else if (secondField.IsTypeOf<int>())
{
    return new B(firstField, (float)secondField);
}
else
{
    ...
}
  1. Otherwise, you can encode type information in the stream. The
    implementation of a MemcachedTranscoder (
    https://github.com/neuecc/MemcachedTranscoder) will help you.

Can I store Context inside serializer and then run
unpacker.Unpack(Context)?

Yes, you can. It is actually built-in (emitted) serializers behavior.

from msgpack-cli.

Evgenus avatar Evgenus commented on July 18, 2024

ありがとうございます

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.