Skip to content

noppoMan/SwiftyJSONRPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftyJSONRPC

A JSON RPC Parser/Serializer For Swift

Features

  • Request Parser/Serializer
  • Response Parser/Serializer
  • Batch Request/Response

Package.swift

import PackageDescription

let package = Package(
    name: "MyApp",
    dependencies: [
        .Package(url: "https://github.com/noppoMan/SwiftyJSONRPC.git", majorVersion: 0, minor: 1),
    ]
)

Usage

Request

Single

import SwiftyJSONRPC

let json: JSON = ["jsonrpc": "2.0", "id": 1, "method": "sum", "params": [1, 1]]
let request = JSONRPCV2.Request(json: json)

print(request.isBatch) // false
print(request.items.first?.id) // 1

print(request.toJSON()) // SwiftyJSON.JSON type

Batch

We supports batch request

import SwiftyJSONRPC

let json: JSON = [
    ["jsonrpc": "2.0", "id": 1, "method": "mul", "params": [2, 2],
    ["id": 2, "method": "mul", "params": [2, 2],
    ["jsonrpc": "2.0", "id": 3, "method": "div", "params": [4, 2]
]
let request = JSONRPCV2.Request(json: json)

print(request.isBatch) // true
print(request.items[0].id) // 1
print(request.items[1].error) // invalidRequest
print(request.items[2].id) // 3

print(request.toJSON()) // SwiftyJSON.JSON type

Response

import SwiftyJSONRPC

let response = JSONRPCV2.Response(
    isBatch: true,
    items: [
        JSONRPCV2.ResponseItem(id: .number(1), result: [2, 2]),
        JSONRPCV2.ResponseItem(id: .number(2), error: .invalidRequest),
    ]
)

let json = response.toJSON()
print(json) // SwiftyJSON.JSON type

License

SwiftyJSONRPC is released under the MIT license. See LICENSE for details.