GithubHelp home page GithubHelp logo

yeti-json's Introduction

This module defines functions that can be used from Yeti to parse JSON input string in a typesafe way.
To use these functions, you just need to load the "json" module:

    {readJson, readJson'} = load json;
    
Please also ensure that you put the following jars in the classpath:
	- json_simple-1.1.jar
	- json-1.0-SNAPSHOT.jar

If you want to use Yeti REPL then you can type the following command line:

    $ java -cp json_simple-1.1.jar;json-1.0-SNAPSHOT.jar;yeti.jar yeti.lang.compiler.yeti

Usage examples follow:

    > x1 = readJson {a = "", b = 0, c = false} '{"a": "abc", "b": 100, "c": true}';

The first argument acts as a "type template" to which the second argument must be mappable after JSON parsing. An exception will occur if the template type doesn't match the type of the parsed input.

Mapping principles follow:

    JSON type     Yeti type    Comments
    ---------     ---------    --------
    object        struct       all fields from the yeti target struct must exist 
                               in JSON object
    list          list         if yeti target list is not empty, then item type 
                               must match with JSON list items type
    string        string	
    number        number
    boolean       boolean
    
    anything      Some 'a     'a must be convertible to type "anything"
    null          Some 'a     in this case, Some 'a is replaced with None () in the 
                              result
    null          list<'a>    in this case, the result is the empty list []

The mapping is recursive, meaning that if JSON input defines list of lists or structs within structs, then the template should be the same.

Other usage examples with lists:

    > x2 = readJson [] '[1, 2, 3]';
    > x2 = readJson [0] '[1, 2, 3]';

The second function takes a list of variants that allows you to specify several different templates that the JSON input may map to; you would use this function as in the following example:

    > x3 = readJson' [A {a = "", b = 0}, B {b = "", c = false}] '{"a": "abc", "b": 123}';

This allows you to use pattern matching when the JSON input that you get may be of different types.

yeti-json's People

Watchers

 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.