Skip to content

Commit

Permalink
Add JSON dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed May 19, 2024
1 parent 0e71f99 commit f2493c1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
<version>1.0.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
61 changes: 60 additions & 1 deletion src/main/java/com/yocto/yoclib/jsonrpc/Message.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
package com.yocto.yoclib.jsonrpc;

public class Message{
import org.json.JSONObject;

public abstract class Message{

private final JSONObject value;

/**
*
* @param value The object value
*/
protected Message(JSONObject value){
this.value = value;
}

/**
*
* @return The JSON-RPC version value
*/
public String getJSONRPC(){
return this.hasJSONRPC()?this.value.getString("jsonrpc"):null;
}

/**
* @return bool
*/
public boolean hasJSONRPC(){
return this.value.has("jsonrpc") && this.value.get("jsonrpc")!=null;
}

/**
*
* @return True if message is a request, false if not.
*/
public boolean isRequest(){
return this.value.has("method") || this.value.has("params");
}

/**
*
* @return True if message is a response, false if not.
*/
public boolean isResponse(){
return this.value.has("result") || this.value.has("error");
}

/**
*
* @return True if message is version 2.0, false if not.
*/
public boolean isVersion2(){
return "2.0".equals(this.getJSONRPC());
}

/**
*
* @return The object value
*/
public JSONObject toObject(){
return this.value;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.yocto.yoclib.jsonrpc;

import org.json.JSONObject;

public class NotificationMessage extends RequestMessage{

/**
* @param value The object value
*/
protected NotificationMessage(JSONObject value) {
super(value);
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/yocto/yoclib/jsonrpc/RequestMessage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.yocto.yoclib.jsonrpc;

import org.json.JSONObject;

public class RequestMessage extends Message{

/**
* @param value The object value
*/
protected RequestMessage(JSONObject value) {
super(value);
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/yocto/yoclib/jsonrpc/ResponseMessage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.yocto.yoclib.jsonrpc;

import org.json.JSONObject;

public class ResponseMessage extends Message{

/**
* @param value The object value
*/
protected ResponseMessage(JSONObject value) {
super(value);
}

}

0 comments on commit f2493c1

Please sign in to comment.