Skip to content

feat: add support for Encode.value and Decode.value#231

Merged
MangelMaxime merged 4 commits intothoth-org:mainfrom
njlr:feat/decode-value
Apr 18, 2026
Merged

feat: add support for Encode.value and Decode.value#231
MangelMaxime merged 4 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.

@MangelMaxime MangelMaxime marked this pull request as ready for review April 18, 2026 09:52
@MangelMaxime MangelMaxime changed the title feat/decode-value feat: add support for Encode.value and Decode.value Apr 18, 2026
@MangelMaxime MangelMaxime merged commit fd88275 into thoth-org:main Apr 18, 2026
3 checks passed
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.

2 participants