HNHacker News
TopNewBestAskShowJobs

beerkg

118 karma · joined May 17, 2022

submissionscomments
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
Shale uses Ruby's standard library parsers out of the box, so if you keep your Ruby up to date with security updates you will be good. Also others in this thread suggested to set minimal version on dependencies, so I'll probably do that in the future version.
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
If you don't define attributes explicitly on the model, Shale will ignore them.

Regarding attributes that you defined but still don't want to be assigned, you should probably filter them before passing them to Shale, or alternatively filter them with Shale before passing them further down the stack (e.g to ActiveRecord)

beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
Documentation site was based on https://vuepress.vuejs.org/ but it evolved so much I dropped Vue all together and wen't with plain HTML instead. I must have left that meta tag from the early days.

Regarding Vue I use it daily at my job, great library :)

beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
HappyMapper was actually an inspiration for Shale. If it had support for JSON, Shale probably wouldn't be created :)
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
I like it actually, using POROs (or any class for that matter) is definitely a big advantage. Maybe I implement something like that for version 2 :)
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
It's a custom template I created (based on https://vuepress.vuejs.org/), because I couldn't find anything that simple. The source code is available on https://github.com/kgiszczak/shale-website

Interactive examples are powered by https://opalrb.com/

beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
Yeah, Shale supports generating models from JSON Schema for now, XML is work in progress and should be ready in two or three weeks.
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
I have a mixed feelings about this, standard library's vulnerabilities are part of Ruby's vulnerabilities, so you would update your Ruby version anyway. But you're right specifing version explicitly would prevent this.
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
I totally agree with your points, but this approach has one big advantage - it's dead simple - define attributes and mapping and you're good to go.
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
Ah, I messed up the example, Person class definition should look like this:

  class Person < Shale::Mapper
    attribute :first_name, Shale::Type::String
    attribute :last_name, Shale::Type::String
    attribute :age, Shale::Type::Integer
    attribute :married, Shale::Type::Boolean, default: false
    attribute :hobbies, Shale::Type::String, collection: true
    attribute :address, Address
  end
And the JSON used for parsing also should contain those atttributes, like:

  {
    "first_name": "John",
    "last_name": "Doe",
    "age": 30,
    "married": false,
    "hobbies": ["Singing", "Dancing"],
    "address": {
      "street": "Oxford Street",
      "city": "London"
    }
  }
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
I'm glad you like it. One clarification - Nokogiri is not required by default, you have to explicitly require "shale/adapter/nokogiri" to use it. If you don't Shale will use REXML which comes from Ruby's standard library.
beerkg··on Show HN: Shale – a Ruby object mapper and serializer for JSON, YAML and XML
Hi, I released Shale, a Ruby gem that allows you to parse JSON, YAML and XML and convert it into Ruby data structures, as well as serialize your Ruby data model to JSON, YAML or XML.

Features:

- convert JSON, XML or YAML into Ruby data model

- serialize data model to JSON, XML or YAML

- generate JSON and XML Schema from Ruby models

- compile JSON Schema into Ruby models (compiling XML Schema is a work in progress)

A quick example so you can get a feel of it:

  require 'shale'

  class Address < Shale::Mapper
    attribute :street, Shale::Type::String
    attribute :city, Shale::Type::String
  end

  class Person < Shale::Mapper
    attribute :first_name, Shale::Type::String
    attribute :last_name, Shale::Type::String
    attribute :address, Address
  end

  # parse data and convert it into Ruby data model
  person = Person.from_json(<<~JSON) # or .from_xml / .from_yaml
  {
    "first_name": "John",
    "last_name": "Doe",
    "address": {
      "street": "Oxford Street",
      "city": "London"
    }
  }
  JSON

  # It will give you
  # =>
  #  #<Person:0xa0a4
  #    @address=#<Address:0xa0a6
  #      @city="London",
  #      @street="Oxford Street",
  #      @zip="E1 6AN">,
  #    @age=50,
  #    @first_name="John",
  #    @hobbies=["Singing", "Dancing"],
  #    @last_name="Doe",
  #    @married=false>

  # serialize Ruby data model to JSON
  Person.new(
    first_name: 'John',
    last_name: 'Doe',
    address: Address.new(street: 'Oxford Street', city: 'London')
  ).to_json # or .to_xml / .to_yaml
Source code is available on GitHub: https://github.com/kgiszczak/shale