Skip to content

Commit 49ae141

Browse files
committed
Further updates to JSON-LD handling.
Signed-off-by: Markus Sabadello <[email protected]>
1 parent 6f87625 commit 49ae141

File tree

5 files changed

+117
-99
lines changed

5 files changed

+117
-99
lines changed

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.danubetech.verifiablecredentials;
22

33

4+
import com.apicatalog.jsonld.loader.DocumentLoader;
45
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts;
56
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords;
7+
import com.fasterxml.jackson.annotation.JsonCreator;
68
import foundation.identity.jsonld.JsonLDKeywords;
79
import foundation.identity.jsonld.JsonLDObject;
810
import foundation.identity.jsonld.JsonLDUtils;
@@ -17,20 +19,22 @@ public class CredentialSubject extends JsonLDObject {
1719
public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_2018_CREDENTIALS_V1 };
1820
public static final String[] DEFAULT_JSONLD_TYPES = { };
1921
public static final String DEFAULT_JSONLD_PREDICATE = VerifiableCredentialKeywords.JSONLD_TERM_CREDENTIALSUBJECT;
22+
public static final DocumentLoader DEFAULT_DOCUMENT_LOADER = VerifiableCredentialContexts.DOCUMENT_LOADER;
2023

21-
private CredentialSubject() {
22-
super(VerifiableCredentialContexts.DOCUMENT_LOADER);
24+
@JsonCreator
25+
public CredentialSubject() {
26+
super();
2327
}
2428

25-
public CredentialSubject(Map<String, Object> jsonObject) {
26-
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
29+
protected CredentialSubject(Map<String, Object> jsonObject) {
30+
super(jsonObject);
2731
}
2832

2933
/*
3034
* Factory methods
3135
*/
3236

33-
public static class Builder extends JsonLDObject.Builder<Builder, CredentialSubject> {
37+
public static class Builder<B extends Builder<B>> extends JsonLDObject.Builder<B> {
3438

3539
private Map<String, Object> claims;
3640

@@ -46,29 +50,29 @@ public CredentialSubject build() {
4650
// add JSON-LD properties
4751
if (this.claims != null) JsonLDUtils.jsonLdAddAll(this.jsonLDObject, this.claims);
4852

49-
return this.jsonLDObject;
53+
return (CredentialSubject) this.jsonLDObject;
5054
}
5155

52-
public Builder claims(Map<String, Object> claims) {
56+
public B claims(Map<String, Object> claims) {
5357
this.claims = claims;
54-
return this;
58+
return (B) this;
5559
}
5660
}
5761

58-
public static Builder builder() {
62+
public static Builder<? extends Builder<?>> builder() {
5963
return new Builder(new CredentialSubject());
6064
}
6165

62-
/*
63-
* Reading the JSON-LD object
64-
*/
66+
public static CredentialSubject fromJsonObject(Map<String, Object> jsonObject) {
67+
return new CredentialSubject(jsonObject);
68+
}
6569

6670
public static CredentialSubject fromJson(Reader reader) {
67-
return JsonLDObject.fromJson(CredentialSubject.class, reader);
71+
return new CredentialSubject(readJson(reader));
6872
}
6973

7074
public static CredentialSubject fromJson(String json) {
71-
return JsonLDObject.fromJson(CredentialSubject.class, json);
75+
return new CredentialSubject(readJson(json));
7276
}
7377

7478
/*

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.danubetech.verifiablecredentials;
22

3+
import com.apicatalog.jsonld.loader.DocumentLoader;
34
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts;
45
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords;
6+
import com.fasterxml.jackson.annotation.JsonCreator;
57
import foundation.identity.jsonld.JsonLDObject;
68
import foundation.identity.jsonld.JsonLDUtils;
79
import info.weboftrust.ldsignatures.LdProof;
@@ -16,20 +18,22 @@ public class VerifiableCredential extends JsonLDObject {
1618
public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_2018_CREDENTIALS_V1 };
1719
public static final String[] DEFAULT_JSONLD_TYPES = { VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLE_CREDENTIAL };
1820
public static final String DEFAULT_JSONLD_PREDICATE = VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLECREDENTIAL;
21+
public static final DocumentLoader DEFAULT_DOCUMENT_LOADER = VerifiableCredentialContexts.DOCUMENT_LOADER;
1922

20-
private VerifiableCredential() {
21-
super(VerifiableCredentialContexts.DOCUMENT_LOADER);
23+
@JsonCreator
24+
public VerifiableCredential() {
25+
super();
2226
}
2327

24-
public VerifiableCredential(Map<String, Object> jsonObject) {
25-
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
28+
protected VerifiableCredential(Map<String, Object> jsonObject) {
29+
super(jsonObject);
2630
}
2731

2832
/*
2933
* Factory methods
3034
*/
3135

32-
public static class Builder extends JsonLDObject.Builder<Builder, VerifiableCredential> {
36+
public static class Builder<B extends Builder<B>> extends JsonLDObject.Builder<B> {
3337

3438
private URI issuer;
3539
private Date issuanceDate;
@@ -39,6 +43,8 @@ public static class Builder extends JsonLDObject.Builder<Builder, VerifiableCred
3943

4044
public Builder(VerifiableCredential jsonLDObject) {
4145
super(jsonLDObject);
46+
this.defaultContexts(true);
47+
this.defaultTypes(true);
4248
}
4349

4450
@Override
@@ -53,51 +59,49 @@ public VerifiableCredential build() {
5359
if (this.credentialSubject != null) this.credentialSubject.addToJsonLDObject(this.jsonLDObject);
5460
if (this.ldProof != null) this.ldProof.addToJsonLDObject(this.jsonLDObject);
5561

56-
return this.jsonLDObject;
62+
return (VerifiableCredential) this.jsonLDObject;
5763
}
5864

59-
public Builder issuer(URI issuer) {
65+
public B issuer(URI issuer) {
6066
this.issuer = issuer;
61-
return this;
67+
return (B) this;
6268
}
6369

64-
public Builder issuanceDate(Date issuanceDate) {
70+
public B issuanceDate(Date issuanceDate) {
6571
this.issuanceDate = issuanceDate;
66-
return this;
72+
return (B) this;
6773
}
6874

69-
public Builder expirationDate(Date expirationDate) {
75+
public B expirationDate(Date expirationDate) {
7076
this.expirationDate = expirationDate;
71-
return this;
77+
return (B) this;
7278
}
7379

74-
public Builder credentialSubject(CredentialSubject credentialSubject) {
80+
public B credentialSubject(CredentialSubject credentialSubject) {
7581
this.credentialSubject = credentialSubject;
76-
return this;
82+
return (B) this;
7783
}
7884

79-
public Builder ldProof(LdProof ldProof) {
85+
public B ldProof(LdProof ldProof) {
8086
this.ldProof = ldProof;
81-
return this;
87+
return (B) this;
8288
}
8389
}
8490

85-
public static Builder builder() {
86-
return new Builder(new VerifiableCredential())
87-
.defaultContexts(true)
88-
.defaultTypes(true);
91+
public static Builder<? extends Builder<?>> builder() {
92+
return new Builder(new VerifiableCredential());
8993
}
9094

91-
/*
92-
* Reading the JSON-LD object
93-
*/
95+
public static VerifiableCredential fromJsonObject(Map<String, Object> jsonObject) {
96+
return new VerifiableCredential(jsonObject);
97+
}
9498

9599
public static VerifiableCredential fromJson(Reader reader) {
96-
return JsonLDObject.fromJson(VerifiableCredential.class, reader);
100+
return new VerifiableCredential(readJson(reader));
97101
}
98102

99103
public static VerifiableCredential fromJson(String json) {
100-
return JsonLDObject.fromJson(VerifiableCredential.class, json);
104+
return new VerifiableCredential(readJson(json));
101105
}
102106

103107
/*

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.danubetech.verifiablecredentials;
22

3+
import com.apicatalog.jsonld.loader.DocumentLoader;
34
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts;
45
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords;
6+
import com.fasterxml.jackson.annotation.JsonCreator;
57
import foundation.identity.jsonld.JsonLDObject;
68
import info.weboftrust.ldsignatures.LdProof;
79

@@ -14,26 +16,30 @@ public class VerifiablePresentation extends JsonLDObject {
1416
public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_2018_CREDENTIALS_V1 };
1517
public static final String[] DEFAULT_JSONLD_TYPES = { VerifiableCredentialKeywords.JSONLD_TERM_VERIFIABLE_PRESENTATION };
1618
public static final String DEFAULT_JSONLD_PREDICATE = null;
19+
public static final DocumentLoader DEFAULT_DOCUMENT_LOADER = VerifiableCredentialContexts.DOCUMENT_LOADER;
1720

18-
private VerifiablePresentation() {
19-
super(VerifiableCredentialContexts.DOCUMENT_LOADER);
21+
@JsonCreator
22+
public VerifiablePresentation() {
23+
super();
2024
}
2125

22-
public VerifiablePresentation(Map<String, Object> jsonObject) {
23-
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
26+
protected VerifiablePresentation(Map<String, Object> jsonObject) {
27+
super(jsonObject);
2428
}
2529

2630
/*
2731
* Factory methods
2832
*/
2933

30-
public static class Builder extends JsonLDObject.Builder<VerifiablePresentation.Builder, VerifiablePresentation> {
34+
public static class Builder<B extends Builder<B>> extends JsonLDObject.Builder<B> {
3135

3236
private VerifiableCredential verifiableCredential;
3337
private LdProof ldProof;
3438

3539
public Builder(VerifiablePresentation jsonLDObject) {
3640
super(jsonLDObject);
41+
this.defaultContexts(true);
42+
this.defaultTypes(true);
3743
}
3844

3945
@Override
@@ -45,36 +51,34 @@ public VerifiablePresentation build() {
4551
if (this.verifiableCredential != null) this.verifiableCredential.addToJsonLDObject(this.jsonLDObject);
4652
if (this.ldProof != null) this.ldProof.addToJsonLDObject(this.jsonLDObject);
4753

48-
return this.jsonLDObject;
54+
return (VerifiablePresentation) this.jsonLDObject;
4955
}
5056

51-
public Builder verifiableCredential(VerifiableCredential verifiableCredential) {
57+
public B verifiableCredential(VerifiableCredential verifiableCredential) {
5258
this.verifiableCredential = verifiableCredential;
53-
return this;
59+
return (B) this;
5460
}
5561

56-
public Builder ldProof(LdProof ldProof) {
62+
public B ldProof(LdProof ldProof) {
5763
this.ldProof = ldProof;
58-
return this;
64+
return (B) this;
5965
}
6066
}
6167

62-
public static Builder builder() {
63-
return new Builder(new VerifiablePresentation())
64-
.defaultContexts(true)
65-
.defaultTypes(true);
68+
public static Builder<? extends Builder<?>> builder() {
69+
return new Builder(new VerifiablePresentation());
6670
}
6771

68-
/*
69-
* Reading the JSON-LD object
70-
*/
72+
public static VerifiablePresentation fromJsonObject(Map<String, Object> jsonObject) {
73+
return new VerifiablePresentation(jsonObject);
74+
}
7175

7276
public static VerifiablePresentation fromJson(Reader reader) {
73-
return JsonLDObject.fromJson(VerifiablePresentation.class, reader);
77+
return new VerifiablePresentation(readJson(reader));
7478
}
7579

7680
public static VerifiablePresentation fromJson(String json) {
77-
return JsonLDObject.fromJson(VerifiablePresentation.class, json);
81+
return new VerifiablePresentation(readJson(json));
7882
}
7983

8084
/*

src/main/java/com/danubetech/verifiablecredentials/credentialstatus/RevocationQuery2020Status.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.danubetech.verifiablecredentials.credentialstatus;
22

3-
import com.danubetech.verifiablecredentials.CredentialSubject;
3+
import com.apicatalog.jsonld.loader.DocumentLoader;
44
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialContexts;
55
import com.danubetech.verifiablecredentials.jsonld.VerifiableCredentialKeywords;
6+
import com.danubetech.verifiablecredentials.proof.BlockchainHashProof2020;
7+
import com.fasterxml.jackson.annotation.JsonCreator;
68
import foundation.identity.jsonld.JsonLDObject;
79
import foundation.identity.jsonld.JsonLDUtils;
10+
import info.weboftrust.ldsignatures.LdProof;
811

912
import java.io.Reader;
1013
import java.net.URI;
@@ -15,20 +18,22 @@ public class RevocationQuery2020Status extends JsonLDObject {
1518
public static final URI[] DEFAULT_JSONLD_CONTEXTS = { VerifiableCredentialContexts.JSONLD_CONTEXT_W3C_2018_CREDENTIALS_V1 };
1619
public static final String[] DEFAULT_JSONLD_TYPES = { VerifiableCredentialKeywords.JSONLD_TERM_REVOCATION_QUERY_2020_STATUS };
1720
public static final String DEFAULT_JSONLD_PREDICATE = VerifiableCredentialKeywords.JSONLD_TERM_CREDENTIALSUBJECT;
21+
public static final DocumentLoader DEFAULT_DOCUMENT_LOADER = VerifiableCredentialContexts.DOCUMENT_LOADER;
1822

19-
private RevocationQuery2020Status() {
20-
super(VerifiableCredentialContexts.DOCUMENT_LOADER);
23+
@JsonCreator
24+
public RevocationQuery2020Status() {
25+
super();
2126
}
2227

23-
public RevocationQuery2020Status(Map<String, Object> jsonObject) {
24-
super(VerifiableCredentialContexts.DOCUMENT_LOADER, jsonObject);
28+
protected RevocationQuery2020Status(Map<String, Object> jsonObject) {
29+
super(jsonObject);
2530
}
2631

2732
/*
2833
* Factory methods
2934
*/
3035

31-
public static class Builder extends JsonLDObject.Builder<Builder, RevocationQuery2020Status> {
36+
public static class Builder<B extends Builder<B>> extends JsonLDObject.Builder<B> {
3237

3338
private String credentialReference;
3439
private String revocationService;
@@ -46,46 +51,46 @@ public RevocationQuery2020Status build() {
4651
if (this.credentialReference != null) JsonLDUtils.jsonLdAdd(this.jsonLDObject, VerifiableCredentialKeywords.JSONLD_TERM_CREDENTIALREFERENCE, this.credentialReference);
4752
if (this.revocationService != null) JsonLDUtils.jsonLdAdd(this.jsonLDObject, VerifiableCredentialKeywords.JSONLD_TERM_REVOCATIONSERVICE, this.revocationService);
4853

49-
return this.jsonLDObject;
54+
return (RevocationQuery2020Status) this.jsonLDObject;
5055
}
5156

52-
public Builder credentialReference(String credentialReference) {
57+
public B credentialReference(String credentialReference) {
5358
this.credentialReference = credentialReference;
54-
return this;
59+
return (B) this;
5560
}
5661

57-
public Builder revocationService(String revocationService) {
62+
public B revocationService(String revocationService) {
5863
this.revocationService = revocationService;
59-
return this;
64+
return (B) this;
6065
}
6166
}
6267

63-
public static Builder builder() {
68+
public static Builder<? extends Builder<?>> builder() {
6469
return new Builder(new RevocationQuery2020Status());
6570
}
6671

67-
/*
68-
* Reading the JSON-LD object
69-
*/
72+
public static RevocationQuery2020Status fromJsonObject(Map<String, Object> jsonObject) {
73+
return new RevocationQuery2020Status(jsonObject);
74+
}
7075

71-
public static CredentialSubject fromJson(Reader reader) {
72-
return JsonLDObject.fromJson(CredentialSubject.class, reader);
76+
public static RevocationQuery2020Status fromJson(Reader reader) {
77+
return new RevocationQuery2020Status(readJson(reader));
7378
}
7479

75-
public static CredentialSubject fromJson(String json) {
76-
return JsonLDObject.fromJson(CredentialSubject.class, json);
80+
public static RevocationQuery2020Status fromJson(String json) {
81+
return new RevocationQuery2020Status(readJson(json));
7782
}
7883

7984
/*
8085
* Adding, getting, and removing the JSON-LD object
8186
*/
8287

83-
public static CredentialSubject getFromJsonLDObject(JsonLDObject jsonLdObject) {
84-
return JsonLDObject.getFromJsonLDObject(CredentialSubject.class, jsonLdObject);
88+
public static RevocationQuery2020Status getFromJsonLDObject(JsonLDObject jsonLdObject) {
89+
return JsonLDObject.getFromJsonLDObject(RevocationQuery2020Status.class, jsonLdObject);
8590
}
8691

8792
public static void removeFromJsonLdObject(JsonLDObject jsonLdObject) {
88-
JsonLDObject.removeFromJsonLdObject(CredentialSubject.class, jsonLdObject);
93+
JsonLDObject.removeFromJsonLdObject(RevocationQuery2020Status.class, jsonLdObject);
8994
}
9095

9196
/*

0 commit comments

Comments
 (0)