xyncro / chiron

JSON for F#
https://xyncro.tech/chiron
MIT License
173 stars 41 forks source link

GetHashCode infinite loop #106

Open ingted opened 1 year ago

ingted commented 1 year ago

GetHashCode would cause infinite loop, please consider to fix it.

My workaround:

    override x.GetHashCode() =

        let rec jsonValue (this:Json) =
            match this with
            | Array  v -> box (v |> List.map (fun j -> jsonValue j) |> Array.ofList)
            | True     -> box true
            | False    -> box false
            | Null     -> null
            | Number v ->
                match System.Int32.TryParse v with
                | true, i -> box i
                | false, _ ->
                    match System.Int64.TryParse v with
                    | true, i64 -> box i64
                    | false, _ ->
                        match bigint.TryParse v with
                        | true, bi -> box bi
                        | false, _ ->
                            match System.Decimal.TryParse v with
                            | true, d -> box d
                            | false, _ -> 
                                match System.Single.TryParse v with
                                | true, s -> box s
                                | false, _ ->
                                    match System.Double.TryParse v with
                                    | true, db -> box db
                                    | false, _ -> box v

            | Object (WriteObject v) ->
                let s = v |> Seq.map (fun (k, j) -> k, jsonValue j)
                s |> Map |> box
            | Object (ReadObject (_, v)) ->
                let s = v |> Seq.map (fun kvp -> kvp.Key, jsonValue kvp.Value)
                s |> Map |> box
            | String v -> box v

        jsonValue x |> hash