Serializers

Serializers

and You

Serializers

and Rails

Build a bridge between a Rails and single-page web app

Do you like things that are...

  • Human readable?
  • Easily Computer Parsable?
  • Lightweight?
  • Fast?

What it isn't:

  • A programming language
  • A markup language
    • e.g. Extensible Markup Language (XML)

What it is:

  • Lightweight data-interchange format based on Javascript's object notation
  • A file with data formatted to be readable by other programming languages
  • Stored in plain text format

Javascript Object Notation

var object = {

    key: value

}

Arrays
Objects

Primitives

var planet = {

    "name": "Saturn",

    "planet_number": 6

}

var polygons = {

    triangle: {

        "sides": 3,

        "color": "blue",

        "types": [

            {"name": "equilateral", "regular": true},

            {"name": "isosceles", "regular": false},

            {"name": "scalene", "regular": false}

        ]

    },

    quadrilateral: {

        "sides": 4,

        "some_other_thing": null

     }

}

Used to generate a JSON API in Rails

Active Model Serializers

Wait.  What are serializers again?

  • Serializers convert objects to store or transmit them to memory, a database, or a file.
  • Serializers save the state of an object in order to be able to recreate it when needed.
  • In our case, Active Model serializers will convert data from our database into JSON

Swizzling

user_serializer.rb

Attributes (as a list of symbols)

users_controller.rb

{

  "user": {

    "id":10,

    "email":"brigitte@allovue.com",

    "admin":true

    "principal":false,

    "staff":false,

    "first_name":"Brigitte",

    "last_name":"Warner",

    "phone":null,

   "budget_lea_ids" [4]

  }

}

Define additional attributes:

Side Note:

10 E North Ave

@BrigitteWarner

Software Developer at Allovue

We're Hiring!

http://allovue.com/about

careers@allovue.com

Serializers

By brigittewarner

Serializers

  • 867