Skip to content

Commit 3199eb1

Browse files
authored
Escape JSON Pointer strings (#2)
* Escape JSON Pointer strings * Add and export escapeKey and unescapeKey functions
1 parent dd92324 commit 3199eb1

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/JSONPointer/Model.hs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module JSONPointer.Model
33
, run
44
, atIndexOrKey
55
, atKey
6+
, escapeKey
7+
, unescapeKey
68
)
79
where
810

@@ -26,7 +28,7 @@ instance Monoid JSONPointer where
2628

2729
instance Show JSONPointer where
2830
showsPrec _ (JSONPointer impl) =
29-
appEndo $ impl (\_ text -> Endo (showString "/" . showString (T.unpack text)))
31+
appEndo $ impl (\_ text -> Endo (showString "/" . showString (T.unpack $ escapeKey text)))
3032

3133
instance Eq JSONPointer where
3234
a == b = show a == show b
@@ -53,3 +55,15 @@ atIndexOrKey index key = JSONPointer $ \handler -> handler index key
5355
{-# INLINE atKey #-}
5456
atKey :: T.Text -> JSONPointer
5557
atKey = atIndexOrKey Nothing
58+
59+
-- |
60+
-- Escape JSON Pointer string.
61+
-- See here https://datatracker.ietf.org/doc/html/rfc6901 for more details.
62+
escapeKey :: T.Text -> T.Text
63+
escapeKey = T.replace "/" "~1" . T.replace "~" "~0"
64+
65+
-- |
66+
-- Unscape JSON Pointer string.
67+
-- See here https://datatracker.ietf.org/doc/html/rfc6901 for more details.
68+
unescapeKey :: T.Text -> T.Text
69+
unescapeKey = T.replace "~0" "~" . T.replace "~1" "/"

0 commit comments

Comments
 (0)