Packages

  • package root
    Definition Classes
    root
  • package little
    Definition Classes
    root
  • package json
    Definition Classes
    little
  • package rpc

    Defines API for JSON-RPC 2.0.

    Defines API for JSON-RPC 2.0.

    import javax.json.JsonValue
    
    import little.json.{ Json, JsonAdapter }
    import little.json.Implicits._
    import little.json.rpc._
    
    case class Params(values: Int*)
    
    // Define adapter for converting params to and from JSON
    implicit object ParamsAdapter extends JsonAdapter[Params] {
      def reading(json: JsonValue): Params =
        Params(json.as[Seq[Int]] : _*)
    
      def writing(params: Params): JsonValue =
        Json.toJson(params.values)
    }
    
    // Create request with builder
    val request = JsonRpcRequest.builder()
      .version("2.0")
      .id("590d24ae-500a-486c-8d73-8035e78529bd")
      .method("sum")
      .params(Params(1, 2, 3))
      .build()
    
    // Initialize response builder
    val responseBuilder = JsonRpcResponse.builder()
      .version(request.version)
      .id(request.id)
    
    request.method match {
      case "sum" =>
        val params = request.params.get.as[Params]
    
        // Set result
        responseBuilder.result(params.values.sum)
      case name =>
        // Or set error
        responseBuilder.error(MethodNotFound(name))
    }
    
    // Create response with builder
    val response = responseBuilder.build()
  • ArrayBuilderCompanion
  • ArrayContextWriter
  • BuilderCompanion
  • ContextWriter
  • Implicits
  • Json
  • JsonAdapter
  • JsonInput
  • JsonOutput
  • ObjectBuilderCompanion
  • ObjectContextWriter

package json

Ordering
  1. Alphabetic
Visibility
  1. Public
  2. Protected

Package Members

  1. package rpc

    Defines API for JSON-RPC 2.0.

    Defines API for JSON-RPC 2.0.

    import javax.json.JsonValue
    
    import little.json.{ Json, JsonAdapter }
    import little.json.Implicits._
    import little.json.rpc._
    
    case class Params(values: Int*)
    
    // Define adapter for converting params to and from JSON
    implicit object ParamsAdapter extends JsonAdapter[Params] {
      def reading(json: JsonValue): Params =
        Params(json.as[Seq[Int]] : _*)
    
      def writing(params: Params): JsonValue =
        Json.toJson(params.values)
    }
    
    // Create request with builder
    val request = JsonRpcRequest.builder()
      .version("2.0")
      .id("590d24ae-500a-486c-8d73-8035e78529bd")
      .method("sum")
      .params(Params(1, 2, 3))
      .build()
    
    // Initialize response builder
    val responseBuilder = JsonRpcResponse.builder()
      .version(request.version)
      .id(request.id)
    
    request.method match {
      case "sum" =>
        val params = request.params.get.as[Params]
    
        // Set result
        responseBuilder.result(params.values.sum)
      case name =>
        // Or set error
        responseBuilder.error(MethodNotFound(name))
    }
    
    // Create response with builder
    val response = responseBuilder.build()

Type Members

  1. trait ArrayBuilderCompanion[T] extends AnyRef

    Adds value to JsonArrayBuilder.

    Adds value to JsonArrayBuilder.

    See also

    ObjectBuilderCompanion

  2. trait ArrayContextWriter[T] extends AnyRef

    Writes value in array context to JsonGenerator.

    Writes value in array context to JsonGenerator.

    See also

    ObjectContextWriter

  3. trait BuilderCompanion[T] extends ArrayBuilderCompanion[T] with ObjectBuilderCompanion[T]

    Adds value to builder.

  4. trait ContextWriter[T] extends ArrayContextWriter[T] with ObjectContextWriter[T]

    Writes value to JsonGenerator.

  5. trait JsonAdapter[T] extends JsonInput[T] with JsonOutput[T]

    Consolidates JsonInput and JsonOutput.

    Consolidates JsonInput and JsonOutput.

    import javax.json.{ JsonObject, JsonValue }
    import little.json.{ Json, JsonAdapter }
    import little.json.Implicits._
    
    case class User(id: Int, name: String)
    
    implicit object UserAdapter extends JsonAdapter[User] {
      // Define how to read User from JsonValue
      def reading(json: JsonValue): User =
        json.asInstanceOf[JsonObject] match {
          case obj => User(obj.getInt("id"), obj.getString("name"))
        }
    
      // Define how to write User to JsonValue
      def writing(user: User): JsonValue =
        Json.obj("id" -> user.id, "name" -> user.name)
    }
    
    // Write User to JsonValue
    val json = Json.toJson(User(0, "root"))
    
    // Read User from JsonValue
    val user = json.as[User]
  6. trait JsonInput[T] extends AnyRef

    Reads value of type T from JsonValue.

    Reads value of type T from JsonValue.

    import javax.json.JsonObject
    import little.json.{ Json, JsonInput }
    import little.json.Implicits.JsonValueType
    
    case class User(id: Int, name: String)
    
    // Define how to read User from JsonValue
    implicit val userInput: JsonInput[User] = {
      case json: JsonObject => User(json.getInt("id"), json.getString("name"))
      case json => throw new IllegalArgumentException("JsonObject required")
    }
    
    // Parse String to JsonValue
    val json = Json.parse("""{ "id": 0, "name": "root" }""")
    
    // Read User from JsonValue
    val user = json.as[User]
    See also

    JsonOutput, JsonAdapter

  7. trait JsonOutput[T] extends BuilderCompanion[T] with ContextWriter[T]

    Writes value of type T to JsonValue.

    Writes value of type T to JsonValue.

    import little.json.{ Json, JsonOutput }
    import little.json.Implicits._
    
    case class User(id: Int, name: String)
    
    // Define how to write User to JsonValue
    implicit val userOutput: JsonOutput[User] = { user =>
      Json.obj("id" -> user.id, "name" -> user.name)
    }
    
    // Write User to JsonValue
    val json = Json.toJson(User(0, "root"))
    See also

    JsonInput, JsonAdapter

  8. trait ObjectBuilderCompanion[T] extends AnyRef

    Adds value to JsonObjectBuilder.

    Adds value to JsonObjectBuilder.

    See also

    ArrayBuilderCompanion

  9. trait ObjectContextWriter[T] extends AnyRef

    Writes value in object context to JsonGenerator.

    Writes value in object context to JsonGenerator.

    See also

    ArrayContextWriter

Value Members

  1. object Implicits

    Provides type classes for javax.json and implicit implementations of JsonOutput and JsonInput.

  2. object Json

    Provides factory methods and other utilities.

    Provides factory methods and other utilities.

    import javax.json.JsonObject
    import little.json.{ Json, JsonInput, JsonOutput }
    import little.json.Implicits._
    
    case class User(id: Int, name: String)
    
    // Define how to read User from JsonValue
    implicit val userJsonInput: JsonInput[User] = {
      case json: JsonObject => User(json.getInt("id"), json.getString("name"))
      case json => throw new IllegalArgumentException("JsonObject required")
    }
    
    // Define how to write User to JsonValue
    implicit val userJsonOutput: JsonOutput[User] = { user =>
      Json.obj("id" -> user.id, "name" -> user.name)
    }
    
    // Parse String to JsonValue
    val json = Json.parse("""{ "id": 0, "name": "root" }""")
    
    // Read User from JsonValue
    val user = json.as[User]
    
    // Write User to JsonValue
    val jsonToo = Json.toJson(user)

Ungrouped