Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inrupt.client.spi.VerifiableCredential;

import java.io.IOException;
Expand Down Expand Up @@ -88,7 +87,7 @@ public VerifiableCredential deserialize(final JsonParser jp, final Deserializati
}

if (node.get("credentialSubject") != null) {
final ObjectNode credentialSubject = (ObjectNode) node.get("credentialSubject");
final JsonNode credentialSubject = node.get("credentialSubject");
final Map<String, Object> finalCredentialSubject = new HashMap<>();
credentialSubject.fields().forEachRemaining(field -> {
finalCredentialSubject.put(field.getKey(), field.getValue());
Expand All @@ -97,7 +96,7 @@ public VerifiableCredential deserialize(final JsonParser jp, final Deserializati
}

if (node.get("credentialStatus") != null) {
final ObjectNode credentialStatus = (ObjectNode) node.path("credentialStatus");
final JsonNode credentialStatus = node.path("credentialStatus");
final Map<String, Object> finalCredentialStatus = new HashMap<>();
credentialStatus.fields().forEachRemaining(field -> {
finalCredentialStatus.put(field.getKey(), field.getValue());
Expand All @@ -106,7 +105,7 @@ public VerifiableCredential deserialize(final JsonParser jp, final Deserializati
}

if (node.get("proof") != null) {
final ObjectNode proof = (ObjectNode) node.get("proof");
final JsonNode proof = node.get("proof");
final Map<String, Object> finalProof = new HashMap<>();
proof.fields().forEachRemaining(field -> {
finalProof.put(field.getKey(), field.getValue());
Expand Down
69 changes: 53 additions & 16 deletions jackson/src/main/java/com/inrupt/client/jackson/VCSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,68 @@ public void serialize(final VerifiableCredential vc, final JsonGenerator jgen,

jgen.writeStartObject();

jgen.writeFieldName("@context");
jgen.writeStartArray();
for (final String context: vc.context) {
jgen.writeString(context);
if (vc.context != null) {
jgen.writeFieldName("@context");
jgen.writeStartArray();
for (final var context : vc.context) {
jgen.writeString(context);
}
jgen.writeEndArray();
}
jgen.writeEndArray();

jgen.writeStringField("id", vc.id);
if (vc.id != null) {
jgen.writeStringField("id", vc.id);
}

jgen.writeFieldName("type");
jgen.writeStartArray();
for (final String type: vc.type) {
jgen.writeString(type);
if (vc.type != null) {
jgen.writeFieldName("type");
jgen.writeStartArray();
for (final var type : vc.type) {
jgen.writeString(type);
}
jgen.writeEndArray();
}
jgen.writeEndArray();

jgen.writeStringField("issuer", vc.issuer);
if (vc.issuer != null) {
jgen.writeStringField("issuer", vc.issuer);
}

jgen.writeStringField("issuanceDate", vc.issuanceDate.toString());
if (vc.issuanceDate != null) {
jgen.writeStringField("issuanceDate", vc.issuanceDate.toString());
}

jgen.writeStringField("credentialSubject", vc.credentialSubject.toString());
if (vc.credentialSubject != null) {
jgen.writeFieldName("credentialSubject");
jgen.writeStartObject();
final var itCSubject = vc.credentialSubject.entrySet().iterator();
while (itCSubject.hasNext()) {
final var entry = itCSubject.next();
jgen.writePOJOField(entry.getKey(), entry.getValue());
}
jgen.writeEndObject();
}

jgen.writeStringField("credentialStatus", vc.credentialStatus.toString());
if (vc.credentialStatus != null) {
jgen.writeFieldName("credentialStatus");
jgen.writeStartObject();
final var itCStatus = vc.credentialStatus.entrySet().iterator();
while (itCStatus.hasNext()) {
final var entry = itCStatus.next();
jgen.writePOJOField(entry.getKey(), entry.getValue());
}
jgen.writeEndObject();
}

jgen.writeStringField("proof", vc.proof.toString());
if (vc.proof != null) {
jgen.writeFieldName("proof");
jgen.writeStartObject();
final var itProof = vc.proof.entrySet().iterator();
while (itProof.hasNext()) {
final var entry = itProof.next();
jgen.writePOJOField(entry.getKey(), entry.getValue());
}
jgen.writeEndObject();
}

jgen.writeEndObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inrupt.client.spi.VerifiableCredential;
import com.inrupt.client.spi.VerifiablePresentation;

Expand Down Expand Up @@ -89,7 +88,7 @@ public VerifiablePresentation deserialize(final JsonParser jp, final Deserializa
}

if (node.get("proof") != null) {
final ObjectNode proof = (ObjectNode) node.get("proof");
final JsonNode proof = node.get("proof");
final Map<String, Object> finalProof = new HashMap<>();
proof.fields().forEachRemaining(field -> {
finalProof.put(field.getKey(), field.getValue());
Expand Down
56 changes: 37 additions & 19 deletions jackson/src/main/java/com/inrupt/client/jackson/VPSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.inrupt.client.spi.VerifiableCredential;
import com.inrupt.client.spi.VerifiablePresentation;

import java.io.IOException;
Expand All @@ -44,32 +43,51 @@ public void serialize(final VerifiablePresentation vp, final JsonGenerator jgen,

jgen.writeStartObject();

jgen.writeFieldName("@context");
jgen.writeStartArray();
for (final String context: vp.context) {
jgen.writeString(context);
if (vp.context != null) {
jgen.writeFieldName("@context");
jgen.writeStartArray();
for (final var context : vp.context) {
jgen.writeString(context);
}
jgen.writeEndArray();
}
jgen.writeEndArray();

jgen.writeStringField("id", vp.id);
if (vp.id != null) {
jgen.writeStringField("id", vp.id);
}

jgen.writeFieldName("type");
jgen.writeStartArray();
for (final String type: vp.type) {
jgen.writeString(type);
if (vp.type != null) {
jgen.writeFieldName("type");
jgen.writeStartArray();
for (final var type : vp.type) {
jgen.writeString(type);
}
jgen.writeEndArray();
}
jgen.writeEndArray();

jgen.writeStringField("holder", vp.holder);
if (vp.holder != null) {
jgen.writeStringField("holder", vp.holder);
}

jgen.writeFieldName("verifiableCredential");
jgen.writeStartArray();
for (final VerifiableCredential vc: vp.verifiableCredential) {
jgen.writeObject(vc);
if (vp.verifiableCredential != null) {
jgen.writeFieldName("verifiableCredential");
jgen.writeStartArray();
for (final var vc : vp.verifiableCredential) {
jgen.writePOJO(vc);
}
jgen.writeEndArray();
}
jgen.writeEndArray();

jgen.writeStringField("proof", vp.proof.toString());
if (vp.proof != null) {
jgen.writeFieldName("proof");
jgen.writeStartObject();
final var itProof = vp.proof.entrySet().iterator();
while (itProof.hasNext()) {
final var entry = itProof.next();
jgen.writeObjectField(entry.getKey(), entry.getValue());
}
jgen.writeEndObject();
}

jgen.writeEndObject();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2022 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.inrupt.client.jackson;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.inrupt.client.spi.JsonProcessor;
import com.inrupt.client.spi.ServiceProvider;
import com.inrupt.client.spi.VerifiableCredential;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.UUID;

import org.junit.jupiter.api.Test;

public class JacksonVCTest {

private static final JsonProcessor processor = ServiceProvider.getJsonProcessor();
private static VerifiableCredential vc;
private static VerifiableCredential vcCopy;

@Test
void roundtripVC() throws IOException {
try (final var res = JacksonVCTest.class.getResourceAsStream("/verifiableCredential.json")) {
vc = processor.fromJson(res, VerifiableCredential.class);
}

final var targetPath = new File("target").toPath();
final var testFolderName = UUID.randomUUID().toString();
final var testFolderPath = Files.createTempDirectory(targetPath, testFolderName);
final var testFile = Files.createTempFile(testFolderPath, UUID.randomUUID().toString(), ".json");

try (final var out = new FileOutputStream(testFile.toString())) {
processor.toJson(vc, out);
}

try (final var in = new FileInputStream(testFile.toString())) {
vcCopy = processor.fromJson(in, VerifiableCredential.class);
}

assertEquals(vc.context, vcCopy.context);
assertEquals(vc.id, vcCopy.id);
assertEquals(vc.type, vcCopy.type);
assertEquals(vc.issuer, vcCopy.issuer);
assertEquals(vc.issuanceDate, vcCopy.issuanceDate);
assertEquals(vc.expirationDate, vcCopy.expirationDate);
assertEquals(vc.credentialSubject, vcCopy.credentialSubject);
assertEquals(vc.credentialStatus, vcCopy.credentialStatus);
assertEquals(vc.proof, vcCopy.proof);

Files.deleteIfExists(testFile);
Files.deleteIfExists(testFolderPath);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2022 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.inrupt.client.jackson;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.inrupt.client.spi.JsonProcessor;
import com.inrupt.client.spi.ServiceProvider;
import com.inrupt.client.spi.VerifiablePresentation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.UUID;

import org.junit.jupiter.api.Test;

public class JacksonVPTest {

private static final JsonProcessor processor = ServiceProvider.getJsonProcessor();
private static VerifiablePresentation vp;
private static VerifiablePresentation vpCopy;

@Test
void roundtripVP() throws IOException {
try (final var res = JacksonVCTest.class.getResourceAsStream("/verifiablePresentation.json")) {
vp = processor.fromJson(res, VerifiablePresentation.class);
}

final var targetPath = new File("target").toPath();
final var testFolderName = UUID.randomUUID().toString();
final var testFolderPath = Files.createTempDirectory(targetPath, testFolderName);
final var testFile = Files.createTempFile(testFolderPath, UUID.randomUUID().toString(), ".json");

try (final var out = new FileOutputStream(testFile.toString())) {
processor.toJson(vp, out);
}

try (final var in = new FileInputStream(testFile.toString())) {
vpCopy = processor.fromJson(in, VerifiablePresentation.class);
}

assertEquals(vp.context, vpCopy.context);
assertEquals(vp.id, vpCopy.id);
assertEquals(vp.type, vpCopy.type);
assertEquals(vp.holder, vpCopy.holder);
assertEquals(vp.verifiableCredential.size(), vpCopy.verifiableCredential.size());
assertEquals(vp.proof, vpCopy.proof);

Files.deleteIfExists(testFile);
Files.deleteIfExists(testFolderPath);

}
}
Loading