Skip to content

Commit ce569cc

Browse files
committed
feat: Continued work on better JSON-LD support.
1 parent de157c8 commit ce569cc

File tree

22 files changed

+527
-539
lines changed

22 files changed

+527
-539
lines changed

src/main/java/com/danubetech/verifiablecredentials/CredentialSubject.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33

44
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts;
5+
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords;
56
import foundation.identity.jsonld.JsonLDObject;
67
import foundation.identity.jsonld.JsonLDUtils;
8+
import info.weboftrust.ldsignatures.LdProof;
9+
import info.weboftrust.ldsignatures.jsonld.LDSecurityContexts;
10+
import info.weboftrust.ldsignatures.jsonld.LDSecurityKeywords;
711

812
import javax.json.JsonObject;
913
import javax.json.JsonValue;
14+
import java.io.Reader;
15+
import java.net.URI;
1016
import java.util.Map;
1117

1218
public class CredentialSubject extends JsonLDObject {
1319

20+
public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_2018_CREDENTIALS_V1 };
21+
public static final String[] DEFAULT_JSONLD_TYPES = { };
22+
public static final String DEFAULT_JSONLD_PREDICATE = VerifiableCredentialKeywords.JSONLD_TERM_CREDENTIALSUBJECT;
23+
1424
private CredentialSubject() {
1525
super(VerifiableCredentialContexts.DOCUMENT_LOADER);
1626
}
@@ -27,8 +37,8 @@ public static class Builder extends JsonLDObject.Builder<Builder, CredentialSubj
2737

2838
private Map<String, JsonValue> claims;
2939

30-
public Builder() {
31-
super(new CredentialSubject());
40+
public Builder(CredentialSubject jsonLDObject) {
41+
super(jsonLDObject);
3242
}
3343

3444
@Override
@@ -49,16 +59,38 @@ public Builder claims(Map<String, JsonValue> claims) {
4959
}
5060

5161
public static Builder builder() {
62+
return new Builder(new CredentialSubject());
63+
}
64+
65+
/*
66+
* Reading the JSON-LD object
67+
*/
68+
69+
public static CredentialSubject fromJson(Reader reader) {
70+
return JsonLDObject.fromJson(CredentialSubject.class, reader);
71+
}
5272

53-
return new Builder();
73+
public static CredentialSubject fromJson(String json) {
74+
return JsonLDObject.fromJson(CredentialSubject.class, json);
75+
}
76+
77+
/*
78+
* Adding, getting, and removing the JSON-LD object
79+
*/
80+
81+
public static CredentialSubject getFromJsonLDObject(JsonLDObject jsonLdObject) {
82+
return JsonLDObject.getFromJsonLDObject(CredentialSubject.class, jsonLdObject);
83+
}
84+
85+
public static void removeFromJsonLdObject(JsonLDObject jsonLdObject) {
86+
JsonLDObject.removeFromJsonLdObject(CredentialSubject.class, jsonLdObject);
5487
}
5588

5689
/*
5790
* Getters
5891
*/
5992

6093
public Map<String, JsonValue> getClaims() {
61-
6294
return JsonLDUtils.jsonLdGetAsJsonValueMap(this.getJsonObject());
6395
}
6496
}
Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
package com.danubetech.verifiablecredentials;
22

33
import java.io.Reader;
4-
import java.io.StringReader;
54
import java.net.URI;
6-
import java.util.*;
5+
import java.util.Date;
76

87
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts;
98
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords;
10-
import com.danubetech.verifiablecredentials.validation.Validation;
119
import foundation.identity.jsonld.JsonLDObject;
1210
import foundation.identity.jsonld.JsonLDUtils;
1311
import info.weboftrust.ldsignatures.LdProof;
14-
import info.weboftrust.ldsignatures.jsonld.LDSecurityKeywords;
1512

16-
import javax.json.Json;
1713
import javax.json.JsonObject;
1814

1915
public class VerifiableCredential extends JsonLDObject {
2016

21-
public static final String DEFAULT_JSONLD_CONTEXT = "https://www.w3.org/2018/credentials/v1";
22-
public static final String DEFAULT_JSONLD_TYPE = "VerifiableCredential";
17+
public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_2018_CREDENTIALS_V1 };
18+
public static final String[] DEFAULT_JSONLD_TYPES = { VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLE_CREDENTIAL };
19+
public static final String DEFAULT_JSONLD_PREDICATE = VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLECREDENTIAL;
2320

2421
private VerifiableCredential() {
2522
super(VerifiableCredentialContexts.DOCUMENT_LOADER);
2623
}
2724

28-
private VerifiableCredential(JsonObject jsonObject, boolean validate) {
29-
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
30-
if (validate) Validation.validate(this);
31-
}
32-
3325
public VerifiableCredential(JsonObject jsonObject) {
34-
this(jsonObject, true);
26+
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
3527
}
3628

3729
/*
@@ -46,20 +38,21 @@ public static class Builder extends JsonLDObject.Builder<Builder, VerifiableCred
4638
private CredentialSubject credentialSubject;
4739
private LdProof ldProof;
4840

49-
public Builder() {
50-
super(new VerifiableCredential());
41+
public Builder(VerifiableCredential jsonLDObject) {
42+
super(jsonLDObject);
5143
}
5244

45+
@Override
5346
public VerifiableCredential build() {
5447

5548
super.build();
5649

5750
// add JSON-LD properties
58-
if (this.issuer != null) JsonLDUtils.jsonLdAddString(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUER, JsonLDUtils.uriToString(this.issuer));
59-
if (this.issuanceDate != null) JsonLDUtils.jsonLdAddString(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUANCE_DATE, JsonLDUtils.dateToString(this.issuanceDate));
60-
if (this.expirationDate != null) JsonLDUtils.jsonLdAddString(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUANCE_DATE, JsonLDUtils.dateToString(this.expirationDate));
61-
if (this.credentialSubject != null) JsonLDUtils.jsonLdAddJsonValue(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_CREDENTIAL_SUBJECT, this.credentialSubject.getJsonObject());
62-
if (this.ldProof != null) JsonLDUtils.jsonLdAddJsonValue(this.jsonLDObject.getJsonObjectBuilder(), LDSecurityKeywords.JSONLD_TERM_PROOF, this.ldProof.getJsonObject());
51+
if (this.issuer != null) JsonLDUtils.jsonLdAddString(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUER, JsonLDUtils.uriToString(this.issuer));
52+
if (this.issuanceDate != null) JsonLDUtils.jsonLdAddString(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUANCEDATE, JsonLDUtils.dateToString(this.issuanceDate));
53+
if (this.expirationDate != null) JsonLDUtils.jsonLdAddString(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_EXPIRATIONDATE, JsonLDUtils.dateToString(this.expirationDate));
54+
if (this.credentialSubject != null) this.credentialSubject.addToJsonLDObject(this.jsonLDObject);
55+
if (this.ldProof != null) this.ldProof.addToJsonLDObject(this.jsonLDObject);
6356

6457
return this.jsonLDObject;
6558
}
@@ -91,55 +84,57 @@ public Builder ldProof(LdProof ldProof) {
9184
}
9285

9386
public static Builder builder() {
94-
95-
return new Builder()
96-
.context(DEFAULT_JSONLD_CONTEXT)
97-
.type(DEFAULT_JSONLD_TYPE);
87+
return new Builder(new VerifiableCredential())
88+
.defaultContexts(true)
89+
.defaultTypes(true);
9890
}
9991

10092
/*
101-
* Serialization
93+
* Reading the JSON-LD object
10294
*/
10395

104-
public static VerifiableCredential fromJson(Reader reader, boolean validate) {
105-
JsonObject jsonObject = Json.createReader(reader).readObject();
106-
return new VerifiableCredential(jsonObject, validate);
96+
public static VerifiableCredential fromJson(Reader reader) {
97+
return JsonLDObject.fromJson(VerifiableCredential.class, reader);
10798
}
10899

109-
public static VerifiableCredential fromJson(String json, boolean validate) {
110-
return fromJson(new StringReader(json), validate);
100+
public static VerifiableCredential fromJson(String json) {
101+
return JsonLDObject.fromJson(VerifiableCredential.class, json);
111102
}
112103

113-
public static VerifiableCredential fromJson(Reader reader) {
114-
return fromJson(reader, true);
104+
/*
105+
* Adding, getting, and removing the JSON-LD object
106+
*/
107+
108+
public static VerifiableCredential getFromJsonLDObject(JsonLDObject jsonLdObject) {
109+
return JsonLDObject.getFromJsonLDObject(VerifiableCredential.class, jsonLdObject);
115110
}
116111

117-
public static VerifiableCredential fromJson(String json) {
118-
return fromJson(json, true);
112+
public static void removeFromJsonLdObject(JsonLDObject jsonLdObject) {
113+
JsonLDObject.removeFromJsonLdObject(VerifiableCredential.class, jsonLdObject);
119114
}
120115

121116
/*
122117
* Getters
123118
*/
124119

125120
@SuppressWarnings("unchecked")
126-
public String getIssuer() {
127-
return JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUER);
121+
public URI getIssuer() {
122+
return JsonLDUtils.stringToUri(JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUER));
128123
}
129124

130125
public Date getIssuanceDate() {
131-
return JsonLDUtils.stringToDate(JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUANCE_DATE));
126+
return JsonLDUtils.stringToDate(JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_ISSUANCEDATE));
132127
}
133128

134129
public Date getExpirationDate() {
135-
return JsonLDUtils.stringToDate(JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_EXPIRATION_DATE));
130+
return JsonLDUtils.stringToDate(JsonLDUtils.jsonLdGetString(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_EXPIRATIONDATE));
136131
}
137132

138133
public CredentialSubject getCredentialSubject() {
139-
return new CredentialSubject(JsonLDUtils.jsonLdGetJsonObject(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_CREDENTIAL_SUBJECT));
134+
return CredentialSubject.getFromJsonLDObject(this);
140135
}
141136

142137
public LdProof getLdProof() {
143-
return new LdProof(JsonLDUtils.jsonLdGetJsonObject(this.getJsonObject(), LDSecurityKeywords.JSONLD_TERM_PROOF));
138+
return LdProof.getFromJsonLDObject(this);
144139
}
145140
}
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
package com.danubetech.verifiablecredentials;
22

33
import java.io.Reader;
4-
import java.io.StringReader;
4+
import java.net.URI;
55

66
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts;
77
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords;
88
import com.danubetech.verifiablecredentials.validation.Validation;
99

1010
import foundation.identity.jsonld.JsonLDObject;
11-
import foundation.identity.jsonld.JsonLDUtils;
1211
import info.weboftrust.ldsignatures.LdProof;
13-
import info.weboftrust.ldsignatures.jsonld.LDSecurityKeywords;
1412

15-
import javax.json.Json;
1613
import javax.json.JsonObject;
1714

1815
public class VerifiablePresentation extends JsonLDObject {
1916

20-
public static final String DEFAULT_JSONLD_CONTEXT = "https://www.w3.org/2018/credentials/v1";
21-
public static final String DEFAULT_JSONLD_TYPE = "VerifiablePresentation";
17+
public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_2018_CREDENTIALS_V1 };
18+
public static final String[] DEFAULT_JSONLD_TYPES = { VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLE_PRESENTATION };
19+
public static final String DEFAULT_JSONLD_PREDICATE = null;
2220

2321
private VerifiablePresentation() {
2422
super(VerifiableCredentialContexts.DOCUMENT_LOADER);
2523
}
2624

27-
private VerifiablePresentation(JsonObject jsonObject, boolean validate) {
28-
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
29-
if (validate) Validation.validate(this);
30-
}
31-
3225
public VerifiablePresentation(JsonObject jsonObject) {
33-
this(jsonObject, true);
26+
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
3427
}
3528

3629
/*
@@ -42,57 +35,61 @@ public static class Builder extends JsonLDObject.Builder<VerifiablePresentation.
4235
private VerifiableCredential verifiableCredential;
4336
private LdProof ldProof;
4437

45-
public Builder() {
46-
super(new VerifiablePresentation());
38+
public Builder(VerifiablePresentation jsonLDObject) {
39+
super(jsonLDObject);
4740
}
4841

42+
@Override
4943
public VerifiablePresentation build() {
5044

5145
super.build();
5246

5347
// add JSON-LD properties
54-
if (this.verifiableCredential != null) JsonLDUtils.jsonLdAddJsonValue(this.jsonLDObject.getJsonObjectBuilder(), VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLE_CREDENTIAL, this.verifiableCredential.getJsonObject());
48+
if (this.verifiableCredential != null) this.verifiableCredential.addToJsonLDObject(this.jsonLDObject);
49+
if (this.ldProof != null) this.ldProof.addToJsonLDObject(this.jsonLDObject);
5550

5651
return this.jsonLDObject;
5752
}
5853

59-
public VerifiablePresentation.Builder verifiableCredential(VerifiableCredential verifiableCredential) {
54+
public Builder verifiableCredential(VerifiableCredential verifiableCredential) {
6055
this.verifiableCredential = verifiableCredential;
6156
return this;
6257
}
6358

64-
public VerifiablePresentation.Builder ldProof(LdProof ldProof) {
59+
public Builder ldProof(LdProof ldProof) {
6560
this.ldProof = ldProof;
6661
return this;
6762
}
6863
}
6964

70-
public static VerifiablePresentation.Builder builder() {
71-
72-
return new VerifiablePresentation.Builder()
73-
.context(DEFAULT_JSONLD_CONTEXT)
74-
.type(DEFAULT_JSONLD_TYPE);
65+
public static Builder builder() {
66+
return new Builder(new VerifiablePresentation())
67+
.defaultContexts(true)
68+
.defaultTypes(true);
7569
}
7670

7771
/*
78-
* Serialization
72+
* Reading the JSON-LD object
7973
*/
8074

81-
public static VerifiablePresentation fromJson(Reader reader, boolean validate) {
82-
JsonObject jsonObject = Json.createReader(reader).readObject();
83-
return new VerifiablePresentation(jsonObject, validate);
75+
public static VerifiablePresentation fromJson(Reader reader) {
76+
return JsonLDObject.fromJson(VerifiablePresentation.class, reader);
8477
}
8578

86-
public static VerifiablePresentation fromJson(String json, boolean validate) {
87-
return fromJson(new StringReader(json), validate);
79+
public static VerifiablePresentation fromJson(String json) {
80+
return JsonLDObject.fromJson(VerifiablePresentation.class, json);
8881
}
8982

90-
public static VerifiablePresentation fromJson(Reader reader) {
91-
return fromJson(reader, true);
83+
/*
84+
* Adding, getting, and removing the JSON-LD object
85+
*/
86+
87+
public static VerifiablePresentation getFromJsonLDObject(JsonLDObject jsonLdObject) {
88+
return JsonLDObject.getFromJsonLDObject(VerifiablePresentation.class, jsonLdObject);
9289
}
9390

94-
public static VerifiablePresentation fromJson(String json) {
95-
return fromJson(json, true);
91+
public static void removeFromJsonLdObject(JsonLDObject jsonLdObject) {
92+
JsonLDObject.removeFromJsonLdObject(VerifiablePresentation.class, jsonLdObject);
9693
}
9794

9895
/*
@@ -123,12 +120,16 @@ public static VerifiablePresentation fromVerifiableCredential(VerifiableCredenti
123120
}
124121
*/
125122

123+
/*
124+
* Getters
125+
*/
126+
126127
@SuppressWarnings("unchecked")
127128
public VerifiableCredential getVerifiableCredential() {
128-
return new VerifiableCredential(JsonLDUtils.jsonLdGetJsonObject(this.getJsonObject(), VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLE_CREDENTIAL));
129+
return VerifiableCredential.getFromJsonLDObject(this);
129130
}
130131

131132
public LdProof getLdProof() {
132-
return new LdProof(JsonLDUtils.jsonLdGetJsonObject(this.getJsonObject(), LDSecurityKeywords.JSONLD_TERM_PROOF));
133+
return LdProof.getFromJsonLDObject(this);
133134
}
134135
}

0 commit comments

Comments
 (0)