losizm / grapple

The JSON library for Scala.
https://losizm.github.io/grapple
Apache License 2.0
6 stars 0 forks source link

Cannot directly parse JSON string literal #1

Closed zgrannan closed 1 year ago

zgrannan commented 2 years ago

It appears that attempt to parse a string literal (not enclosed in an object), leads to an error:

  "Json.parse" should "work" in {
    Json.parse("\"test\"")
  }

yields

[info] - should work *** FAILED ***
[info]   grapple.json.JsonParserError: Unexpected character '"' at offset 0
[info]   at grapple.json.JsonParserError$.apply(JsonException.scala:50)
[info]   at grapple.json.JsonParserImpl.unexpectedChar(JsonParserImpl.scala:300)
[info]   at grapple.json.JsonParserImpl.firstEvent(JsonParserImpl.scala:117)
[info]   at grapple.json.JsonParserImpl.init$$anonfun$1(JsonParserImpl.scala:97)
[info]   at scala.util.Try$.apply(Try.scala:210)
[info]   at grapple.json.JsonParserImpl.init(JsonParserImpl.scala:97)
[info]   at grapple.json.JsonParserImpl.hasNext(JsonParserImpl.scala:43)
[info]   at grapple.json.JsonParserImpl.next(JsonParserImpl.scala:49)
[info]   at grapple.json.JsonReaderImpl.read(JsonReaderImpl.scala:27)
[info]   at grapple.json.Json$.parse(Json.scala:59)
losizm commented 2 years ago

Json.parse(String) is intended for JSON structures only – i.e., JsonObject and JsonArray. Although not expressly stated, see return type for Json in scaladoc.

That said, I will consider changing the implementation to parse any JSON value... and will most certainly update the documentation.

Much thanks for your input.

losizm commented 1 year ago

The scaladoc for all parse methods is updated. An annotation is applied indicating that JsonParserError is thrown if argument cannot be parsed to JSON structure. See commit 9fc45204f1e644cdbf9af00b90f7542b7ce52eeb.

No enhancement is applied to parse other JSON values.