Skip to content

Commit

Permalink
feat: serialize request denoising
Browse files Browse the repository at this point in the history
  • Loading branch information
YongwuHe committed Jan 8, 2024
1 parent 28d4fd9 commit 45b8f19
Show file tree
Hide file tree
Showing 50 changed files with 2,539 additions and 796 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -550,4 +550,17 @@ public static Integer getFirstNumeric(String s) {
public static boolean isNullWord(String str) {
return equals(str, "null") || equals(str, "NULL");
}

public static int countMatches(final String str, final String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = CharSequenceUtils.indexOf(str, sub, idx)) != INDEX_NOT_FOUND) {
count++;
idx += sub.length();
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ private ArexConstants() {}
public static final String REPLAY_ORIGINAL_MOCKER = "arex-replay-original-mocker";
public static final String AREX_EXTENSION_ATTRIBUTE = "arex-extension-attribute";
public static final String GSON_SERIALIZER = "gson";
public static final String GSON_REQUEST_SERIALIZER = "gson-request";
public static final String JACKSON_SERIALIZER = "jackson";
public static final String JACKSON_REQUEST_SERIALIZER = "jackson-request";
public static final String CONFIG_DEPENDENCY = "arex_replay_prepare_dependency";
public static final String PREFIX = "arex-";
public static final String CONFIG_VERSION = "configBatchNo";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ public static String serialize(Object object, String serializer) {
}
}

// public static String serializeRequest(Object object) {
// return serializeRequest(object, ArexConstants.JACKSON_REQUEST_SERIALIZER);
// }
//
// public static String serializeRequest(Object object, String serializer) {
// try {
// return serializeWithException(object, serializer);
// } catch (Throwable ex) {
// LogManager.warn("serializeRequest", StringUtil.format("can not serialize object: %s, cause: %s", TypeUtil.errorSerializeToString(object), ex.toString()));
// return null;
// }
// }

/**
* Deserialize by Class
*
Expand Down
10 changes: 10 additions & 0 deletions arex-instrumentation-foundation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@
<version>2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.23</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.arex.foundation.serializer;

import com.google.auto.service.AutoService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.arex.foundation.serializer.custom.ProtobufAdapterFactory;
import io.arex.foundation.serializer.custom.SerializerExclusionStrategy;
import io.arex.foundation.serializer.custom.StringAdapter;
import io.arex.foundation.serializer.custom.time.adapter.CalendarAdapter;
import io.arex.foundation.serializer.custom.time.adapter.ClassAdapter;
import io.arex.foundation.serializer.custom.time.adapter.DateAdapter;
import io.arex.foundation.serializer.custom.time.adapter.DateTimeAdapter;
import io.arex.foundation.serializer.custom.time.adapter.InstantAdapter;
import io.arex.foundation.serializer.custom.time.adapter.JodaLocalDateAdapter;
import io.arex.foundation.serializer.custom.time.adapter.JodaLocalDateTimeAdapter;
import io.arex.foundation.serializer.custom.time.adapter.JodaLocalTimeAdapter;
import io.arex.foundation.serializer.custom.time.adapter.LocalDateAdapter;
import io.arex.foundation.serializer.custom.time.adapter.LocalDateTimeAdapter;
import io.arex.foundation.serializer.custom.time.adapter.LocalTimeAdapter;
import io.arex.foundation.serializer.custom.time.adapter.OffsetDateTimeAdapter;
import io.arex.foundation.serializer.custom.time.adapter.TimeZoneAdapter;
import io.arex.foundation.serializer.custom.time.adapter.XMLGregorianCalendarAdapter;
import io.arex.inst.runtime.serializer.StringSerializable;
import org.joda.time.DateTime;

import javax.xml.datatype.XMLGregorianCalendar;
import java.lang.reflect.Type;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

@AutoService(StringSerializable.class)
public class GsonRequestSerializer implements StringSerializable{

public static GsonRequestSerializer INSTANCE = new GsonRequestSerializer();
private final Gson serializer;

public GsonRequestSerializer() {
this.serializer = new GsonBuilder()
.registerTypeAdapter(DateTime.class, new DateTimeAdapter.GsonSerializer(true))
.registerTypeAdapter(org.joda.time.LocalDateTime.class, new JodaLocalDateTimeAdapter.GsonSerializer(true))
.registerTypeAdapter(org.joda.time.LocalDate.class, new JodaLocalDateAdapter.GsonSerializer())
.registerTypeAdapter(org.joda.time.LocalTime.class, new JodaLocalTimeAdapter.GsonSerializer(true))
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter.GsonSerializer(true))
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter.GsonSerializer())
.registerTypeAdapter(LocalTime.class, new LocalTimeAdapter.GsonSerializer(true))
.registerTypeAdapter(Calendar.class, new CalendarAdapter.GsonSerializer(true))
.registerTypeAdapter(GregorianCalendar.class, new CalendarAdapter.GsonSerializer(true))
.registerTypeAdapter(XMLGregorianCalendar.class, new XMLGregorianCalendarAdapter.GsonSerializer(true))
.registerTypeAdapter(Timestamp.class, new DateAdapter.GsonSerializer(true))
.registerTypeAdapter(Date.class, new DateAdapter.GsonSerializer(true))
.registerTypeAdapter(java.sql.Date.class, new DateAdapter.GsonSerializer(true))
.registerTypeAdapter(Time.class, new DateAdapter.GsonSerializer(true))
.registerTypeAdapter(Instant.class, new InstantAdapter.GsonSerializer(true))
.registerTypeAdapter(Class.class, new ClassAdapter.GsonSerializer())
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeAdapter.GsonSerializer(true))
.registerTypeAdapter(TimeZone.class, new TimeZoneAdapter.GsonSerializer())
.registerTypeAdapter(String.class, new StringAdapter.GsonSerializer())
.registerTypeAdapterFactory(new ProtobufAdapterFactory())
.enableComplexMapKeySerialization()
.setExclusionStrategies(new SerializerExclusionStrategy.GsonExclusion())
.disableHtmlEscaping()
.create();
}

@Override
public String name() {
return "gson-request";
}

@Override
public String serialize(Object object) throws Throwable {
if (object == null) {
return null;
}
return serializer.toJson(object);
}

@Override
public <T> T deserialize(String value, Class<T> clazz) throws Throwable {
// request serializer not need deserialize
return null;
}

@Override
public <T> T deserialize(String value, Type type) throws Throwable {
// request serializer not need deserialize
return null;
}

@Override
public StringSerializable reCreateSerializer() {
INSTANCE = new GsonRequestSerializer();
return INSTANCE;
}
}
Loading

0 comments on commit 45b8f19

Please sign in to comment.