GithubHelp home page GithubHelp logo

Comments (2)

benoist avatar benoist commented on June 28, 2024

Currently it should work with nested hashes as long as you use the primitive types known to message pack. Symbol and MyClass are not known.

You would have to create a new class with mapping defined in the class.
https://github.com/benoist/msgpack-crystal/blob/master/src/message_pack/mapping.cr

However the Union type with a custom class is difficult to do, as you might get the following problem:

class MyHash
  MessagePack.mapping({
    hash: Hash(Symbol, (Int32 | String | MyClass))
  })
end

class MyClass
  MessagePack.mapping({
    my_string: String
  })
end

{ 
  "string" => MyHash.new({ 
    :symbol1 => "string", 
    :symbol2 => MyClass.new("class string") 
  })
}.to_msgpack

It will create a message packed buffer something like this (its missing a few bits but I left it out so its more readable)

[ :hash_start, :string, "string", :hash_start, :string, "symbol1", :string, "string", :string, "symbol2", :string, "class string"]

When you want to unpack it and gets to the symbol2 value, it doesn't know it should be of type MyClass.
The only way to do this would be to use message pack extensions, which requires you to define the extension types yourself.

This is currently not supported.

from msgpack-crystal.

bararchy avatar bararchy commented on June 28, 2024

@benoist Thanks :) That helped.

from msgpack-crystal.

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.