import Data.Aeson as A
import Data.Aeson.Diff as AD
import Data.Map as M
foo :: Map Int String
foo = mempty
bar :: Map Int String
bar = M.fromList [(123, "asdf")]
encoded = A.encode (AD.diff (A.toJSON foo) (A.toJSON bar))
decoded :: Patch
Right decoded = A.eitherDecode encoded
applied = AD.patch decoded (A.toJSON foo)
In this example, we're computing a diff between two Map Int String values. Note that these still produce string keys when encoded:
λ> putStrLn $ decodeUtf8 $ A.encode bar
{"123":"asdf"}
When we compute the diff and encode it, we get the following:
From briefly looking over RFC6902, it seems like the right thing to do is actually not to make a distinction between OKey and AKey? It seems like whether a given path component is an "object index" or "array index" should instead depend on which type of JSON object you find during patching. (I.e. if you encounter an array, then try to parse the value as a numeric index, otherwise treat it as a string key for an object.) What do you think?
Consider the following:
In this example, we're computing a diff between two
Map Int String
values. Note that these still produce string keys when encoded:When we compute the diff and encode it, we get the following:
This "path" seems to be ambiguous, because when we try to decode it:
The
pointerPath
is read in as anAKey
rather than the expectedOKey
. As a result, it fails to apply:From briefly looking over RFC6902, it seems like the right thing to do is actually not to make a distinction between
OKey
andAKey
? It seems like whether a given path component is an "object index" or "array index" should instead depend on which type of JSON object you find during patching. (I.e. if you encounter an array, then try to parse the value as a numeric index, otherwise treat it as a string key for an object.) What do you think?