|
| 1 | +module Data.Json.Extended |
| 2 | + ( module Sig |
| 3 | + |
| 4 | + , EJson(..) |
| 5 | + , getEJson |
| 6 | + , roll |
| 7 | + , unroll |
| 8 | + |
| 9 | + , null |
| 10 | + , boolean |
| 11 | + , integer |
| 12 | + , decimal |
| 13 | + , string |
| 14 | + , timestamp |
| 15 | + , date |
| 16 | + , time |
| 17 | + , interval |
| 18 | + , objectId |
| 19 | + , orderedSet |
| 20 | + , object |
| 21 | + , object' |
| 22 | + , array |
| 23 | + |
| 24 | + , renderEJson |
| 25 | + |
| 26 | + , arbitraryEJsonOfSize |
| 27 | + , arbitraryJsonEncodableEJsonOfSize |
| 28 | + ) where |
| 29 | + |
| 30 | +import Prelude |
| 31 | + |
| 32 | +import Data.Eq1 (eq1) |
| 33 | +import Data.Ord1 (compare1) |
| 34 | +import Data.Argonaut.Encode (class EncodeJson, encodeJson) |
| 35 | +import Data.Argonaut.Decode (class DecodeJson, decodeJson) |
| 36 | + |
| 37 | +import Data.Functor.Mu as Mu |
| 38 | +import Data.HugeNum as HN |
| 39 | +import Data.List as L |
| 40 | +import Data.Map as Map |
| 41 | +import Data.Maybe as M |
| 42 | +import Data.StrMap as SM |
| 43 | +import Data.Tuple as T |
| 44 | + |
| 45 | +import Test.StrongCheck as SC |
| 46 | +import Test.StrongCheck.Gen as Gen |
| 47 | + |
| 48 | +import Data.Json.Extended.Signature as Sig |
| 49 | + |
| 50 | +newtype EJson = EJson (Mu.Mu Sig.EJsonF) |
| 51 | + |
| 52 | +getEJson |
| 53 | + ∷ EJson |
| 54 | + → Mu.Mu Sig.EJsonF |
| 55 | +getEJson (EJson x) = |
| 56 | + x |
| 57 | + |
| 58 | +roll |
| 59 | + ∷ Sig.EJsonF EJson |
| 60 | + → EJson |
| 61 | +roll = |
| 62 | + EJson |
| 63 | + <<< Mu.roll |
| 64 | + <<< map getEJson |
| 65 | + |
| 66 | +unroll |
| 67 | + ∷ EJson |
| 68 | + → Sig.EJsonF EJson |
| 69 | +unroll = |
| 70 | + getEJson |
| 71 | + >>> Mu.unroll |
| 72 | + >>> map EJson |
| 73 | + |
| 74 | +instance eqEJson ∷ Eq EJson where |
| 75 | + eq (EJson a) (EJson b) = |
| 76 | + eq1 (Mu.unroll a) (Mu.unroll b) |
| 77 | + |
| 78 | +instance ordEJson ∷ Ord EJson where |
| 79 | + compare (EJson a) (EJson b) = |
| 80 | + compare1 (Mu.unroll a) (Mu.unroll b) |
| 81 | + |
| 82 | +instance showEJson ∷ Show EJson where |
| 83 | + show = renderEJson |
| 84 | + |
| 85 | +instance decodeJsonEJson ∷ DecodeJson EJson where |
| 86 | + decodeJson json = |
| 87 | + roll <$> |
| 88 | + Sig.decodeJsonEJsonF |
| 89 | + decodeJson |
| 90 | + (Sig.String >>> roll) |
| 91 | + json |
| 92 | + |
| 93 | +-- | This is a _lossy_ encoding of EJSON to JSON; JSON only supports objects with strings |
| 94 | +-- as keys. |
| 95 | +instance encodeJsonEJson ∷ EncodeJson EJson where |
| 96 | + encodeJson (EJson x) = |
| 97 | + Sig.encodeJsonEJsonF |
| 98 | + encodeJson |
| 99 | + asKey |
| 100 | + (EJson <$> Mu.unroll x) |
| 101 | + |
| 102 | + where |
| 103 | + asKey |
| 104 | + ∷ EJson |
| 105 | + → M.Maybe String |
| 106 | + asKey (EJson x) = |
| 107 | + case Mu.unroll x of |
| 108 | + Sig.String k → pure k |
| 109 | + _ → M.Nothing |
| 110 | + |
| 111 | + |
| 112 | +arbitraryEJsonOfSize |
| 113 | + ∷ Gen.Size |
| 114 | + → Gen.Gen EJson |
| 115 | +arbitraryEJsonOfSize size = |
| 116 | + roll <$> |
| 117 | + case size of |
| 118 | + 0 → Sig.arbitraryBaseEJsonF |
| 119 | + n → Sig.arbitraryEJsonF $ arbitraryEJsonOfSize (n - 1) |
| 120 | + |
| 121 | +-- | Generate only JSON-encodable objects |
| 122 | +arbitraryJsonEncodableEJsonOfSize |
| 123 | + ∷ Gen.Size |
| 124 | + → Gen.Gen EJson |
| 125 | +arbitraryJsonEncodableEJsonOfSize size = |
| 126 | + roll <$> |
| 127 | + case size of |
| 128 | + 0 → Sig.arbitraryBaseEJsonF |
| 129 | + n → Sig.arbitraryEJsonFWithKeyGen keyGen $ arbitraryJsonEncodableEJsonOfSize (n - 1) |
| 130 | + where |
| 131 | + keyGen = |
| 132 | + roll <<< Sig.String <$> |
| 133 | + SC.arbitrary |
| 134 | + |
| 135 | +renderEJson |
| 136 | + ∷ EJson |
| 137 | + → String |
| 138 | +renderEJson (EJson x) = |
| 139 | + Sig.renderEJsonF |
| 140 | + renderEJson |
| 141 | + (EJson <$> Mu.unroll x) |
| 142 | + |
| 143 | +null ∷ EJson |
| 144 | +null = roll Sig.Null |
| 145 | + |
| 146 | +boolean ∷ Boolean → EJson |
| 147 | +boolean = roll <<< Sig.Boolean |
| 148 | + |
| 149 | +integer ∷ Int → EJson |
| 150 | +integer = roll <<< Sig.Integer |
| 151 | + |
| 152 | +decimal ∷ HN.HugeNum → EJson |
| 153 | +decimal = roll <<< Sig.Decimal |
| 154 | + |
| 155 | +string ∷ String → EJson |
| 156 | +string = roll <<< Sig.String |
| 157 | + |
| 158 | +timestamp ∷ String → EJson |
| 159 | +timestamp = roll <<< Sig.Timestamp |
| 160 | + |
| 161 | +date ∷ String → EJson |
| 162 | +date = roll <<< Sig.Date |
| 163 | + |
| 164 | +time ∷ String → EJson |
| 165 | +time = roll <<< Sig.Time |
| 166 | + |
| 167 | +interval ∷ String → EJson |
| 168 | +interval = roll <<< Sig.Interval |
| 169 | + |
| 170 | +objectId ∷ String → EJson |
| 171 | +objectId = roll <<< Sig.ObjectId |
| 172 | + |
| 173 | +orderedSet ∷ Array EJson → EJson |
| 174 | +orderedSet = roll <<< Sig.OrderedSet |
| 175 | + |
| 176 | +array ∷ Array EJson → EJson |
| 177 | +array = roll <<< Sig.Array |
| 178 | + |
| 179 | +object ∷ Map.Map EJson EJson → EJson |
| 180 | +object = roll <<< Sig.Object <<< L.fromList <<< Map.toList |
| 181 | + |
| 182 | +object' ∷ SM.StrMap EJson → EJson |
| 183 | +object' = roll <<< Sig.Object <<< map go <<< L.fromList <<< SM.toList |
| 184 | + where |
| 185 | + go (T.Tuple a b) = T.Tuple (string a) b |
0 commit comments