• Stars
    star
    39
  • Rank 670,704 (Top 14 %)
  • Language
    Crystal
  • License
    ISC License
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

(De)serialize any Crystal object - out of the box. Supports JSON, YAML and Byte format.

Crystalizer

CI Documentation ISC

[De]serialize any Crystal object - out of the box. Supports JSON, YAML and Byte format.

Features

  • [De]serialize anything, "out-of-the-box"
  • Advanced serialization with annotations, but not required
  • Shared annotations for all formats (JSON, YAML...)

Implementation bonus: no monkey patching involved :) (no method pollution on objects)

Installation

Add the dependency to your shard.yml:

dependencies:
  crystalizer:
    github: j8r/crystalizer

Documentation

https://j8r.github.io/crystalizer

Usage

Basic

require "crystalizer/json"
require "crystalizer/yaml"

struct Point
  getter x : Int32
  @[Crystalizer::Field(key: "Y")]
  getter y : String

  def initialize(@x, @y)
  end
end

point = Point.new 1, "a"

{Crystalizer::YAML, Crystalizer::JSON}.each do |format|
  puts format
  string = format.serialize point
  puts string
  puts format.deserialize string, to: Point
end

Result:

Crystalizer::YAML
---
x: 1
Y: a
Point(@x=1, @y="a")
Crystalizer::JSON
{
  "x": 1,
  "Y": "a"
}
Point(@x=1, @y="a")

Any

Parsing any type, and converting to JSON/YAML.

require "crystalizer/json"
require "crystalizer/yaml"

yaml_string = <<-E
one: 1
two: 2
sub:
  ary:
  - one
  - 2
E

yaml_any = Crystalizer::YAML.parse yaml_string
puts yaml_any

json_string = Crystalizer::JSON.serialize yaml_any
puts json_string

json_any = Crystalizer::JSON.parse json_string
puts Crystalizer::YAML.serialize json_any

Result:

{"one" => 1, "two" => 2, "sub" => {"ary" => ["one", 2]}}
{
  "one": 1,
  "two": 2,
  "sub": {
    "ary": [
      "one",
      2
    ]
  }
}
---
one: 1
two: 2
sub:
  ary:
  - one
  - 2

User-defined serialization

Allows to define custom serialization and deserialization for a given type.

struct MyType
  include Crystalizer::Type

  def initialize(@i : Int32)
  end

  def self.deserialize(deserializer : Crystalizer::Deserializer)
    new deserializer.deserialize to: Int32
  end

  def serialize(serializer : Crystalizer::Serializer) : Nil
    serializer.serialize @i
  end
end

Note

Annotations are similar to the stdlib's Serializable, but all features are not yet fully implemented.

License

Copyright (c) 2020-2023 Julien Reichardt - ISC License

More Repositories

1

dockerfiles

Repository for my dockerfiles
Dockerfile
117
star
2

cride

A light CLI text editor/IDE written in Crystal
Crystal
49
star
3

clicr

A simple declarative command line interface builder
Crystal
29
star
4

con

A simple, fast and readable JSON-compatible serialization format
Crystal
23
star
5

pool.cr

A simple thread-safe generic pool.
Crystal
14
star
6

crystal-autobind

Automatic C bindings generator for Crystal
Crystal
14
star
7

crystal-object-send

Interpret a String to an Object method call
Crystal
13
star
8

tail.cr

Tailing library for Crystal - get and/or follow the end of a file/IO
Crystal
10
star
9

error.cr

Efficient errors without raising exceptions - no expensive stack unwinding
Crystal
8
star
10

libcrown

Library for Unix users, groups and passwords manipulation in Crystal
Crystal
7
star
11

semantic_compare

Compare semver versions using semantic expressions similar to ones from npm's semver implementation
Crystal
5
star
12

crystal-byte-protocol

A byte protocol for Crystal
Crystal
4
star
13

minetest-entity_modifier

Modify the model and size of entities, including players.
Lua
4
star
14

crystalxd

Crystal client for the LXD REST API
Crystal
4
star
15

sherd

Crystal package manager
Crystal
3
star
16

dotfiles

My Git-versioned dotfiles
CSS
2
star
17

crystal-open-simplex-noise

2D, 3D, and 4D open simplex noise in crystal.
Crystal
2
star
18

urandgen.sh

Generate random numbers in base32 or base64 with /dev/urandom
Shell
2
star
19

SimpleShellMenu

A minimal POSIX shell menu in 50 lines that only use stty, dd and od
Shell
2
star
20

unicode_blocr

Identify the Unicode block to which a character belong.
Crystal
2
star
21

dstats

Simple docker stats to get performance stats of your containers
Shell
2
star
22

DustShip

A little POSIX compliant shell game where the goal is to collect asteroids with a ship
Shell
2
star
23

Orball

A Cordova/mobile compatible experiment game using Phaser with CoffeeScript - https://j8r.github.io/Orball/
JavaScript
2
star
24

js-byte-format

Encode/decode JavaScript datatypes (String, Number, Object) to/from bytes
JavaScript
1
star
25

dynany

Dynamic JSON/YAML mapping manipulation - extends Any
Crystal
1
star