Comments (3)
ArraySerializable module in shard since v0.15
from msgpack-crystal.
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.
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)
- Packing array of hashes HOT 11
- Failed giving version HOT 1
- not compiled with crystal 0.17
- Parsing an unknown data structure. HOT 1
- not correct handle union type in mapping
- Support for nested Hash HOT 2
- Crash in mapping HOT 3
- Error while building with Crystal 0.21 HOT 1
- Add support for type extension HOT 4
- Support for missing or optional HOT 1
- Bad process on nil HOT 2
- The fix of #49 doesn't cover type restriction. HOT 1
- Broken stream reading, when type mismatch
- Typo
- Can't infer the type of instance variable '@var_present' HOT 5
- Shall not include Time conversion by default HOT 11
- Missing Timestamp extension type HOT 2
- have Serializable override inspect? HOT 2
- Type discriminator HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack-crystal.