Giter Club home page Giter Club logo

Comments (4)

mausch avatar mausch commented on May 30, 2024

Hi! No question is dumb.

parseJSON does take a string as parameter, but it returns a Choice<'a, string>.
If it's Choice1Of2 then the parsing was successful and you have the parsed value.
If it's Choice2Of2 then there was some parsing error and you get the error message as a string, which you can use to throw as an exception, display to the user or whatever you like.

Does that make sense?

To create a "singleton" instance of Config I recommend using F#'s lazy expressions

Cheers

from fleece.

MangelMaxime avatar MangelMaxime commented on May 30, 2024

Thanks for the answer and the tips.

I think I understand an then if the result is successful then I can pass it to FromJson of ConfigJson ?

from fleece.

mausch avatar mausch commented on May 30, 2024

No, FromJson is called implicitly by parseJSON, you don't need to call it
in your code.
On 31 May 2016 13:35, "Maxime Mangel" [email protected] wrote:

Thanks for the answer and the tips.

I think I understand an then if the result is successful then I can pass
it to FromJson of ConfigJson ?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#16 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAFz2jjEDAcEpHnz4cBGJrefwO0sH8yTks5qHCrzgaJpZM4IqcXv
.

from fleece.

MangelMaxime avatar MangelMaxime commented on May 30, 2024

Ok :) Thanks for the help.

I am letting here my problem solved.

namespace Server

open System
open Fleece
open Fleece.Operators
open FSharpPlus

type ConfigJson = {
    RelativeRoot: string
    Server: string
    Database: string
    Uid: string
    Pwd: string
}

type ConfigJson with 
    static member Create relativeRoot server database uid pwd= 
        { 
            ConfigJson.RelativeRoot = relativeRoot;
            ConfigJson.Server = server;
            ConfigJson.Database = database;
            ConfigJson.Uid = uid;
            ConfigJson.Pwd = pwd;
        }

    static member FromJSON (_:ConfigJson) = 
        function
        | JObject o -> ConfigJson.Create <!> (o .@ "relativeRoot") <*> (o .@ "server") <*> (o .@ "database") <*> (o .@ "uid") <*> (o .@ "pwd")
        | x -> Failure (sprintf "Expected ConfigJson, found %A" x)

module Config =

    let config: ConfigJson =
        let data: ConfigJson ParseResult = parseJSON(System.IO.File.ReadAllText("server.json"))

        match data with
        | Success a -> a
        | Failure e -> failwith "Error when parsing json file"

from fleece.

Related Issues (20)

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.