Skip to content

Commit

Permalink
feat: Add parameter to return parse tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed May 19, 2021
1 parent 7fda509 commit f0ddea5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
26 changes: 19 additions & 7 deletions src/main/java/foundation/identity/did/DID.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import foundation.identity.did.parser.*;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonValue;

import java.net.URI;
Expand Down Expand Up @@ -174,14 +175,25 @@ public Object visitRules(ArrayList<Rule> rules) {
* Helper methods
*/

public JsonObject toJsonObject() {
public JsonObject toJsonObject(boolean addParseTree) {

JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();

return Json.createObjectBuilder()
jsonObjectBuilder = jsonObjectBuilder
.add("method", this.getMethod() == null ? JsonValue.NULL : Json.createValue(this.getMethod()))
.add("methodSpecificId", this.getMethodSpecificId() == null ? JsonValue.NULL : Json.createValue(this.getMethodSpecificId()))
.add("parseTree", this.getParseTree() == null ? JsonValue.NULL : Json.createValue(this.getParseTree()))
.add("parseRuleCount", this.getParseRuleCount() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<String, Object>(this.getParseRuleCount())).build())
.build();
.add("methodSpecificId", this.getMethodSpecificId() == null ? JsonValue.NULL : Json.createValue(this.getMethodSpecificId()));

if (addParseTree) {
jsonObjectBuilder = jsonObjectBuilder
.add("parseTree", this.getParseTree() == null ? JsonValue.NULL : Json.createValue(this.getParseTree()))
.add("parseRuleCount", this.getParseRuleCount() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<String, Object>(this.getParseRuleCount())).build());
}

return jsonObjectBuilder.build();
}

public JsonObject toJsonObject() {
return toJsonObject(false);
}

/*
Expand Down Expand Up @@ -250,6 +262,6 @@ public boolean equals(Object obj) {
@Override
public String toString() {

return this.didString.toString();
return this.didString;
}
}
28 changes: 20 additions & 8 deletions src/main/java/foundation/identity/did/DIDURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import foundation.identity.did.parser.*;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonValue;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
Expand Down Expand Up @@ -178,17 +179,28 @@ public Object visitRules(ArrayList<Rule> rules) {
* Helper methods
*/

public JsonObject toJsonObject() {
public JsonObject toJsonObject(boolean addParseTree) {

JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();

return Json.createObjectBuilder()
.add("did", this.getDid() == null ? JsonValue.NULL : this.getDid().toJsonObject())
jsonObjectBuilder = jsonObjectBuilder
.add("did", this.getDid() == null ? JsonValue.NULL : this.getDid().toJsonObject(addParseTree))
.add("parameters", this.getParameters() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<String, Object>(this.getParameters())).build())
.add("path", this.getPath() == null ? JsonValue.NULL : Json.createValue(this.getPath()))
.add("query", this.getQuery() == null ? JsonValue.NULL : Json.createValue(this.getQuery()))
.add("fragment", this.getFragment() == null ? JsonValue.NULL : Json.createValue(this.getFragment()))
.add("parseTree", this.getParseTree() == null ? JsonValue.NULL : Json.createValue(this.getParseTree()))
.add("parseRuleCount", this.getParseRuleCount() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<String, Object>(this.getParseRuleCount())).build())
.build();
.add("fragment", this.getFragment() == null ? JsonValue.NULL : Json.createValue(this.getFragment()));

if (addParseTree) {
jsonObjectBuilder = jsonObjectBuilder
.add("parseTree", this.getParseTree() == null ? JsonValue.NULL : Json.createValue(this.getParseTree()))
.add("parseRuleCount", this.getParseRuleCount() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<String, Object>(this.getParseRuleCount())).build());
}

return jsonObjectBuilder.build();
}

public JsonObject toJsonObject() {
return this.toJsonObject(false);
}

/*
Expand Down Expand Up @@ -281,6 +293,6 @@ public boolean equals(Object obj) {
@Override
public String toString() {

return this.didUrlString.toString();
return this.didUrlString;
}
}

0 comments on commit f0ddea5

Please sign in to comment.