GithubHelp home page GithubHelp logo

Comments (3)

kostya avatar kostya commented on June 23, 2024 3

ArraySerializable module in shard since v0.15

from msgpack-crystal.

kostya avatar kostya commented on June 23, 2024 1

I also think about doing compact mapping with arrays (sounds like protobuf format actually jeromegn/protobuf.cr#26), because packed data should be much smaller. Btw, it should be new Serializable module like SerializableCompact.

Until this not supported, you can do it like this (And also write macro that generate such code):

require "msgpack"

class MessagePackPersonArray
  def initialize(pull : MessagePack::Unpacker)
    token = pull.prefetch_token
    raise "expected array" unless token.type == :ARRAY

    size = token.size
    raise "expected array with size > 2" if size < 2
    token.used = true

    @name = String.new(pull)
    @age = Int32.new(pull)
    (size - 2).times { pull.skip_value }
  end
end

person = MessagePackPersonArray.from_msgpack(["John", 30, "1", 2, [1, 2, 3]].to_msgpack)
p person # => #<MessagePackPersonArray:0x102ef0e80 @name="John", @age=30>

from msgpack-crystal.

kostya avatar kostya commented on June 23, 2024

i add this in compact_serializable branch, you can read specs, about how it works https://github.com/crystal-community/msgpack-crystal/blob/compact_serializable/spec/compact_serializable_spec.cr

require "./src/msgpack"

class MessagePackPersonArray
  include MessagePack::CompactSerializable
  include MessagePack::CompactSerializable::Unmapped

  property name : String
  property age : Int32?
end

person = MessagePackPersonArray.from_msgpack(["John", 30, "1", 2, [1, 2, 3]].to_msgpack)
p person 
#=> #<MessagePackPersonArray:0x100aadea0 @msgpack_unmapped={2 => "1", 3 => 2_u8, 4 => [1_u8, 2_u8, 3_u8]}, @name="John", @age=30>

0, 1 it indexed by default, you can change it.

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.