Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going Beyond JSON: Representers, ASTs, and Code

C01828b92e05aea326a6e2dbeb59f373?s=47 Stephen Mizell
September 28, 2018
22

Going Beyond JSON: Representers, ASTs, and Code

This talk was given at REST Fest. It's a way to view JSON as a language rather than interact directly with the format. It also goes into the idea of the representer pattern.

C01828b92e05aea326a6e2dbeb59f373?s=128

Stephen Mizell

September 28, 2018
Tweet

Transcript

  1. Going Beyond JSON: Representers, ASTs, and Code

  2. Hi, I'm Stephen. I've thought way too much about JSON

    @Stephen_Mizell
  3. JSON { "name": "John Doe" }

  4. JSONx <?xml version="1.0" encoding="UTF-8"?> <json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"> <json:string

    name="name">John Doe</json:string> </json:object>
  5. Ruby { :name => "John Doe" } Or... json.name "John

    Doe"
  6. Refract { "element": "object", "content": [ { "element": "member", "content":

    { "key": { "element": "string", "content": "name" }, "value": { "element": "string", "content": "John Doe" } } } ] }
  7. These are all instruc.ons to build some internal structure of

    data.
  8. Abstract Source Tree A tree representa*on of the abstract syntac*c

    structure of source code wri5en in a programming language.
  9. Homoiconicity The code and representa.on of the code (AST) is

    the essen.ally the same
  10. Syntax + Structure + Seman.cs + Data Find ways to

    modularize and compose
  11. Instead of JSON -> Applica6on Logic Use JSON -> AST

    -> Applica2on Logic
  12. Treat your formats—JSON and others—as languages. Treat them as DSLs.

    Parse them to canonical structures decoupled from the format itself.
  13. Quick Demo

  14. Representers

  15. Coupling to HAL { "_links": { "item": { "href": "..."

    } } } // Get the item link doc._links.item.href
  16. Coupling to HAL { "_links": { "item": [ { "href":

    "..." }, { "href": "..." } ] } } // Broken! doc._links.item.href
  17. Using a Representer doc.links.hasRel('item'); doc.links.getByRel('item').first(); Decoupled from structure, syntax, and

    format.
  18. Using a Representer doc.links.add('item', 'http://example.com/1'); doc.links.add('item', 'http://example.com/2'); Decoupled from hypermedia

    formats. Easy to test what your model is independent of formats.
  19. Connascence of Type (CoT)

  20. Instead of JSON -> Applica6on Logic Use JSON -> Representer

    -> Applica5on Logic
  21. This provides a clearer boundary between your applica5on and the

    outside world
  22. Your data model is not your object model is not

    your resource model is not your message model. — Mike Amundsen
  23. Real world example GraphQL has an AST. OpenAPI does not.

  24. Takeaways • Define boundaries • We shouldn't have breaking changes—clients

    should just degrade • Consider inputs/outputs as languages • Think about canonical structures for crea?ng unity in a world of diversity