Skip to content

Commit

Permalink
feat: update Storage files
Browse files Browse the repository at this point in the history
Update Json Classes to accept new format.
Update data json files to account for new format.
Create OptionalSerializer to force null values to be formatted properly
  • Loading branch information
jovantanyk committed Mar 23, 2024
1 parent 19c1404 commit db359dd
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 85 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/commons/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class JsonUtil {
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.registerModule(new SimpleModule("SimpleModule")
.addSerializer(Level.class, new ToStringSerializer())
.addDeserializer(Level.class, new LevelDeserializer(Level.class)));
.addDeserializer(Level.class, new LevelDeserializer(Level.class))
.addSerializer(new OptionalSerializer()));

static <T> void serializeObjectToJsonFile(Path jsonFile, T objectToSerialize) throws IOException {
FileUtil.writeToFile(jsonFile, toJsonString(objectToSerialize));
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/seedu/address/commons/util/OptionalSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.commons.util;

import java.io.IOException;
import java.util.Optional;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
* A Serializer for handling Optional values and converting them into
* proper JSON strings with correct null formatting.
*/
public class OptionalSerializer extends JsonSerializer<Optional<?>> {
@Override
public void serialize(Optional<?> optional, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
if (optional.isPresent()) {
jsonGenerator.writeObject(optional.get());
} else {
jsonGenerator.writeNull();
}
}
/**
* Avoids directly specifying Optional.class with generics
*/
public Class<Optional<?>> handledType() {
return (Class<Optional<?>>) (Class<?>) Optional.class;
}
}
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
public class SampleDataUtil {
public static Person[] getSamplePersons() {
return new Person[] {
new Person(new Nric("A1234567B"), new Name("Alex Yeoh"), new Phone("87438807"),
new Person(new Nric("T0234567C"), new Name("Alex Yeoh"), new Phone("87438807"),
new Address("Blk 30 Geylang Street 29, #06-40"), new DateOfBirth("1977-04-03"),
new Sex("M"), new Status("HEALTHY")),
new Person(new Nric("A1234568B"), new Name("Bernice Yu"), new Phone("99272758"),
new Person(new Nric("S9234568N"), new Name("Bernice Yu"), new Phone("99272758"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"), new DateOfBirth("1977-04-03"),
new Sex("F"), new Status("UNWELL")),
new Person(new Nric("A1234569B"), new Name("Charlotte Oliveiro"), new Phone("93210283"),
new Person(new Nric("S8934569Z"), new Name("Charlotte Oliveiro"), new Phone("93210283"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"), new DateOfBirth("2001-04-03"),
new Sex("F"), new Status("HEALTHY")),
new Person(new Nric("A1234560B"), new Name("David Li"), new Phone("91031282"),
new Person(new Nric("T0134560A"), new Name("David Li"), new Phone("91031282"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"), new DateOfBirth("1987-11-03"),
new Sex("M"), new Status("PENDING")),
new Person(new Nric("A1234561B"), new Name("Irfan Ibrahim"), new Phone("92492021"),
new Person(new Nric("T0534161B"), new Name("Irfan Ibrahim"), new Phone("92492021"),
new Address("Blk 47 Tampines Street 20, #17-35"), new DateOfBirth("1970-12-03"),
new Sex("M"), new Status("UNWELL")),
new Person(new Nric("A1234562B"), new Name("Roy Balakrishnan"), new Phone("92624417"),
new Person(new Nric("S9781662B"), new Name("Roy Balakrishnan"), new Phone("92624417"),
new Address("Blk 45 Aljunied Street 85, #11-31"), new DateOfBirth("1987-04-03"),
new Sex("M"), new Status("PENDING"))
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
{
"persons": [ {
"name": "Valid Person",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
}, {
"name": "Person With Invalid Phone Field",
"phone": "948asdf2424",
"email": "[email protected]",
"address": "4th street"
} ]
"persons": [
{
"nric": "T0139571B",
"name": "Valid Person",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0139571B",
"name": "Invalid Person Phone Wrong",
"phone": "94eeqa!1253a",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
{
"persons": [ {
"name": "Person with invalid name field: Ha!ns Mu@ster",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
} ]
"persons": [
{
"nric": "INVALID NRIC!!!!",
"name": "Alice Pauline",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
{
"persons": [ {
"name": "Alice Pauline",
"phone": "94351253",
"email": "[email protected]",
"address": "123, Jurong West Ave 6, #08-111",
"tags": [ "friends" ]
}, {
"name": "Alice Pauline",
"phone": "94351253",
"email": "[email protected]",
"address": "4th street"
} ]
"persons": [
{
"nric": "T0139571B",
"name": "Alice Pauline",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0139571B",
"name": "Alice Pauline",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{
"persons": [ {
"name": "Hans Muster",
"phone": "9482424",
"email": "invalid@email!3e",
"address": "4th street"
} ]
"persons": [
{
"name": "Hans Muster",
"phone": "94820a",
"address": "4th street",
"dateOfBirth": "2001-01-01",
"sex": "M",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,124 @@
{
"_comment": "AddressBook save file which contains the same Person values as in TypicalPersons#getTypicalAddressBook()",
"persons" : [ {
"name" : "Alice Pauline",
"phone" : "94351253",
"email" : "[email protected]",
"address" : "123, Jurong West Ave 6, #08-111",
"tags" : [ "friends" ]
}, {
"name" : "Benson Meier",
"phone" : "98765432",
"email" : "[email protected]",
"address" : "311, Clementi Ave 2, #02-25",
"tags" : [ "owesMoney", "friends" ]
}, {
"name" : "Carl Kurz",
"phone" : "95352563",
"email" : "[email protected]",
"address" : "wall street",
"tags" : [ ]
}, {
"name" : "Daniel Meier",
"phone" : "87652533",
"email" : "[email protected]",
"address" : "10th street",
"tags" : [ "friends" ]
}, {
"name" : "Elle Meyer",
"phone" : "9482224",
"email" : "[email protected]",
"address" : "michegan ave",
"tags" : [ ]
}, {
"name" : "Fiona Kunz",
"phone" : "9482427",
"email" : "[email protected]",
"address" : "little tokyo",
"tags" : [ ]
}, {
"name" : "George Best",
"phone" : "9482442",
"email" : "[email protected]",
"address" : "4th street",
"tags" : [ ]
} ]
"persons": [
{
"nric": "T0139571B",
"name": "Alice Pauline",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0439571C",
"name": "Benson Meier",
"phone": "98765432",
"address": "311, Clementi Ave 2, #02-25",
"dateOfBirth": "2001-01-01",
"sex": "M",
"status": "UNWELL",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0284994B",
"name": "Carl Kurz",
"phone": "95352563",
"address": "wall street",
"dateOfBirth": "2001-01-01",
"sex": "M",
"status": "PENDING",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "S9839571A",
"name": "Daniel Meier",
"phone": "87652533",
"address": "10th street",
"dateOfBirth": "2001-01-01",
"sex": "M",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "S8913957B",
"name": "Elle Meyer",
"phone": "94821224",
"address": "michegan ave",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "UNWELL",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0536171Z",
"name": "Fiona Kunz",
"phone": "94820427",
"address": "little tokyo",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "PENDING",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0829102Z",
"name": "George Best",
"phone": "94824421",
"address": "4th street",
"dateOfBirth": "2001-01-01",
"sex": "M",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}

0 comments on commit db359dd

Please sign in to comment.