Skip to content

feat/decode-value#231

Draft
njlr wants to merge 3 commits intothoth-org:mainfrom
njlr:feat/decode-value
Draft

feat/decode-value#231
njlr wants to merge 3 commits intothoth-org:mainfrom
njlr:feat/decode-value

Conversation

@njlr
Copy link
Copy Markdown
Contributor

@njlr njlr commented Feb 3, 2026

This PR brings back Decode.value and Encode.value.

These allow JSON to be transformed in and out of a backend-agnostic JSON representation in Thoth.Json.Core.

With these, the user can say "any valid JSON would go here".

For example:

open Thoth.Json.Core


type Message = 
  {
    Sender : string
    Subject : string
    Metadata : Json
  }


[<RequireQualifiedAccess>]
module Message =

  let decode : Decoder<Message> =
    Decode.object 
      (fun get -> 
        {
          Sender = get.Required.Field "sender" Decode.string
          Subject = get.Required.Field "subject" Decode.string
          Metadata = get.Required.Field "metadata" Decode.value
        })

I have marked this as draft because there is a decision to make around integer precision:

  • Technically JSON spec allows arbitrarily large/precise numbers.
  • Most implementations restrict numbers to be a float.
  • To be fully general, we should represent numbers using something like a BigDecimal.
    • However, BigDecimal is not part of the .NET standard libraries.
    • Or perhaps a string... but I expect most users would want a number to work with.
    • Or perhaps we could include our own number representation in the library?
  • The current choice is float.
  • IDecoderHelpers and IEncoderHelpers, as they stand now, do not support arbitrary numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant