Skip to content

Commit

Permalink
Initialize Gson with custom Instance serializer
Browse files Browse the repository at this point in the history
Since Instant is immutable in Java 17, we need to implement serialize and de-serialize methods to convert Instant object into gson.
  • Loading branch information
zhx828 authored and jfkonecn committed Sep 20, 2024
1 parent f14a5eb commit 9196fc2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.mskcc.oncokb.curation.domain.enumeration.ReferenceGenome;
import org.mskcc.oncokb.curation.util.GsonUtils;
import org.mskcc.oncokb.curation.vm.ensembl.EnsemblArchiveId;
import org.mskcc.oncokb.curation.vm.ensembl.EnsemblSequence;
import org.mskcc.oncokb.curation.vm.ensembl.EnsemblTranscript;
Expand Down Expand Up @@ -162,7 +163,7 @@ public List<org.mskcc.oncokb.curation.vm.ensembl.EnsemblArchiveId> getArchiveIds

RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.postForObject(getArchiveIdPOSTUrl(referenceGenome), entity, String.class);
Gson gson = new Gson();
Gson gson = GsonUtils.create();
Type type = new TypeToken<Map<String, EnsemblArchiveId>>() {}.getType();
Map<String, org.mskcc.oncokb.curation.vm.ensembl.EnsemblArchiveId> archiveMap = gson.fromJson(response, type);
return archiveMap.values().stream().filter(val -> val != null).collect(Collectors.toList());
Expand Down Expand Up @@ -207,9 +208,8 @@ public List<org.mskcc.oncokb.curation.vm.ensembl.EnsemblTranscript> getIds(
try {
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.postForObject(getLookupPOSTUrl(referenceGenome, includeUtr, expand), entity, String.class);
Gson gson = new Gson();
Type type = new TypeToken<Map<String, EnsemblTranscript>>() {}.getType();
Map<String, org.mskcc.oncokb.curation.vm.ensembl.EnsemblTranscript> transcriptMap = gson.fromJson(response, type);
Map<String, org.mskcc.oncokb.curation.vm.ensembl.EnsemblTranscript> transcriptMap = GsonUtils.create().fromJson(response, type);
return transcriptMap.values().stream().filter(val -> val != null).collect(Collectors.toList());
} catch (Exception e) {
log.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.mskcc.oncokb.curation.service.dto.pubmed;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.gson.Gson;
import java.io.Serializable;
import java.time.Instant;
import java.util.HashSet;
Expand All @@ -10,6 +9,7 @@
import org.mskcc.oncokb.curation.domain.Flag;
import org.mskcc.oncokb.curation.domain.Synonym;
import org.mskcc.oncokb.curation.domain.enumeration.ArticleType;
import org.mskcc.oncokb.curation.util.GsonUtils;

public class PubMedDTO implements Serializable {

Expand Down Expand Up @@ -136,7 +136,7 @@ public PubMedDTO(Article article) {
this.link = article.getLink();
this.authors = article.getAuthors();
this.date = article.getDate();
this.additionalInfo = new Gson().fromJson(article.getAdditionalInfo(), AdditionalInfoDTO.class);
this.additionalInfo = GsonUtils.create().fromJson(article.getAdditionalInfo(), AdditionalInfoDTO.class);
this.flags = article.getFlags();
this.synonyms = article.getSynonyms();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.mskcc.oncokb.curation.service.mapper;

import com.google.gson.Gson;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.mskcc.oncokb.curation.domain.Article;
import org.mskcc.oncokb.curation.service.dto.pubmed.PubMedDTO;
import org.mskcc.oncokb.curation.util.GsonUtils;
import org.springframework.stereotype.Service;

/**
Expand Down Expand Up @@ -42,7 +42,7 @@ public Article pubMedDTOToArticle(PubMedDTO pubMedDTO) {
article.setLink(pubMedDTO.getLink());
article.setDate(pubMedDTO.getDate());
article.setAuthors(pubMedDTO.getAuthors());
article.setAdditionalInfo(new Gson().toJson(pubMedDTO.getAdditionalInfo()));
article.setAdditionalInfo(GsonUtils.create().toJson(pubMedDTO.getAdditionalInfo()));
article.setFlags(pubMedDTO.getFlags());
article.setSynonyms(pubMedDTO.getSynonyms());
return article;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/mskcc/oncokb/curation/util/GsonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mskcc.oncokb.curation.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.time.Instant;
import org.mskcc.oncokb.curation.config.InstantTypeAdapter;

public class GsonUtils {

public static Gson create() {
// https://medium.com/@pratiktikarye/instant-to-gson-conversion-in-java-17-2673bb59fbea
return new GsonBuilder().registerTypeAdapter(Instant.class, new InstantTypeAdapter()).create();
}
}

0 comments on commit 9196fc2

Please sign in to comment.