-
Notifications
You must be signed in to change notification settings - Fork 39
feat: Optional Jackson serialization for 5x performance improvement on large batches #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jaredmixpanel
merged 6 commits into
master
from
feature/jackson-performance-optimization
Nov 24, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc5dfdd
feat: Add optional Jackson serialization for 5x performance improvement
jaredmixpanel f3585cb
Add exception logging to Jackson serialization fallback (#50)
Copilot 6db4103
Use StandardCharsets.UTF_8 in JacksonSerializer (#49)
Copilot 3aa83f7
Update src/test/java/com/mixpanel/mixpanelapi/internal/SerializerBenc…
jaredmixpanel 8914264
chore: Update Jackson version to 2.20.0
jaredmixpanel 233f665
Update src/main/java/com/mixpanel/mixpanelapi/MixpanelAPI.java
jaredmixpanel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| .classpath | ||
| .metadata | ||
| target/ | ||
| .vscode/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
src/main/java/com/mixpanel/mixpanelapi/internal/JacksonSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package com.mixpanel.mixpanelapi.internal; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.StringWriter; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * High-performance JSON serialization implementation using Jackson's streaming API. | ||
| * This implementation provides significant performance improvements for large batches | ||
| * while maintaining compatibility with org.json JSONObjects. | ||
| * | ||
| * @since 1.6.0 | ||
| */ | ||
| public class JacksonSerializer implements JsonSerializer { | ||
|
|
||
| private final JsonFactory jsonFactory; | ||
|
|
||
| public JacksonSerializer() { | ||
| this.jsonFactory = new JsonFactory(); | ||
| } | ||
|
|
||
| @Override | ||
| public String serializeArray(List<JSONObject> messages) throws IOException { | ||
| if (messages == null || messages.isEmpty()) { | ||
| return "[]"; | ||
| } | ||
|
|
||
| StringWriter writer = new StringWriter(); | ||
| try (JsonGenerator generator = jsonFactory.createGenerator(writer)) { | ||
| writeJsonArray(generator, messages); | ||
| } | ||
| return writer.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] serializeArrayToBytes(List<JSONObject> messages) throws IOException { | ||
| if (messages == null || messages.isEmpty()) { | ||
| return "[]".getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
| try (JsonGenerator generator = jsonFactory.createGenerator(outputStream)) { | ||
| writeJsonArray(generator, messages); | ||
| } | ||
| return outputStream.toByteArray(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getImplementationName() { | ||
| return "Jackson"; | ||
| } | ||
|
|
||
| /** | ||
| * Writes a JSON array of messages using the Jackson streaming API. | ||
| */ | ||
| private void writeJsonArray(JsonGenerator generator, List<JSONObject> messages) throws IOException { | ||
| generator.writeStartArray(); | ||
| for (JSONObject message : messages) { | ||
| writeJsonObject(generator, message); | ||
| } | ||
| generator.writeEndArray(); | ||
| } | ||
|
|
||
| /** | ||
| * Recursively writes a JSONObject using Jackson's streaming API. | ||
| * This avoids the conversion overhead while leveraging Jackson's performance. | ||
| */ | ||
| private void writeJsonObject(JsonGenerator generator, JSONObject jsonObject) throws IOException { | ||
| generator.writeStartObject(); | ||
|
|
||
| Iterator<String> keys = jsonObject.keys(); | ||
| while (keys.hasNext()) { | ||
| String key = keys.next(); | ||
| Object value = jsonObject.opt(key); | ||
|
|
||
| if (value == null || value == JSONObject.NULL) { | ||
| generator.writeNullField(key); | ||
| } else if (value instanceof String) { | ||
| generator.writeStringField(key, (String) value); | ||
| } else if (value instanceof Number) { | ||
| if (value instanceof Integer) { | ||
| generator.writeNumberField(key, (Integer) value); | ||
| } else if (value instanceof Long) { | ||
| generator.writeNumberField(key, (Long) value); | ||
| } else if (value instanceof Double) { | ||
| generator.writeNumberField(key, (Double) value); | ||
| } else if (value instanceof Float) { | ||
| generator.writeNumberField(key, (Float) value); | ||
| } else { | ||
| // Handle other Number types | ||
| generator.writeNumberField(key, ((Number) value).doubleValue()); | ||
| } | ||
| } else if (value instanceof Boolean) { | ||
| generator.writeBooleanField(key, (Boolean) value); | ||
| } else if (value instanceof JSONObject) { | ||
| generator.writeFieldName(key); | ||
| writeJsonObject(generator, (JSONObject) value); | ||
| } else if (value instanceof JSONArray) { | ||
| generator.writeFieldName(key); | ||
| writeJsonArray(generator, (JSONArray) value); | ||
| } else { | ||
| // For any other type, use toString() | ||
| generator.writeStringField(key, value.toString()); | ||
| } | ||
| } | ||
|
|
||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| /** | ||
| * Recursively writes a JSONArray using Jackson's streaming API. | ||
| */ | ||
| private void writeJsonArray(JsonGenerator generator, JSONArray jsonArray) throws IOException { | ||
| generator.writeStartArray(); | ||
|
|
||
| for (int i = 0; i < jsonArray.length(); i++) { | ||
| Object value = jsonArray.opt(i); | ||
|
|
||
| if (value == null || value == JSONObject.NULL) { | ||
| generator.writeNull(); | ||
| } else if (value instanceof String) { | ||
| generator.writeString((String) value); | ||
| } else if (value instanceof Number) { | ||
| if (value instanceof Integer) { | ||
| generator.writeNumber((Integer) value); | ||
| } else if (value instanceof Long) { | ||
| generator.writeNumber((Long) value); | ||
| } else if (value instanceof Double) { | ||
| generator.writeNumber((Double) value); | ||
| } else if (value instanceof Float) { | ||
| generator.writeNumber((Float) value); | ||
| } else { | ||
| generator.writeNumber(((Number) value).doubleValue()); | ||
| } | ||
| } else if (value instanceof Boolean) { | ||
| generator.writeBoolean((Boolean) value); | ||
| } else if (value instanceof JSONObject) { | ||
| writeJsonObject(generator, (JSONObject) value); | ||
| } else if (value instanceof JSONArray) { | ||
| writeJsonArray(generator, (JSONArray) value); | ||
| } else { | ||
| generator.writeString(value.toString()); | ||
| } | ||
| } | ||
|
|
||
| generator.writeEndArray(); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/mixpanel/mixpanelapi/internal/JsonSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.mixpanel.mixpanelapi.internal; | ||
|
|
||
| import org.json.JSONObject; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Internal interface for JSON serialization. | ||
| * Provides methods to serialize lists of JSONObjects to various formats. | ||
| * This allows for different implementations (org.json, Jackson) to be used | ||
| * based on performance requirements and available dependencies. | ||
| * | ||
| * @since 1.6.0 | ||
| */ | ||
| public interface JsonSerializer { | ||
|
|
||
| /** | ||
| * Serializes a list of JSONObjects to a JSON array string. | ||
| * | ||
| * @param messages The list of JSONObjects to serialize | ||
| * @return A JSON array string representation | ||
| * @throws IOException if serialization fails | ||
| */ | ||
| String serializeArray(List<JSONObject> messages) throws IOException; | ||
|
|
||
| /** | ||
| * Serializes a list of JSONObjects directly to UTF-8 encoded bytes. | ||
| * This method can be more efficient for large payloads as it avoids | ||
| * the intermediate String creation. | ||
| * | ||
| * @param messages The list of JSONObjects to serialize | ||
| * @return UTF-8 encoded bytes of the JSON array | ||
| * @throws IOException if serialization fails | ||
| */ | ||
| byte[] serializeArrayToBytes(List<JSONObject> messages) throws IOException; | ||
|
|
||
| /** | ||
| * Returns the name of this serializer implementation. | ||
| * Useful for logging and debugging purposes. | ||
| * | ||
| * @return The implementation name (e.g., "org.json", "Jackson") | ||
| */ | ||
| String getImplementationName(); | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/mixpanel/mixpanelapi/internal/OrgJsonSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.mixpanel.mixpanelapi.internal; | ||
|
|
||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * JSON serialization implementation using org.json library. | ||
| * This is the default implementation that maintains backward compatibility. | ||
| * | ||
| * @since 1.6.0 | ||
| */ | ||
| public class OrgJsonSerializer implements JsonSerializer { | ||
|
|
||
| @Override | ||
| public String serializeArray(List<JSONObject> messages) throws IOException { | ||
| if (messages == null || messages.isEmpty()) { | ||
| return "[]"; | ||
| } | ||
|
|
||
| JSONArray array = new JSONArray(); | ||
| for (JSONObject message : messages) { | ||
| array.put(message); | ||
| } | ||
| return array.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] serializeArrayToBytes(List<JSONObject> messages) throws IOException { | ||
| return serializeArray(messages).getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| @Override | ||
| public String getImplementationName() { | ||
| return "org.json"; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.