Zaid-Ajaj / LiteDB.FSharp

Advanced F# Support for LiteDB, an embedded NoSql database for .NET with type-safe query expression through F# quotations
MIT License
181 stars 20 forks source link

Added UseCustomJsonConverters method to use custom json converters #18

Closed humhei closed 6 years ago

humhei commented 6 years ago

Fixes #17

Zaid-Ajaj commented 6 years ago

Again, feel free to merge or let me know so I can merge/publish it

humhei commented 6 years ago

It seems that LiteDB (de)serialize directly with bson data and entity type(Also for properties) While we going through a json data middleware by jsonconverters

Maybe i only want to be able to use custom json converter(Inherited from FsharpJsonConverter) So i can do more custom convertions

Zaid-Ajaj commented 6 years ago

@humhei I honestly wouldn't know why someone would want to customize the converter, we use JSON just as an internal layer to make the conversion easy to/from bson. In anycase, I will publish the new version asap

humhei commented 6 years ago

In my special case I use it for retrieve template datas.

I generate data from .fsx files and also sometime retrive from it I also sometimes retrieve data from database

I use this special converters to specfic The logic of retrieving where from or generating new datas

    [<RequireQualifiedAccess>]
    type RetrieveFrom =
        | FromFsx = 0
        | FromDB = 1

    [<RequireQualifiedAccess>]
    type Settable<'T> =
        | Auto
        | Manual of 'T
    [<CustomEquality;NoComparison>]
    type AutoSet<'T when 'T : equality> =
        {
            Kind: Settable<'T>
            RetrieveFrom: RetrieveFrom
        }
        override x.Equals(y) =
            match y with 
            | :? AutoSet<'T> as y -> 
                if x.RetrieveFrom = y.RetrieveFrom then 
                    x.Kind = y.Kind
                else
                    let fromFsx,fromDB =
                        match x.RetrieveFrom,y.RetrieveFrom with 
                        | RetrieveFrom.FromFsx,RetrieveFrom.FromDB -> x,y
                        | RetrieveFrom.FromDB,RetrieveFrom.FromFsx -> y,x
                        | _, _ -> Logger.invalidToken()

                    match fromFsx.Kind,fromDB.Kind with 
                    | Settable.Auto _, Settable.Auto -> false
                    | Settable.Manual _, Settable.Auto -> false
                    | Settable.Auto, Settable.Manual _ -> true
                    | Settable.Manual m1, Settable.Manual m2 -> m1 = m2

            | _ -> false

        override x.GetHashCode() = 
            Logger.notImplemented()

    type AutoSetHangtagTemplate = AutoSet<HangtagTemplate option>
    let converters = 
        let cv = new MyJsonConverter()
        cv.RegisterType<AutoSetHangtagTemplate>(fun v ->
            {v with RetrieveFrom = RetrieveFrom.FromDB}
        )
        cv
humhei commented 6 years ago

Maybe i am in wrong design patterns But i want to try it and make it flying for a while