Skip to content

Commit

Permalink
Merge pull request #7 from aureliony/refactor
Browse files Browse the repository at this point in the history
Refactor existing JSON serialization and deserialization code to our current uses.
  • Loading branch information
fivetran-tangyetong authored Feb 27, 2024
2 parents af490e0 + 10d3015 commit dfb1e3d
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 435 deletions.
16 changes: 8 additions & 8 deletions src/main/java/seedu/address/commons/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public class JsonUtil {

private static final Logger logger = LogsCenter.getLogger(JsonUtil.class);

private static ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.registerModule(new SimpleModule("SimpleModule")
.addSerializer(Level.class, new ToStringSerializer())
.addDeserializer(Level.class, new LevelDeserializer(Level.class)));
private static final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.registerModule(new SimpleModule("Logger")
.addSerializer(Level.class, new ToStringSerializer())
.addDeserializer(Level.class, new LevelDeserializer(Level.class)));

static <T> void serializeObjectToJsonFile(Path jsonFile, T objectToSerialize) throws IOException {
FileUtil.writeToFile(jsonFile, toJsonString(objectToSerialize));
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/seedu/address/model/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Represents a Person's address in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)}
Expand All @@ -17,7 +19,7 @@ public class Address {
*/
public static final String VALIDATION_REGEX = "[^\\s].*";

public final String value;
public final String address;

/**
* Constructs an {@code Address}.
Expand All @@ -27,7 +29,7 @@ public class Address {
public Address(String address) {
requireNonNull(address);
checkArgument(isValidAddress(address), MESSAGE_CONSTRAINTS);
value = address;
this.address = address;
}

/**
Expand All @@ -38,8 +40,9 @@ public static boolean isValidAddress(String test) {
}

@Override
@JsonValue
public String toString() {
return value;
return address;
}

@Override
Expand All @@ -54,12 +57,12 @@ public boolean equals(Object other) {
}

Address otherAddress = (Address) other;
return value.equals(otherAddress.value);
return address.equals(otherAddress.address);
}

@Override
public int hashCode() {
return value.hashCode();
return address.hashCode();
}

}
13 changes: 8 additions & 5 deletions src/main/java/seedu/address/model/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Represents a Person's email in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
Expand Down Expand Up @@ -31,7 +33,7 @@ public class Email {
private static final String DOMAIN_REGEX = "(" + DOMAIN_PART_REGEX + "\\.)*" + DOMAIN_LAST_PART_REGEX;
public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@" + DOMAIN_REGEX;

public final String value;
public final String email;

/**
* Constructs an {@code Email}.
Expand All @@ -41,7 +43,7 @@ public class Email {
public Email(String email) {
requireNonNull(email);
checkArgument(isValidEmail(email), MESSAGE_CONSTRAINTS);
value = email;
this.email = email;
}

/**
Expand All @@ -52,8 +54,9 @@ public static boolean isValidEmail(String test) {
}

@Override
@JsonValue
public String toString() {
return value;
return email;
}

@Override
Expand All @@ -68,12 +71,12 @@ public boolean equals(Object other) {
}

Email otherEmail = (Email) other;
return value.equals(otherEmail.value);
return email.equals(otherEmail.email);
}

@Override
public int hashCode() {
return value.hashCode();
return email.hashCode();
}

}
14 changes: 8 additions & 6 deletions src/main/java/seedu/address/model/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
Expand All @@ -18,7 +20,7 @@ public class Name {
*/
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";

public final String fullName;
public final String name;

/**
* Constructs a {@code Name}.
Expand All @@ -28,7 +30,7 @@ public class Name {
public Name(String name) {
requireNonNull(name);
checkArgument(isValidName(name), MESSAGE_CONSTRAINTS);
fullName = name;
this.name = name;
}

/**
Expand All @@ -38,10 +40,10 @@ public static boolean isValidName(String test) {
return test.matches(VALIDATION_REGEX);
}


@Override
@JsonValue
public String toString() {
return fullName;
return name;
}

@Override
Expand All @@ -56,12 +58,12 @@ public boolean equals(Object other) {
}

Name otherName = (Name) other;
return fullName.equals(otherName.fullName);
return name.equals(otherName.name);
}

@Override
public int hashCode() {
return fullName.hashCode();
return name.hashCode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public NameContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Person person) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword));
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().name, keyword));
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.tag.Tag;

Expand Down Expand Up @@ -37,6 +41,21 @@ public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tag
this.tags.addAll(tags);
}

@JsonCreator
private Person(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<String> tagNames) {
this.name = new Name(name);
this.phone = new Phone(phone);
this.email = new Email(email);
this.address = new Address(address);
if (tagNames != null) {
for (String tagName : tagNames) {
this.tags.add(new Tag(tagName));
}
}
}

public Name getName() {
return name;
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/seedu/address/model/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Represents a Person's phone number in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)}
*/
public class Phone {


public static final String MESSAGE_CONSTRAINTS =
"Phone numbers should only contain numbers, and it should be at least 3 digits long";
public static final String VALIDATION_REGEX = "\\d{3,}";
public final String value;
public final String phone;

/**
* Constructs a {@code Phone}.
Expand All @@ -23,7 +24,7 @@ public class Phone {
public Phone(String phone) {
requireNonNull(phone);
checkArgument(isValidPhone(phone), MESSAGE_CONSTRAINTS);
value = phone;
this.phone = phone;
}

/**
Expand All @@ -34,8 +35,9 @@ public static boolean isValidPhone(String test) {
}

@Override
@JsonValue
public String toString() {
return value;
return phone;
}

@Override
Expand All @@ -50,12 +52,12 @@ public boolean equals(Object other) {
}

Phone otherPhone = (Phone) other;
return value.equals(otherPhone.value);
return phone.equals(otherPhone.phone);
}

@Override
public int hashCode() {
return value.hashCode();
return phone.hashCode();
}

}
10 changes: 7 additions & 3 deletions src/main/java/seedu/address/model/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;
import seedu.address.storage.UniquePersonListDeserializer;

/**
* A list of persons that enforces uniqueness between its elements and does not allow nulls.
Expand All @@ -22,11 +26,10 @@
*
* @see Person#isSamePerson(Person)
*/
@JsonDeserialize(using = UniquePersonListDeserializer.class)
public class UniquePersonList implements Iterable<Person> {

private final ObservableList<Person> internalList = FXCollections.observableArrayList();
private final ObservableList<Person> internalUnmodifiableList =
FXCollections.unmodifiableObservableList(internalList);

/**
* Returns true if the list contains an equivalent person as the given argument.
Expand Down Expand Up @@ -100,8 +103,9 @@ public void setPersons(List<Person> persons) {
/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
@JsonValue
public ObservableList<Person> asUnmodifiableObservableList() {
return internalUnmodifiableList;
return FXCollections.unmodifiableObservableList(internalList);
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Represents a Tag in the address book.
* Guarantees: immutable; name is valid as declared in {@link #isValidTagName(String)}
Expand All @@ -25,6 +27,11 @@ public Tag(String tagName) {
this.tagName = tagName;
}

@JsonValue
public String get() {
return tagName;
}

/**
* Returns true if a given string is a valid tag name.
*/
Expand Down
Loading

0 comments on commit dfb1e3d

Please sign in to comment.