GithubHelp home page GithubHelp logo

sparser's Introduction

sParser

Parser Combinator implemented in Scala

The AST for JSON in Scala is represented as

case object JNull extends JSON
case class JNumber(get:Double) extends JSON
case class JString(get:String) extends JSON
case class JBool(get: Boolean) extends JSON
case class JArray(get:IndexedSeq[JSON]) extends JSON
case class JObject(get: Map[String, JSON]) extends JSON

Parsing some JSON file as examples

This is an example json data

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}    

We name it as "example" and run the parser

println(run(jparse)(example))

We get

Right(JObject(Map("widget" -> JObject(Map("debug" -> JString("on"), "window" -> JObject(Map("title" -> JString("Sample Konfabulator Widget"), "name" -> JString("main_window"), "width" -> JNumber(500.0), "height" -> JNumber(500.0))), "image" -> JObject(HashMap("vOffset" -> JNumber(250.0), "alignment" -> JString("center"), "src" -> JString("Images/Sun.png"), "name" -> JString("sun1"), "hOffset" -> JNumber(250.0))), "text" -> JObject(HashMap("vOffset" -> JNumber(100.0), "data" -> JString("Click Here"), "size" -> JNumber(36.0), "alignment" -> JString("center"), "onMouseUp" -> JString("sun1.opacity = (sun1.opacity / 100) * 90;"), "style" -> JString("bold"), "name" -> JString("text1"), "hOffset" -> JNumber(250.0))))))))

The main class for the JSON parser is in the file JSON.scala

sparser's People

Contributors

maboroshichan avatar

Watchers

James Cloos avatar  avatar

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.