11package com .danubetech .verifiablecredentials ;
22
33import java .io .Reader ;
4- import java .io .StringReader ;
54import java .net .URI ;
6- import java .util .* ;
5+ import java .util .Date ;
76
87import com .danubetech .verifiablecredentials .jsonld .VerifiableCredentialContexts ;
98import com .danubetech .verifiablecredentials .jsonld .VerifiableCredentialKeywords ;
10- import com .danubetech .verifiablecredentials .validation .Validation ;
119import foundation .identity .jsonld .JsonLDObject ;
1210import foundation .identity .jsonld .JsonLDUtils ;
1311import info .weboftrust .ldsignatures .LdProof ;
14- import info .weboftrust .ldsignatures .jsonld .LDSecurityKeywords ;
1512
16- import javax .json .Json ;
1713import javax .json .JsonObject ;
1814
1915public 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}
0 commit comments