Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ebcc2475a2
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 261 lines (199 sloc) 7.237 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
-----------------------------------------------------------------------------
-- |
-- Module : Json
-- Copyright : (c) 2011 Leon P Smith
-- License : BSD3
--
-- Maintainer : Leon P Smith <leon@melding-monads.com>
--
-- Data structure agnostic JSON serialization
--
-----------------------------------------------------------------------------

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Json
     ( Key (..)
     , Value(..)
     , Object
     , row
     , Array
     , element
     , Escaped(..)
     ) where

import Blaze.ByteString.Builder as Blaze
                   ( Write
                   , Builder
                   , copyByteString
                   , fromByteString
                   , fromLazyByteString
                   , writeByteString
                   , fromWrite
                   , fromWriteList
                   , writeWord8 )
import Blaze.ByteString.Builder.Char.Utf8
                   ( fromChar, writeChar, fromText, fromLazyText )
import Blaze.Text (float, double, integral)

import Data.Bits ( Bits((.&.), shiftR) )
import qualified Data.Map as Map
import Data.Monoid ( Monoid (mempty, mappend, mconcat) )
import Data.Int ( Int8, Int16, Int32, Int64)
import Data.Word ( Word, Word8, Word16, Word32, Word64 )

import qualified Data.Char as Char

import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.UTF8 as BU
import qualified Data.ByteString.Lazy.UTF8 as BLU
import Data.ByteString.Internal ( c2w )

import qualified Data.Text as T
import qualified Data.Text.Lazy as TL

---- The "core" of json-builder

class Value a => Key a where
  escape :: a -> Escaped

class Value a where
  toBuilder :: a -> Blaze.Builder

newtype Escaped = Escaped Blaze.Builder deriving (Monoid)

instance Key Escaped where
  escape = id

instance Value Escaped where
  toBuilder (Escaped str) = fromChar '"' `mappend` str `mappend` fromChar '"'

type CommaTracker = (Bool -> Blaze.Builder) -> Bool -> Blaze.Builder

comma b f True = b `mappend` f False
comma b f False = fromChar ',' `mappend` b `mappend` f False
{-# INLINE comma #-}

newtype Object = Object CommaTracker

instance Value Object where
  toBuilder (Object f) = fromChar '{' `mappend` f (\_ -> fromChar '}') True

instance Monoid Object where
  mempty = Object id
  mappend (Object f) (Object g) = Object (f . g)

row :: (Key k, Value a) => k -> a -> Object
row k a = Object syntax
  where
    syntax = comma (mconcat [ toBuilder k, fromChar ':', toBuilder a ])


newtype Array = Array CommaTracker

instance Value Array where
  toBuilder (Array f) = fromChar '[' `mappend` f (\_ -> fromChar ']') True

instance Monoid Array where
  mempty = Array id
  mappend (Array f) (Array g) = Array (f . g)

element :: Value a => a -> Array
element a = Array $ comma (toBuilder a)

-- Primitive instances for json-builder

instance Value () where
  toBuilder _ = copyByteString "null"

instance Value Int where
  toBuilder = integral

instance Value Int8 where
  toBuilder = integral

instance Value Int16 where
  toBuilder = integral

instance Value Int32 where
  toBuilder = integral

instance Value Int64 where
  toBuilder = integral

instance Value Integer where
  toBuilder = integral

instance Value Word where
  toBuilder = integral

instance Value Word8 where
  toBuilder = integral

instance Value Word16 where
  toBuilder = integral

instance Value Word32 where
  toBuilder = integral

instance Value Word64 where
  toBuilder = integral

instance Value Double where
  toBuilder = double

instance Value Float where
  toBuilder = float

instance Value Bool where
  toBuilder True = copyByteString "true"
  toBuilder False = copyByteString "false"

instance Key BS.ByteString where
  escape x = Escaped (loop (splitQ x))
    where
      splitQ = BU.break quoteNeeded

      loop (a,b)
        = fromByteString a `mappend`
            case BU.decode b of
              Nothing -> mempty
              Just (c,n) -> fromWrite (quoteChar c) `mappend`
                                loop (splitQ (BS.drop n b))

instance Value BS.ByteString where
  toBuilder = toBuilder . escape

instance Key BL.ByteString where
  escape x = Escaped (loop (splitQ x))
    where
      splitQ = BLU.break quoteNeeded

      loop (a,b)
        = fromLazyByteString a `mappend`
            case BLU.decode b of
              Nothing -> mempty
              Just (c,n) -> fromWrite (quoteChar c) `mappend`
                                loop (splitQ (BL.drop n b))

instance Value BL.ByteString where
  toBuilder = toBuilder . escape

instance Key T.Text where
  escape x = Escaped (loop (splitQ x))
    where
      splitQ = T.break quoteNeeded

      loop (a,b)
        = fromText a `mappend`
            case T.uncons b of
              Nothing -> mempty
              Just (c,b') -> fromWrite (quoteChar c) `mappend`
                                 loop (splitQ b')

instance Value T.Text where
  toBuilder = toBuilder . escape

instance Key TL.Text where
  escape x = Escaped (loop (splitQ x))
    where
      splitQ = TL.break quoteNeeded

      loop (a,b)
        = fromLazyText a `mappend`
            case TL.uncons b of
              Nothing -> mempty
              Just (c,b') -> fromWrite (quoteChar c) `mappend`
                                 loop (splitQ b')

instance Value TL.Text where
  toBuilder = toBuilder . escape

instance Key [Char] where
  escape str = Escaped (fromWriteList writeEscapedChar str)
    where
      writeEscapedChar c | quoteNeeded c = quoteChar c
                         | otherwise = writeChar c

instance Value [Char] where
  toBuilder = toBuilder . escape

instance Value a => Value [a] where
  toBuilder = toBuilder . mconcat . map element

instance (Key k, Value a) => Value (Map.Map k a) where
  toBuilder = toBuilder
            . Map.foldrWithKey (\k a b -> row k a `mappend` b) mempty

------------------------------------------------------------------------------

quoteNeeded :: Char -> Bool
quoteNeeded c = c == '\\' || c == '"' || Char.ord c < 0x20
{-# INLINE quoteNeeded #-}

quoteChar :: Char -> Write
quoteChar c = case c of
                '\\' -> writeByteString "\\\\"
                '"' -> writeByteString "\\\""
                '\b' -> writeByteString "\\b"
                '\f' -> writeByteString "\\f"
                '\n' -> writeByteString "\\n"
                '\r' -> writeByteString "\\r"
                '\t' -> writeByteString "\\t"
                _ -> hexEscape c

hexEscape :: Char -> Write
hexEscape (c2w -> c)
  = writeByteString "\\u00"
    `mappend` writeWord8 (char ((c `shiftR` 4) .&. 0xF))
    `mappend` writeWord8 (char ( c .&. 0xF))

char :: Word8 -> Word8
char i | i < 10 = i + 48
       | otherwise = i + 87
{-# INLINE char #-}
Something went wrong with that request. Please try again.