Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide some examples #4

Closed
Evgenus opened this issue Feb 13, 2013 · 5 comments
Closed

Provide some examples #4

Evgenus opened this issue Feb 13, 2013 · 5 comments
Assignees
Labels

Comments

@Evgenus
Copy link

Evgenus commented Feb 13, 2013

I'm trying to use this and have no clue what is the good practice. Especially I'm wondering about PackToCore and UnpackFromCore pair. Should I use Read* functions inside UnpackFromCore? It is too painful to mine knowledge from unittests.

@ghost ghost assigned yfakariya Feb 13, 2013
@yfakariya
Copy link
Member

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?

@Evgenus
Copy link
Author

Evgenus commented Feb 13, 2013

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.

@Evgenus
Copy link
Author

Evgenus commented Feb 13, 2013

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.

@yfakariya
Copy link
Member

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.

@Evgenus
Copy link
Author

Evgenus commented Feb 19, 2013

ありがとうございます

@Evgenus Evgenus closed this as completed Feb 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants