thoth-org / Thoth.Json

Library for working with JSON in a type safe manner, this libs is targeting Fable

Home Page:https://thoth-org.github.io/Thoth.Json/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`Decode.fromString Decode.value ""` returns `Ok null` instead of an error

njlr opened this issue · comments

commented

Decode.fromString Decode.value "" returns Ok null instead of an error.

Issue is demonstrated by this script:

#r "nuget: Thoth.Json.Net, 11.0.0"

open Thoth.Json.Net

let tryNormalizeJson (maybeJson : string) : string =
  match Decode.fromString Decode.value maybeJson with
  | Ok j -> Encode.toString 2 j
  | Error _ -> maybeJson

let myJson = ""
let normalized = tryNormalizeJson myJson

printfn $"%s{normalized}"

(*

System.NullReferenceException: Object reference not set to an instance of an object.
   at Thoth.Json.Net.Encode.toString(Int32 space, JToken token)
   at <StartupCode$FSI_0002>.$FSI_0002.main@() in /home/runner/Scratch.fsx:line 12
Stopped due to error

*)

Hum, this is because Decode.value does no checks.

It is defined as let value _ v = Ok v meaning that anything you pass to it succeed. It was added because Elm implementation had it.

So I am unsure what to do here. TBH honest I never used Decode.value 🙈