gram
Safe HaskellNone
LanguageHaskell2010

Gram.JSON

Description

JSON serialization and deserialization for Pattern Subject.

This module provides canonical JSON representation of PatternSubject with bidirectional conversion support. The format is designed for interoperability and can serve as an exchange format between different gram implementations.

JSON Format

Patterns are represented as:

{ "value": Subject, "elements": [Pattern] }

Subjects are represented as:

{ "symbol": String, "labels": [String], "properties": {...} }

Value types use native JSON for simple types and discriminated objects for complex types (Symbol, TaggedString, Range, Measurement).

Synopsis

Serialization

patternToValue :: Pattern Subject -> Value Source #

Convert a Pattern to aeson Value (for serialization)

subjectToValue :: Subject -> Value Source #

Convert a Subject to aeson Value (for serialization)

valueToJSON :: Value -> Value Source #

Convert a Subject.Value to aeson Value (for serialization)

Deserialization

patternFromValue :: Value -> Parser (Pattern Subject) Source #

Parse a Pattern from aeson Value (for deserialization)

subjectFromValue :: Value -> Parser Subject Source #

Parse a Subject from aeson Value (for deserialization)

valueFromJSON :: Value -> Parser Value Source #

Parse a Subject.Value from aeson Value (for deserialization)

Utilities

canonicalizeJSON :: Value -> Value Source #

Recursively sort all object keys alphabetically in a JSON Value

This function ensures that equivalent data structures produce byte-for-byte identical JSON strings, enabling reliable automated comparison.

Special handling: Pattern objects always have "subject" before "elements" to maintain semantic ordering.

Since: 0.1.0

Orphan instances