Skip to content

Commit f2493c1

Browse files
committed
Add JSON dependency
1 parent 0e71f99 commit f2493c1

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
<version>1.0.0-SNAPSHOT</version>
99

1010
<properties>
11-
<maven.compiler.source>18</maven.compiler.source>
12-
<maven.compiler.target>18</maven.compiler.target>
11+
<maven.compiler.source>17</maven.compiler.source>
12+
<maven.compiler.target>17</maven.compiler.target>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
</properties>
1515

1616
<dependencies>
17+
<dependency>
18+
<groupId>org.json</groupId>
19+
<artifactId>json</artifactId>
20+
<version>20240303</version>
21+
</dependency>
1722
<dependency>
1823
<groupId>junit</groupId>
1924
<artifactId>junit</artifactId>
Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
package com.yocto.yoclib.jsonrpc;
22

3-
public class Message{
3+
import org.json.JSONObject;
4+
5+
public abstract class Message{
6+
7+
private final JSONObject value;
8+
9+
/**
10+
*
11+
* @param value The object value
12+
*/
13+
protected Message(JSONObject value){
14+
this.value = value;
15+
}
16+
17+
/**
18+
*
19+
* @return The JSON-RPC version value
20+
*/
21+
public String getJSONRPC(){
22+
return this.hasJSONRPC()?this.value.getString("jsonrpc"):null;
23+
}
24+
25+
/**
26+
* @return bool
27+
*/
28+
public boolean hasJSONRPC(){
29+
return this.value.has("jsonrpc") && this.value.get("jsonrpc")!=null;
30+
}
31+
32+
/**
33+
*
34+
* @return True if message is a request, false if not.
35+
*/
36+
public boolean isRequest(){
37+
return this.value.has("method") || this.value.has("params");
38+
}
39+
40+
/**
41+
*
42+
* @return True if message is a response, false if not.
43+
*/
44+
public boolean isResponse(){
45+
return this.value.has("result") || this.value.has("error");
46+
}
47+
48+
/**
49+
*
50+
* @return True if message is version 2.0, false if not.
51+
*/
52+
public boolean isVersion2(){
53+
return "2.0".equals(this.getJSONRPC());
54+
}
55+
56+
/**
57+
*
58+
* @return The object value
59+
*/
60+
public JSONObject toObject(){
61+
return this.value;
62+
}
463

564
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package com.yocto.yoclib.jsonrpc;
22

3+
import org.json.JSONObject;
4+
35
public class NotificationMessage extends RequestMessage{
46

7+
/**
8+
* @param value The object value
9+
*/
10+
protected NotificationMessage(JSONObject value) {
11+
super(value);
12+
}
13+
514
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package com.yocto.yoclib.jsonrpc;
22

3+
import org.json.JSONObject;
4+
35
public class RequestMessage extends Message{
46

7+
/**
8+
* @param value The object value
9+
*/
10+
protected RequestMessage(JSONObject value) {
11+
super(value);
12+
}
13+
514
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package com.yocto.yoclib.jsonrpc;
22

3+
import org.json.JSONObject;
4+
35
public class ResponseMessage extends Message{
46

7+
/**
8+
* @param value The object value
9+
*/
10+
protected ResponseMessage(JSONObject value) {
11+
super(value);
12+
}
13+
514
}

0 commit comments

Comments
 (0)