argonaut icon indicating copy to clipboard operation
argonaut copied to clipboard

Feature request: Cursor can descend into string as json

Open newhoggy opened this issue 9 years ago • 4 comments

Would be nice if there was a combinator that can descend into a JSON string as if it were JSON. For example with the JSON:

{ "field": "\"{\"subField\": 1}" }

Be able to navigate a cursor like this:

c --\! "field" --\ "subField".as[Int]

Here's some example code as proof of concept:

    implicit class ACursorOps(self: ACursor) {
      def --\!(fieldName: String): ACursor = (self --\ fieldName).parsed

      def parsed: ACursor = {
        self.as[String].result match {
          case Left((_, history)) =>
            self.history.failedACursor(self.any.cursor)
          case Right(text) =>
            Parse.parse(text) match {
              case Left(message) =>
                self.history.failedACursor(self.any.cursor)
              case Right(json) =>
                self.history.acursor(json.cursor)
            }
        }
      }
    }

    implicit class HCursorOps(self: HCursor) {
      def --\!(fieldName: String): ACursor = (self --\ fieldName).parsed
    }

newhoggy avatar Apr 05 '16 05:04 newhoggy

Hmmm, that's interesting, I'm slightly worried it's satisfying a niche case but I'll keep this here for now and wont close it.

seanparsons avatar Apr 05 '16 08:04 seanparsons

I encountered this kind of JSON when using AWS. If you have subscribe an SNS topic to an S3 bucket and then subscribe and SQS subscription to SNS, you will get JSON that looks like this.

newhoggy avatar Apr 05 '16 21:04 newhoggy

I don't doubt it's out there, it's more that each edge case feature we support is more potential weight to maintain.

seanparsons avatar Apr 06 '16 08:04 seanparsons

:+1:

tonymorris avatar Apr 07 '16 07:04 tonymorris