Giter Site home page Giter Site logo

jay's Introduction

Jay

NuGet Version Build Status

The aim of this library was to take the core JSON parser from the amazing FSharp.Data project, and modernize/simplify it's API.

Getting Started

Install the Jay NuGet package:

PM>  Install-Package Jay

Or using the dotnet CLI

dotnet add package Jay

An Example

Let's consider a stripped down Tweet object.

Note: the user and entities properties have been removed for clarity.

{
 "created_at": "Wed Oct 10 20:19:24 +0000 2018",
 "id": 1050118621198921728,
 "id_str": "1050118621198921728",
 "text": "To make room for more expression, we will now count all emojis as equal—including those with gender‍‍‍ ‍‍and skin t… https://t.co/MkGjXf9aXm", 
}

In order to work with this in our F# program, we'll first need to create a record type.

type Tweet = 
    {
        CreatedAt : DateTimeOffset
        Id        : int64
        IdStr     : string
        Text      : string
    }

Next we'll define a module with the same name, Tweet, and a function called fromJson to consume our JSON and return a Tweet record.

type Tweet = 
    {
        CreatedAt : DateTimeOffset
        Id        : int64
        IdStr     : string
        Text      : string
    }

module Tweet =
    let fromJson (json : Json) =
        {
            CreatedAt = json?created_at.AsDateTimeOffset()
            Id        = json?id.AsInt64()
            IdStr     = json?idStr.AsString()
            Text      = json?text.AsString()
        }


let tweetJson = ... // JSON from above
let tweet = 
    tweetJson
    |> Json.parse
    |> Tweet.fromJson

Finally, we'll create another function toJson to convert our record back into JSON represented as an abstract syntax tree.

type Tweet = 
    {
        CreatedAt : DateTimeOffset
        Id        : int64
        IdStr     : string
        Text      : string
    }

module Tweet =
    let fromJson (json : Json) =
        {
            CreatedAt = json?created_at.AsDateTimeOffset()
            Id        = json?id.AsInt64()
            IdStr     = json?idStr.AsString()
            Text      = json?text.AsString()
        }

    let toJson (tweet : Tweet) =
        JObject 
            [|
                "created_at", JString (tweet.CreatedAt.ToString())
                "id",         JNumber (float tweet.Id)
                "id_str",     JString tweet.IdStr
                "text",       JString tweet.Text
            |]

let tweetJson = ... // JSON from above
let tweet = 
    tweetJson
    |> Json.parse
    |> Tweet.fromJson

let json =
    tweet
    |> Tweet.toJson
    |> Json.serialize

And that's it!

Find a bug?

There's an issue for that.

License

Built with ♥ by Pim Brouwers in Toronto, ON. Licensed under Apache License 2.0.

jay's People

Contributors

pimbrouwers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kaashyapan numpsy

jay's Issues

Reading Json that contains comments

Hi,

It looks to me like Jay doesn't like parsing Json that contains comments...
Those might not be 'officially' supported, but I see that FSharp.Data was changed to ignore them rather than breaking in fsprojects/FSharp.Data#1448, so would there be any interest in trying to port that change over to here?

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.