Skip to content

Commit

Permalink
Factored out InstantConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
leeN committed Mar 14, 2024
1 parent b9e09e7 commit 5175dab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import com.owlike.genson.reflect.VisibilityFilter;
import com.sap.fontus.sql.tainter.QueryParameters;
import com.sap.fontus.utils.Pair;
import com.sap.fontus.utils.InstantConverter;


import java.time.Instant;
Expand All @@ -28,7 +27,7 @@ public void setPurposes(List<Purpose> purposes) {
public void setCreated(long created) {
this.created = created;
}
private static final Genson genson = new GensonBuilder().useFields(true, VisibilityFilter.PRIVATE).create();
private static final Genson genson = new GensonBuilder().withConverters(new InstantConverter()).useFields(true, VisibilityFilter.PRIVATE).create();
private List<Purpose> purposes;
private long created;

Expand Down
42 changes: 3 additions & 39 deletions fontus/src/main/java/com/sap/fontus/sql/driver/Utils.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package com.sap.fontus.sql.driver;

import com.owlike.genson.Context;
import com.owlike.genson.Converter;
import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import com.owlike.genson.reflect.VisibilityFilter;
import com.owlike.genson.stream.ObjectReader;
import com.owlike.genson.stream.ObjectWriter;
import com.sap.fontus.taintaware.shared.IASTaintRanges;
import com.sap.fontus.taintaware.unified.IASString;
import com.sap.fontus.taintaware.unified.IASTaintInformationable;
import com.sap.fontus.taintaware.unified.TaintInformationFactory;

import java.time.Instant;
import com.sap.fontus.utils.InstantConverter;

public final class Utils {
private static final Genson serializer = new GensonBuilder()
.withConverters(new Utils.InstantConverter())
.withConverters(new InstantConverter())
.useClassMetadata(true)
.useRuntimeType(true)
.useFields(true, VisibilityFilter.PRIVATE)
.useMethods(false)
.create();
private static final Genson deserializer =new GensonBuilder()
.withConverters(new Utils.InstantConverter())
.withConverters(new InstantConverter())
.useClassMetadata(true)
.useRuntimeType(true)
.useFields(true, VisibilityFilter.PRIVATE)
Expand All @@ -47,35 +42,4 @@ public static IASTaintInformationable parseTaint(String json) {
return ranges != null ? TaintInformationFactory.createTaintInformation(ranges.getLength(), ranges.getTaintRanges()) : null;
}

public static class InstantConverter implements Converter<Instant> {

@Override
public void serialize(Instant object, ObjectWriter writer, Context ctx) throws Exception {
long epochSecond = object.getEpochSecond();
int nano = object.getNano();
writer.beginObject();
writer.writeNumber("seconds", epochSecond);
writer.writeNumber("nanos", nano);
writer.endObject();
}

@Override
public Instant deserialize(ObjectReader reader, Context ctx) throws Exception {
reader.beginObject();
long epochSecond = -1L;
int nano = -1;
while(reader.hasNext()) {
reader.next();
if("seconds".equals(reader.name())) {
epochSecond = reader.valueAsLong();
} else if ("nanos".equals(reader.name())) {
nano = reader.valueAsInt();
} else {
throw new IllegalStateException(String.format("Unknown field name: %s", reader.name()));
}
}
reader.endObject();
return Instant.ofEpochSecond(epochSecond, nano);
}
}
}
40 changes: 40 additions & 0 deletions fontus/src/main/java/com/sap/fontus/utils/InstantConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.sap.fontus.utils;

import com.owlike.genson.Context;
import com.owlike.genson.Converter;
import com.owlike.genson.stream.ObjectReader;
import com.owlike.genson.stream.ObjectWriter;

import java.time.Instant;

public class InstantConverter implements Converter<Instant> {

@Override
public void serialize(Instant object, ObjectWriter writer, Context ctx) throws Exception {
long epochSecond = object.getEpochSecond();
int nano = object.getNano();
writer.beginObject();
writer.writeNumber("seconds", epochSecond);
writer.writeNumber("nanos", nano);
writer.endObject();
}

@Override
public Instant deserialize(ObjectReader reader, Context ctx) throws Exception {
reader.beginObject();
long epochSecond = -1L;
int nano = -1;
while (reader.hasNext()) {
reader.next();
if ("seconds".equals(reader.name())) {
epochSecond = reader.valueAsLong();
} else if ("nanos".equals(reader.name())) {
nano = reader.valueAsInt();
} else {
throw new IllegalStateException(String.format("Unknown field name: %s", reader.name()));
}
}
reader.endObject();
return Instant.ofEpochSecond(epochSecond, nano);
}
}

0 comments on commit 5175dab

Please sign in to comment.