hydra-kernel-0.18.1: src/main/haskell/Hydra/Core/Json/Writer.hs
-- Note: this is an automatically generated file. Do not edit.
-- | JSON serialization functions using the Hydra AST
module Hydra.Core.Json.Writer where
import qualified Hydra.Core.Ast as Ast
import qualified Hydra.Core.Json.Model as Model
import qualified Hydra.Core.Overlay.Haskell.Lib.Equality as Equality
import qualified Hydra.Core.Overlay.Haskell.Lib.Lists as Lists
import qualified Hydra.Core.Overlay.Haskell.Lib.Literals as Literals
import qualified Hydra.Core.Overlay.Haskell.Lib.Logic as Logic
import qualified Hydra.Core.Overlay.Haskell.Lib.Math as Math
import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Core.Overlay.Haskell.Lib.Ordering as Ordering
import qualified Hydra.Core.Overlay.Haskell.Lib.Pairs as Pairs
import qualified Hydra.Core.Overlay.Haskell.Lib.Strings as Strings
import qualified Hydra.Core.Serialization as Serialization
import Prelude hiding (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void
-- | The colon operator used to separate keys and values in JSON objects
colonOp :: Ast.Op
colonOp =
Ast.Op {
Ast.opSymbol = (Ast.Symbol ":"),
Ast.opPadding = Ast.Padding {
Ast.paddingLeft = Ast.WsNone,
Ast.paddingRight = Ast.WsSpace},
Ast.opPrecedence = (Ast.Precedence 0),
Ast.opAssociativity = Ast.AssociativityNone}
-- | Like colonOp, but with no padding on either side, for compact-mode output
compactColonOp :: Ast.Op
compactColonOp =
Ast.Op {
Ast.opSymbol = (Ast.Symbol ":"),
Ast.opPadding = Ast.Padding {
Ast.paddingLeft = Ast.WsNone,
Ast.paddingRight = Ast.WsNone},
Ast.opPrecedence = (Ast.Precedence 0),
Ast.opAssociativity = Ast.AssociativityNone}
-- | A comma separator with no padding on either side, for compact-mode output
compactCommaOp :: Ast.Op
compactCommaOp =
Ast.Op {
Ast.opSymbol = (Ast.Symbol ","),
Ast.opPadding = Ast.Padding {
Ast.paddingLeft = Ast.WsNone,
Ast.paddingRight = Ast.WsNone},
Ast.opPrecedence = (Ast.Precedence 0),
Ast.opAssociativity = Ast.AssociativityNone}
-- | Comma-separate the elements inside the given bracket pair with no insignificant whitespace at all.
compactList :: Ast.Brackets -> [Ast.Expr] -> Ast.Expr
compactList br els =
Logic.ifElse (Lists.isEmpty els) (Serialization.brackets br Serialization.inlineStyle (Serialization.cst "")) (Serialization.brackets br Serialization.inlineStyle (Serialization.sep compactCommaOp els))
-- | The default layout options, reproducing the historical, non-configurable behavior of printJson byte-for-byte: pretty-printed, two-space indent, 120-column adaptive width, no indent-depth limit.
defaultJsonWriterOptions :: Model.JsonWriterOptions
defaultJsonWriterOptions =
Model.JsonWriterOptions {
Model.jsonWriterOptionsCompact = False,
Model.jsonWriterOptionsIndent = " ",
Model.jsonWriterOptionsMaxLineWidth = 120,
Model.jsonWriterOptionsMaxIndentDepth = Nothing}
-- | A half-block style (newline before content, none after) using the options' indent string.
halfBlockStyleWith :: Model.JsonWriterOptions -> Ast.BlockStyle
halfBlockStyleWith options =
Ast.BlockStyle {
Ast.blockStyleIndent = (Just (Model.jsonWriterOptionsIndent options)),
Ast.blockStyleNewlineBeforeContent = True,
Ast.blockStyleNewlineAfterContent = False}
-- | Encode a byte (0..255) as a two-character lowercase hex string. Non-byte inputs yield placeholder '?' characters.
hexByte :: Int -> String
hexByte c =
let nibble =
\i -> Optionals.withDefault "?" (Optionals.map (\ch -> Strings.fromList (Lists.singleton ch)) (Strings.charAt i "0123456789abcdef"))
hi = nibble (Optionals.withDefault 0 (Math.div c 16))
lo = nibble (Optionals.withDefault 0 (Math.mod c 16))
in (Strings.concat2 hi lo)
-- | Escape and quote a string for JSON output
jsonString :: String -> String
jsonString s =
let hexEscape = \c -> Strings.concat2 "\\u00" (hexByte c)
escape =
\c -> Logic.ifElse (Equality.equal c 34) "\\\"" (Logic.ifElse (Equality.equal c 92) "\\\\" (Logic.ifElse (Equality.equal c 8) "\\b" (Logic.ifElse (Equality.equal c 12) "\\f" (Logic.ifElse (Equality.equal c 10) "\\n" (Logic.ifElse (Equality.equal c 13) "\\r" (Logic.ifElse (Equality.equal c 9) "\\t" (Logic.ifElse (Ordering.lt c 32) (hexEscape c) (Strings.fromList (Lists.singleton c)))))))))
escaped = Strings.concat (Lists.map escape (Strings.toList s))
in (Strings.concat2 (Strings.concat2 "\"" escaped) "\"")
-- | Convert a key-value pair to an AST expression, honoring the given writer options and nesting depth.
keyValueToExprWith :: Model.JsonWriterOptions -> Int -> (String, Model.Value) -> Ast.Expr
keyValueToExprWith options depth pair =
let key = Pairs.first pair
value = Pairs.second pair
colon = Logic.ifElse (Model.jsonWriterOptionsCompact options) compactColonOp colonOp
in (Serialization.ifx colon (Serialization.cst (jsonString key)) (valueToExprWith options depth value))
-- | Serialize a JSON value to a string, using the default (pretty-printed) layout options.
printJson :: Model.Value -> String
printJson value = printJsonWith defaultJsonWriterOptions value
-- | Serialize a JSON value to a string using the given layout options.
printJsonWith :: Model.JsonWriterOptions -> Model.Value -> String
printJsonWith options value = Serialization.printExpr (valueToExprWith options 0 value)
-- | Choose between an inline and a block rendering of the same logical collection, given the (non-compact) writer options and the current nesting depth. Depths at or beyond maxIndentDepth always render inline; otherwise the width-adaptive choice from chooseLayout applies, using the options' own line width in place of the fixed maxLineWidth constant.
styleForDepth :: Model.JsonWriterOptions -> Int -> Ast.Expr -> Ast.Expr -> Ast.Expr
styleForDepth options depth inlineExpr blockExpr =
let atMaxDepth =
Optionals.match (Model.jsonWriterOptionsMaxIndentDepth options) False (\maxDepth -> Ordering.gte depth maxDepth)
in (Logic.ifElse atMaxDepth inlineExpr (Serialization.chooseLayout (Model.jsonWriterOptionsMaxLineWidth options) inlineExpr blockExpr))
-- | Convert a JSON value to an AST expression for serialization, using the default layout options.
valueToExpr :: Model.Value -> Ast.Expr
valueToExpr value = valueToExprWith defaultJsonWriterOptions 0 value
-- | Convert a JSON value to an AST expression for serialization, honoring the given writer options and nesting depth.
valueToExprWith :: Model.JsonWriterOptions -> Int -> Model.Value -> Ast.Expr
valueToExprWith options depth value =
let childDepth = Math.add depth 1
compact = Model.jsonWriterOptionsCompact options
style = Logic.ifElse compact Serialization.inlineStyle (halfBlockStyleWith options)
in case value of
Model.ValueArray v0 ->
let els = Lists.map (valueToExprWith options childDepth) v0
in (Logic.ifElse compact (compactList Serialization.squareBrackets els) (styleForDepth options depth (Serialization.bracketList Serialization.inlineStyle els) (Serialization.bracketList style els)))
Model.ValueBoolean v0 -> Serialization.cst (Logic.ifElse v0 "true" "false")
Model.ValueNull -> Serialization.cst "null"
Model.ValueNumber v0 ->
let rounded = Literals.decimalToBigint v0
shown = Literals.printDecimal v0
notExponential = Ordering.lte (Lists.length (Strings.splitOn "e" shown)) 1
fracParts = Strings.splitOn "." shown
frac = Optionals.withDefault "" (Lists.at 1 fracParts)
isWhole =
Logic.and notExponential (Logic.or (Ordering.lte (Lists.length fracParts) 1) (Strings.isEmpty (Strings.concat (Strings.splitOn "0" frac))))
plain = Literals.printBigint rounded
in (Serialization.cst (Logic.ifElse (Logic.and isWhole (Ordering.lte (Strings.length plain) (Strings.length shown))) plain shown))
Model.ValueObject v0 ->
let els = Lists.map (keyValueToExprWith options childDepth) v0
in (Logic.ifElse compact (compactList Serialization.curlyBraces els) (styleForDepth options depth (Serialization.curlyBracesList Nothing Serialization.inlineStyle els) (Serialization.curlyBracesList Nothing style els)))
Model.ValueString v0 -> Serialization.cst (jsonString v0)