Git Product home page Git Product logo

jsondsl's Introduction

JSONDSL

Define and compose JSON with Swift enums. Just exploring the concepts of DSLs and pushing the boundaries of usefulness. This is just a playground to play around with and can be improved in many ways.

What's in this playground?

This playground includes a single recursive enum and some free functions to define JSON objects entirely in Swift. A future iteration could parse the object definition into a dictionary [String: Any] and use JSONSerialization or an Encoder to create JSON files.

Current state:

let doc = object([
    .string("name", "Doc"),
    .integer("dogs", 1)
])

let marty = object("bestFriend",[
    .string("name", "Marty"),
    .integer("dogs", 0)
])

object([
    string("model", "Dolorian"),
    bool("isNew", false),
    integer("seats", 2),
    marty,
    array("owners", [
        doc
    ])
])

Produces the following JSON Object:

{
  "model": "Dolorian",
  "isNew": false,
  "seats": 2,
  "bestFriend": {
    "name": "Marty",
    "dogs": 0
  },
  "owners": [
    {
      "name": "Doc",
      "dogs": 1
    }
  ]
}

After calling the render function. Future iteration could also include a validator to make sure that one can only create valid JSON objects which is much safer than relying on just a simple String.

Making JSONObject conform to ExpressibleByArrayLiteral reduces clutter:

let jsonString_expressible_by_array_literal = render(
    [
        string("model", "Dolorian"),
        bool("isNew", false),
        integer("seats", 2),
        marty,
        array("owners", [
            doc
        ])
    ]
)

Why?

This is just a quick fun project to explore free functions, DSLs, and composition. It can be quite useful in future iterations to generate test data and have it checked by a compiler instead of just strings.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.