module Drasil.Data.Formats.JSON.Core
  ( -- * JSON
    JSON(..),
  )
where

import Data.Text (Text)
import qualified Data.Text as T (pack)
import Data.Scientific (Scientific)
import Data.String (IsString(..))

-- | A JSON representation conformant with
-- [RFC 8259](https://datatracker.ietf.org/doc/html/rfc8259).
data JSON =
    -- | Note that empty and duplicate keys are allowed,
    -- and how they are handled is up to the implementation of the reader.
    JObject [(Text, JSON)]
  | JArray [JSON]
  | JString Text
  | JNumber Scientific
  | JBool Bool
  | JNull
  deriving (Int -> JSON -> ShowS
[JSON] -> ShowS
JSON -> String
(Int -> JSON -> ShowS)
-> (JSON -> String) -> ([JSON] -> ShowS) -> Show JSON
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSON -> ShowS
showsPrec :: Int -> JSON -> ShowS
$cshow :: JSON -> String
show :: JSON -> String
$cshowList :: [JSON] -> ShowS
showList :: [JSON] -> ShowS
Show, JSON -> JSON -> Bool
(JSON -> JSON -> Bool) -> (JSON -> JSON -> Bool) -> Eq JSON
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSON -> JSON -> Bool
== :: JSON -> JSON -> Bool
$c/= :: JSON -> JSON -> Bool
/= :: JSON -> JSON -> Bool
Eq)

-- | Using the 'OverloadedStrings' language extension, string literals
-- can be automatically converted to 'JString's.
instance IsString JSON where
  fromString :: String -> JSON
fromString = Text -> JSON
JString (Text -> JSON) -> (String -> Text) -> String -> JSON
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack