grapple.json
Members list
Packages
Type members
Classlikes
Provides JSON utilities.
Provides JSON utilities.
import scala.language.implicitConversions
import grapple.json.{ *, given }
// Create JSON object
val user = Json.obj("id" -> 1000, "name" -> "lupita")
// Create JSON array
val info = Json.arr(user, "/home/lupita", 8L * 1024 * 1024 * 1024)
// Parse JSON text
val root = Json.parse("""{ "id": 0, "name": "root" }""")
case class User(id: Int, name: String)
given userOutput: JsonOutput[User] with
def write(u: User) = Json.obj("id" -> u.id, "name" -> u.name)
// Convert value to JSON object
val nobody = Json.toJson(User(65534, "nobody"))
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
Json.type
Defines JSON input and output conversions.
Defines JSON input and output conversions.
Attributes
- Supertypes
Represents JSON array.
Represents JSON array.
Attributes
- See also
- Companion
- object
- Supertypes
- Known subtypes
-
class JsonStructureFacade
Defines JSON array builder.
Defines JSON array builder.
import scala.language.implicitConversions
import grapple.json.{ *, given }
val user = JsonArrayBuilder()
.add(1000)
.add("lupita")
.add(Set("lupita", "sudoer"))
.toJsonArray()
assert { user(0).as[Int] == 1000 }
assert { user(1).as[String] == "lupita" }
assert { user(2).as[Set[String]] == Set("lupita", "sudoer") }
Attributes
- See also
- Supertypes
-
class Objecttrait Matchableclass Any
Defines JSON array error.
Defines JSON array error.
Value parameters
- cause
-
underlying cause
- index
-
array index
Attributes
- Supertypes
-
trait Producttrait Equalsclass JsonExceptionclass RuntimeExceptionclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
Provides JSON boolean factory.
Provides JSON boolean factory.
Attributes
- Companion
- trait
- Supertypes
-
trait Sumtrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
JsonBoolean.type
Defines JSON exception.
Defines JSON exception.
Value parameters
- cause
-
underlying cause
- message
-
detail message
Attributes
- Constructor
-
Constructs exception with detail message and underlying cause.
- Supertypes
-
class RuntimeExceptionclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
- Known subtypes
-
class JsonRpcErrorclass InternalErrorclass InvalidParamsclass InvalidRequestclass MethodNotFoundclass ParseErrorclass JsonArrayErrorclass JsonExpectationErrorclass JsonGeneratorErrorclass JsonObjectErrorclass JsonParserErrorShow all
Defines JSON expectation error.
Defines JSON expectation error.
Value parameters
- actual
-
class
- expected
-
class
Attributes
- Supertypes
-
trait Producttrait Equalsclass JsonExceptionclass RuntimeExceptionclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
Defines JSON generator.
Defines JSON generator.
import java.io.StringWriter
import scala.language.implicitConversions
import grapple.json.{ *, given }
val buf = StringWriter()
val out = JsonGenerator(buf)
try
out.writeStartObject() // start root object
out.write("id", 1000)
out.write("name", "lupita")
out.writeStartArray("groups") // start nested array
out.write("lupita")
out.write("admin")
out.write("sudoer")
out.writeEnd() // end nested array
out.writeStartObject("info") // start nested object
out.write("home", "/home/lupita")
out.write("storage", "8 GiB")
out.writeEnd() // end nested object
out.writeEnd() // end root object
out.flush()
val json = Json.parse(buf.toString)
assert { json("id") == JsonNumber(1000) }
assert { json("name") == JsonString("lupita") }
assert { json("groups") == Json.arr("lupita", "admin", "sudoer") }
assert { json("info") == Json.obj("home" -> "/home/lupita", "storage" -> "8 GiB") }
finally
out.close()
Attributes
- See also
- Companion
- object
- Supertypes
-
trait AutoCloseableclass Objecttrait Matchableclass Any
Provides JSON generator factory.
Provides JSON generator factory.
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
JsonGenerator.type
Defines JSON generator error.
Defines JSON generator error.
Value parameters
- message
-
detail message
Attributes
- Supertypes
-
trait Producttrait Equalsclass JsonExceptionclass RuntimeExceptionclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
Defines JSON input conversion.
Defines JSON input conversion.
import scala.language.implicitConversions
import grapple.json.{ *, given }
case class User(id: Int, name: String)
// Define how to convert JsonValue to User
given userInput: JsonInput[User] =
json => User(json("id"), json("name"))
val json = Json.obj("id" -> 0, "name" -> "root")
assert { json.as[User] == User(0, "root") }
Attributes
- See also
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
trait JsonAdapter[T]
Provides JSON number factory.
Provides JSON number factory.
Attributes
- Companion
- trait
- Supertypes
-
trait Sumtrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
JsonNumber.type
Represents JSON object.
Represents JSON object.
Attributes
- See also
- Companion
- object
- Supertypes
- Known subtypes
-
class JsonStructureFacade
Provides JSON object factory.
Provides JSON object factory.
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
JsonObject.type
Defines JSON object builder.
Defines JSON object builder.
import scala.language.implicitConversions
import grapple.json.{ *, given }
val user = JsonObjectBuilder()
.add("id", 1000)
.add("name", "lupita")
.add("groups", Set("lupita", "sudoer"))
.toJsonObject()
assert { user("id").as[Int] == 1000 }
assert { user("name").as[String] == "lupita" }
assert { user("groups").as[Set[String]] == Set("lupita", "sudoer") }
Attributes
- See also
- Supertypes
-
class Objecttrait Matchableclass Any
Defines JSON object error.
Defines JSON object error.
Value parameters
- cause
-
underlying cause
- key
-
object key
Attributes
- Supertypes
-
trait Producttrait Equalsclass JsonExceptionclass RuntimeExceptionclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
Defines JSON output conversion.
Defines JSON output conversion.
import scala.language.implicitConversions
import grapple.json.{ *, given }
case class User(id: Int, name: String)
// Define how to convert User to JsonValue
given userOutput: JsonOutput[User] =
user => Json.obj("id" -> user.id, "name" -> user.name)
val users = Json.arr(User(0, "root"), User(1000, "lupita"))
assert { users(0) == Json.obj("id" -> 0, "name" -> "root") }
assert { users(1) == Json.obj("id" -> 1000, "name" -> "lupita") }
Attributes
- See also
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
trait JsonAdapter[T]
Defines JSON parser.
Defines JSON parser.
import scala.language.implicitConversions
import grapple.json.{ *, given }
import grapple.json.JsonParser.Event
val parser = JsonParser("""{ "id": 1000, "name": "lupita", "groups": ["lupita", "admin"] }""")
try
// Get first event (start root object)
assert { parser.next() == Event.StartObject }
// Get key and value
assert { parser.next() == Event.Key("id") }
assert { parser.next() == Event.Value(1000) }
// Get key and value
assert { parser.next() == Event.Key("name") }
assert { parser.next() == Event.Value("lupita") }
// Get key and value
assert { parser.next() == Event.Key("groups") }
assert { parser.next() == Event.StartArray } // start nested array
assert { parser.next() == Event.Value("lupita") }
assert { parser.next() == Event.Value("admin") }
assert { parser.next() == Event.EndArray } // end nested array
// Get final event (end root object)
assert { parser.next() == Event.EndObject }
// No more events
assert { !parser.hasNext }
finally
parser.close()
Attributes
- See also
- Companion
- object
- Supertypes
Provides JSON parser factory and other utilities.
Provides JSON parser factory and other utilities.
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
JsonParser.type
Defines JSON parser error.
Defines JSON parser error.
Value parameters
- message
-
detail message
- offset
-
character offset of parser error
Attributes
- Supertypes
-
trait Producttrait Equalsclass JsonExceptionclass RuntimeExceptionclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
Defines JSON reader.
Defines JSON reader.
import scala.language.implicitConversions
import grapple.json.{ *, given }
val in = JsonReader("""{ "id": 1000, "name": "lupita" }""")
try
val user = in.read()
assert { user("id").as[Int] == 1000 }
assert { user("name").as[String] == "lupita" }
finally
in.close()
Attributes
- See also
- Companion
- object
- Supertypes
-
trait AutoCloseableclass Objecttrait Matchableclass Any
Provides JSON reader factory.
Provides JSON reader factory.
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
JsonReader.type
Provides JSON string factory.
Provides JSON string factory.
Attributes
- Companion
- trait
- Supertypes
-
trait Sumtrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
JsonString.type
Represents JSON structure.
Represents JSON structure.
Attributes
- Supertypes
- Known subtypes
Assumes either JSON object or JSON array.
Assumes either JSON object or JSON array.
Attributes
- See also
- Note
-
A structure facade is created by conversion only.
- Supertypes
-
trait JsonArraytrait JsonObjecttrait JsonStructuretrait JsonValueclass Objecttrait Matchableclass AnyShow all
Represents JSON value.
Represents JSON value.
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
trait JsonBooleanobject Falseobject Trueobject JsonNulltrait JsonNumbertrait JsonStringtrait JsonStructuretrait JsonArrayclass JsonStructureFacadetrait JsonObjectShow all
Defines JSON writer.
Defines JSON writer.
import java.io.StringWriter
import scala.language.implicitConversions
import grapple.json.{ *, given }
val buf = StringWriter()
val out = JsonWriter(buf)
try
val user = Json.obj("id" -> 1000, "name" -> "lupita")
out.write(user)
val json = Json.parse(buf.toString)
assert { json == user }
finally
out.close()
Attributes
- See also
- Companion
- object
- Supertypes
-
trait AutoCloseableclass Objecttrait Matchableclass Any
Provides JSON writer factory.
Provides JSON writer factory.
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
JsonWriter.type
Givens
Givens
Converts JsonValue
to Array
.
Converts JsonValue
to Array
.
Attributes
Converts Array
to JsonArray
.
Converts Array
to JsonArray
.
Attributes
Converts JsonValue
to BigDecimal
.
Converts JsonValue
to BigDecimal
.
Attributes
Converts BigDecimal
to JsonNumber
.
Converts BigDecimal
to JsonNumber
.
Attributes
Converts JsonValue
to BigInt
.
Converts JsonValue
to BigInt
.
Attributes
Converts BigInt
to JsonNumber
.
Converts BigInt
to JsonNumber
.
Attributes
Converts JsonValue
to Boolean
.
Converts JsonValue
to Boolean
.
Attributes
Converts Boolean
to JsonBoolean
.
Converts Boolean
to JsonBoolean
.
Attributes
Converts JsonValue
to Double
.
Converts JsonValue
to Double
.
Attributes
Converts Double
to JsonNumber
.
Converts Double
to JsonNumber
.
Attributes
Converts JsonValue
to Either
.
Converts JsonValue
to Either
.
Attributes
Converts Either
to JsonValue
.
Converts Either
to JsonValue
.
Attributes
Converts Failure
to JsonNull
.
Converts Failure
to JsonNull
.
Attributes
Converts JsonValue
to Float
.
Converts JsonValue
to Float
.
Attributes
Converts Float
to JsonNumber
.
Converts Float
to JsonNumber
.
Attributes
Converts JsonValue
to Int
.
Converts JsonValue
to Int
.
Attributes
Converts Int
to JsonNumber
.
Converts Int
to JsonNumber
.
Attributes
Converts JsonValue
to Iterable
.
Converts JsonValue
to Iterable
.
Attributes
Converts Iterable
to JsonArray
.
Converts Iterable
to JsonArray
.
Attributes
Casts JsonValue
to JsonArray
.
Casts JsonValue
to JsonArray
.
Attributes
Casts JsonValue
to JsonBoolean
.
Casts JsonValue
to JsonBoolean
.
Attributes
Converts (String, T)
to (String, JsonValue)
.
Converts (String, T)
to (String, JsonValue)
.
Attributes
Applies conversion using JsonInput
.
Applies conversion using JsonInput
.
Attributes
Casts JsonValue
to JsonNull
.
Casts JsonValue
to JsonNull
.
Attributes
Casts JsonValue
to JsonNumber
.
Casts JsonValue
to JsonNumber
.
Attributes
Casts JsonValue
to JsonObject
.
Casts JsonValue
to JsonObject
.
Attributes
Applies conversion using JsonOutput
.
Applies conversion using JsonOutput
.
Attributes
Casts JsonValue
to JsonString
.
Casts JsonValue
to JsonString
.
Attributes
Converts JsonValue
to JsonStructureFacade
.
Converts JsonValue
to JsonStructureFacade
.
Attributes
Returns JsonValue
as is.
Returns JsonValue
as is.
This instance is required to perform actions such as the following:
import scala.language.implicitConversions
import grapple.json.{ Json, JsonValue, given }
val json = Json.obj("values" -> Json.arr("abc", 123, true))
// Requires jsonValueJsonInput
val list = json("values").as[List[JsonValue]]
Attributes
Converts Left
to JsonValue
.
Converts Left
to JsonValue
.
Attributes
Converts JsonValue
to Long
.
Converts JsonValue
to Long
.
Attributes
Converts Long
to JsonNumber
.
Converts Long
to JsonNumber
.
Attributes
Converts JsonValue
to Map
.
Converts JsonValue
to Map
.
Attributes
Converts Map
to JsonObject
.
Converts Map
to JsonObject
.
Attributes
Converts None
to JsonNull
.
Converts None
to JsonNull
.
Attributes
Converts Some
to JsonValue
or returns JsonNull
if None
.
Converts Some
to JsonValue
or returns JsonNull
if None
.
Attributes
Converts Right
to JsonValue
.
Converts Right
to JsonValue
.
Attributes
Converts JsonValue
to String
.
Converts JsonValue
to String
.
Attributes
Converts String
to JsonString
.
Converts String
to JsonString
.
Attributes
Converts JsonValue
to Try
.
Converts JsonValue
to Try
.
Attributes
Converts Success
to JsonValue
or returns JsonNull
if Failure
.
Converts Success
to JsonValue
or returns JsonNull
if Failure
.
Attributes
Extensions
Extensions
Gets value in JSON object.
Gets value in JSON object.
Value parameters
- key
-
object key
Attributes
- Throws
-
JsonExpectationError
if not JsonObject
JsonObjectErrorif object key does not exist
Gets value in JSON array.
Gets value in JSON array.
Value parameters
- index
-
array index
Attributes
- Throws
-
JsonArrayError
if array index is out of bounds
JsonExpectationErrorif not JsonArray
Collects values with given object key while traversing nested objects and arrays.
Collects values with given object key while traversing nested objects and arrays.
import grapple.json.{ Json, \\, given }
val json = Json.parse("""{
"node": {
"name": "localhost",
"users": [
{ "id": 0, "name": "root" },
{ "id": 1000, "name": "lupita" }
]
}
}""")
val names = (json \\ "name").map(_.as[String])
assert { names == Seq("localhost", "root", "lupita") }
Value parameters
- key
-
object key