3

Let's say I have this little Argonaut Json instance:

import argonaut._, Argonaut._

Json.obj(
  "id"     := 42,
  "viewed" := false
)

Now, I want to remove the pair whose key is viewed. I found the following to work, but it's a bit too verbose. Is there anything shorter than this:

for {
  field   <- json.cursor.downField("viewed")
  updated <- field.delete
} yield updated.undo

Also, the name of the method that "commits" the changes (undo) seems a bit misleading in my opinion? How should I interpret the name? Initially I thought there's a way to undo the last change in the history of modifications.

UPDATE

I have found one solution. Using an HCursor:

json.hcursor.downField("viewed").delete.undo

The question of why undo is named undo is still open though.

Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • 1
    As a side note, there are operator aliases for most of the methods you're using: `-(!(json.acursor --\ "viewed"))`. And about `undo`: it kind of makes sense on the model of Anti-XML's `unselect`—you're not undoing the modifications, you're undoing the navigation into the tree. – Travis Brown Nov 17 '13 at 19:18
  • @TravisBrown thanks. I know about the operator aliases, I just don't want to shock my coworkers (too much). Regarding `undo`... ok, now I can see why it might be named like that, but boy, it reads really wrong. Normally one would assume it undoes the last operation. For example, `downField("X").downField("Y").undo.undo` should go back to where I was initially. Bad naming IMHO. Thanks for your help. – Ionuț G. Stan Nov 17 '13 at 19:38

1 Answers1

0

Cursor is floating/moving reference like a pointer in C/C++. And downField is move instruction to the cursor, therefore if you don't undo cursor remains in "viewed" propety instead of parent json object. To return to parent json object, you have to undo to move cursor.

bigwheel
  • 140
  • 9