HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulecanonical-json-0.6.0.1Haskell2010

Text.JSON.Canonical

An implementation of Canonical JSON.

http://wiki.laptop.org/go/Canonical_JSON

The "canonical JSON" format is designed to provide repeatable hashes of JSON-encoded data. It is designed for applications that need to hash, sign or authenitcate JSON data structures.

The format is an extended subset of the normal JSON format.

Canonical JSON is parsable with any full JSON parser, and it allows whitespace for pretty-printed human readable presentation, but it can be put into a canonical form which then has a stable serialised representation and thus a stable hash.

The basic concept is that a file in the canonical JSON format can be read using parseCanonicalJSON. Note that this input file does not itself need to be in canonical form, it just needs to be in the canonical JSON format. Then the renderCanonicalJSON function is used to render into the canonical form. This is then the form that can be hashed or signed etc.

The prettyCanonicalJSON is for convenience to render in a human readable style, since the canoncal form eliminates unnecessary white space which makes the output hard to read. This style is again suitable to read using parseCanonicalJSON. So this is suitable to use for producing output that has to be later hashed or otherwise checked.

Known bugs/limitations:

  • Decoding/encoding Unicode code-points beyond U+00ff is currently broken

  • 5 types
  • 5 classes
  • 10 values

Types

3 declarations
datadata JSValue
#
Instances7Eq, Ord, Read, Show, NFData, FromJSON, …
  • Eq JSValueDefined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Ord JSValueDefined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Read JSValueDefined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Show JSValueDefined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • NFData JSValueDefined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Monad m => FromJSON m JSValueDefined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Class
  • Monad m => ToJSON m JSValueDefined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Class
newtypenewtype Int54
#

54-bit integer values

JavaScript can only safely represent numbers between -(2^53 - 1) and 2^53 - 1.

TODO: Although we introduce the type here, we don't actually do any bounds checking and just inherit all type class instance from Int64. We should probably define fromInteger to do bounds checking, give different instances for type classes such as Bounded and FiniteBits, etc.

Instances17Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Enum Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Eq Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Integral Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Data Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Num Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Ord Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Read Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Real Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Show Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Ix Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Bits Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • FiniteBits Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • Storable Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • PrintfArg Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Types
  • ReportSchemaErrors m => FromJSON m Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Class
  • Monad m => ToJSON m Int54Defined in canonical-json-0.6.0.1 · Text.JSON.Canonical.Class
newtypenewtype JSString
#

Canonical JSON strings are in fact just bytes.

Instances13Eq, Ord, Read, Show, IsString, Semigroup, …

Parsing and printing

3 declarations

Render a JSON value in canonical form. This rendered form is canonical and so allows repeatable hashes.

For pretty printing, see prettyCanonicalJSON.

NB: Canonical JSON's string escaping rules deviate from RFC 7159 JSON which requires

"All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)."

Whereas the current specification of Canonical JSON explicitly requires to violate this by only escaping the quotation mark and the reverse solidus. This, however, contradicts Canonical JSON's statement that "Canonical JSON is parsable with any full JSON parser"

Consequently, Canonical JSON is not a proper subset of RFC 7159.

Type classes

8 declarations
classclass ToJSON (m :: Type -> Type) a where
#

Methods

Instances6ToJSON
classclass FromJSON (m :: Type -> Type) a where
#

Methods

Instances6FromJSON

Utility

6 declarations