All Projects → trailblazer → representable

trailblazer / representable

Licence: MIT license
Maps representation documents from and to Ruby objects. Includes JSON, XML and YAML support, plain properties and compositions.

Projects that are alternatives of or similar to representable

Thorsserializer
C++ Serialization library for JSON
Stars: ✭ 241 (-65.02%)
Mutual labels:  yaml, json-serialization, json-parser
Mir Ion
WIP, use libmir/asdf package for now
Stars: ✭ 78 (-88.68%)
Mutual labels:  json-serialization, json-parser
Jsondoc
JSON object for Delphi based on IUnknown and Variant
Stars: ✭ 20 (-97.1%)
Mutual labels:  json-serialization, json-parser
Bfj
MOVED TO GITLAB
Stars: ✭ 164 (-76.2%)
Mutual labels:  json-serialization, json-parser
Fastjson
A fast JSON parser/generator for Java.
Stars: ✭ 23,997 (+3382.87%)
Mutual labels:  json-serialization, json-parser
Json
JSON for Modern C++
Stars: ✭ 27,824 (+3938.32%)
Mutual labels:  json-serialization, json-parser
Spotify Json
Fast and nice to use C++ JSON library.
Stars: ✭ 145 (-78.96%)
Mutual labels:  json-serialization, json-parser
kson
A Java serialization/deserialization library to convert Java Objects into json and back, faster and powerful then Gson.
Stars: ✭ 25 (-96.37%)
Mutual labels:  json-serialization, json-parser
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (-87.52%)
Mutual labels:  yaml, json-parser
counsel-jq
Traverse complex JSON and YAML structures with live feedback
Stars: ✭ 99 (-85.63%)
Mutual labels:  yaml, json-parser
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+10.16%)
Mutual labels:  yaml, xml-parser
Python Rapidjson
Python wrapper around rapidjson
Stars: ✭ 417 (-39.48%)
Mutual labels:  json-serialization, json-parser
Jsoncons
A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
Stars: ✭ 400 (-41.94%)
Mutual labels:  json-serialization, json-parser
Spray Json
A lightweight, clean and simple JSON implementation in Scala
Stars: ✭ 917 (+33.09%)
Mutual labels:  json-serialization, json-parser
Easyjson
Fast JSON serializer for golang.
Stars: ✭ 3,512 (+409.72%)
Mutual labels:  json-serialization, json-parser
Waargonaut
JSON decoding/encoding/manipulation library.
Stars: ✭ 82 (-88.1%)
Mutual labels:  json-serialization, json-parser
libstud-json
JSON pull-parser/push-serializer library for C++
Stars: ✭ 20 (-97.1%)
Mutual labels:  json-serialization, json-parser
JsonFormatter
Easy, Fast and Lightweight Json Formatter. (Serializer and Deserializer)
Stars: ✭ 26 (-96.23%)
Mutual labels:  json-serialization, json-parser
Json Dry
🌞 JSON-dry allows you to serialize & revive objects containing circular references, dates, regexes, class instances,...
Stars: ✭ 214 (-68.94%)
Mutual labels:  json-serialization, json-parser
domino-jackson
Jackson with Annotation processing
Stars: ✭ 46 (-93.32%)
Mutual labels:  json-serialization, json-parser

Representable

Representable maps Ruby objects to documents and back.

Build Status Gem Version

In other words: Take an object and decorate it with a representer module. This will allow you to render a JSON, XML or YAML document from that object. But that's only half of it! You can also use representers to parse a document and create or populate an object.

Representable is helpful for all kind of mappings, rendering and parsing workflows. However, it is mostly useful in API code. Are you planning to write a real REST API with representable? Then check out the Roar gem first, save work and time and make the world a better place instead.

Full Documentation

Representable comes with a rich set of options and semantics for parsing and rendering documents. Its full documentation can be found on the Trailblazer site.

Example

What if we're writing an API for music - songs, albums, bands.

class Song < OpenStruct
end

song = Song.new(title: "Fallout", track: 1)

Defining Representations

Representations are defined using representer classes, called _decorator, or modules.

In these examples, let's use decorators

class SongRepresenter < Representable::Decorator
  include Representable::JSON

  property :title
  property :track
end

In the representer the #property method allows declaring represented attributes of the object. All the representer requires for rendering are readers on the represented object, e.g. #title and #track. When parsing, it will call setters - in our example, that'd be #title= and #track=.

Rendering

Mixing in the representer into the object adds a rendering method.

SongRepresenter.new(song).to_json
#=> {"title":"Fallout","track":1}

Parsing

It also adds support for parsing.

song = SongRepresenter.new(song).from_json(%{ {"title":"Roxanne"} })
#=> #<Song title="Roxanne", track=nil>

Note that parsing hashes per default does require string keys and does not pick up symbol keys.

Collections

Let's add a list of composers to the song representation.

class SongRepresenter < Representable::Decorator
  include Representable::JSON

  property :title
  property :track
  collection :composers
end

Surprisingly, #collection lets us define lists of objects to represent.

Song.new(title: "Fallout", composers: ["Stewart Copeland", "Sting"]).
  extend(SongRepresenter).to_json

#=> {"title":"Fallout","composers":["Stewart Copeland","Sting"]}

And again, this works both ways - in addition to the title it extracts the composers from the document, too.

Nesting

Representers can also manage compositions. Why not use an album that contains a list of songs?

class Album < OpenStruct
end

album = Album.new(name: "The Police", songs: [song, Song.new(title: "Synchronicity")])

Here comes the representer that defines the composition.

class AlbumRepresenter < Representable::Decorator
  include Representable::JSON

  property :name
  collection :songs, decorator: SongRepresenter, class: Song
end

Inline Representers

If you don't want to maintain two separate modules when nesting representations you can define the SongRepresenter inline.

class AlbumRepresenter < Representable::Decorator
  include Representable::JSON

  property :name

  collection :songs, class: Song do
    property :title
    property :track
    collection :composers
  end
end

More

Representable has many more features and can literally parse and render any kind of document to an arbitrary Ruby object graph.

Please check the official documentation for more.

Installation

The representable gem runs with all Ruby versions >= 2.4.0. t

gem 'representable'

Dependencies

Representable does a great job with JSON, it also features support for XML, YAML and pure ruby hashes. But Representable did not bundle dependencies for JSON and XML.

If you want to use JSON, add the following to your Gemfile:

gem 'multi_json'

If you want to use XML, add the following to your Gemfile:

gem 'nokogiri'

Copyright

Representable started as a heavily simplified fork of the ROXML gem. Big thanks to Ben Woosley for his extremely inspiring work.

  • Copyright (c) 2011-2020 Nick Sutterer [email protected]
  • ROXML is Copyright (c) 2004-2009 Ben Woosley, Zak Mandhro and Anders Engstrom.

Representable is released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].