-
Notifications
You must be signed in to change notification settings - Fork 318
Add JSON component #7973
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
Merged
Add JSON component #7973
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ff855b1
feat(json): Add JSON component
PerfectSlayer 68ceae1
feat(internal-api): Remove Strings toJson and escapeToJson methods
PerfectSlayer bb01c7d
feat(ci-visibility): Migrate to JSON component
PerfectSlayer d5d3c44
feat(junit-4.10): Migrate to JSON component
PerfectSlayer 6907d11
feat(junit-5.3): Migrate to JSON component
PerfectSlayer 386c690
feat(karate): Migrate to JSON component
PerfectSlayer b9230b5
feat(testng): Migrate to JSON component
PerfectSlayer 1776ab7
feat(bootstrap): Migrate to JSON component
PerfectSlayer 7c3d6e2
feat(json): Improve structure performance
PerfectSlayer 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| plugins { | ||
| id("me.champeau.jmh") | ||
| } | ||
|
|
||
| apply(from = "$rootDir/gradle/java.gradle") | ||
|
|
||
| jmh { | ||
| version = "1.28" | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
components/json/src/jmh/java/datadog/json/JsonWriterBenchmark.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,106 @@ | ||
| package datadog.json; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.MICROSECONDS; | ||
| import static org.openjdk.jmh.annotations.Mode.AverageTime; | ||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| @BenchmarkMode(AverageTime) | ||
| @OutputTimeUnit(MICROSECONDS) | ||
| @Fork(value = 1) | ||
| @SuppressWarnings("unused") | ||
| public class JsonWriterBenchmark { | ||
| @Benchmark | ||
| public void writeSimpleArray(Blackhole blackhole) { | ||
| try (JsonWriter writer = new JsonWriter()) { | ||
| writer | ||
| .beginArray() | ||
| .beginObject() | ||
| .name("true") | ||
| .value(true) | ||
| .endObject() | ||
| .beginObject() | ||
| .name("false") | ||
| .value(false) | ||
| .endObject() | ||
| .endArray(); | ||
| blackhole.consume(writer.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void writeComplexArray(Blackhole blackhole) { | ||
| try (JsonWriter writer = new JsonWriter()) { | ||
| writer | ||
| .beginArray() | ||
| .value("first level") | ||
| .beginArray() | ||
| .value("second level") | ||
| .beginArray() | ||
| .value("third level") | ||
| .beginObject() | ||
| .name("key") | ||
| .value("value") | ||
| .endObject() | ||
| .beginObject() | ||
| .name("key") | ||
| .value("value") | ||
| .endObject() | ||
| .endArray() // second level | ||
| .beginObject() | ||
| .name("key") | ||
| .value("value") | ||
| .endObject() | ||
| .endArray() // first level | ||
| .beginObject() | ||
| .name("key") | ||
| .value("value") | ||
| .endObject() | ||
| .value("last value") | ||
| .endArray(); | ||
| blackhole.consume(writer.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void writeComplexObject(Blackhole blackhole) { | ||
| try (JsonWriter writer = new JsonWriter()) { | ||
| writer | ||
| .beginObject() | ||
| .name("attrs") | ||
| .beginObject() | ||
| .name("attr1") | ||
| .value("value1") | ||
| .name("attr2") | ||
| .value("value2") | ||
| .endObject() | ||
| .name("data") | ||
| .beginArray() | ||
| .beginObject() | ||
| .name("x") | ||
| .value(1) | ||
| .name("y") | ||
| .value(12.3) | ||
| .endObject() | ||
| .beginObject() | ||
| .name("x") | ||
| .value(2) | ||
| .name("y") | ||
| .value(4.56) | ||
| .endObject() | ||
| .beginObject() | ||
| .name("x") | ||
| .value(3) | ||
| .name("y") | ||
| .value(789) | ||
| .endObject() | ||
| .endArray() | ||
| .endObject(); | ||
| blackhole.consume(writer.toString()); | ||
| } | ||
| } | ||
| } |
106 changes: 106 additions & 0 deletions
106
components/json/src/main/java/datadog/json/JsonMapper.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,106 @@ | ||
| package datadog.json; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Map; | ||
|
|
||
| /** Utility class for simple Java structure mapping into JSON strings. */ | ||
| public final class JsonMapper { | ||
|
|
||
| private JsonMapper() {} | ||
|
|
||
| /** | ||
| * Converts a {@link String} to a JSON string. | ||
| * | ||
| * @param string The string to convert. | ||
| * @return The converted JSON string. | ||
| */ | ||
| public static String toJson(String string) { | ||
| if (string == null || string.isEmpty()) { | ||
| return ""; | ||
| } | ||
| try (JsonWriter writer = new JsonWriter()) { | ||
| writer.value(string); | ||
| return writer.toString(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a {@link Map} to a JSON object. | ||
| * | ||
| * @param map The map to convert. | ||
| * @return The converted JSON object as Java string. | ||
| */ | ||
| public static String toJson(Map<String, ?> map) { | ||
| if (map == null || map.isEmpty()) { | ||
| return "{}"; | ||
| } | ||
| try (JsonWriter writer = new JsonWriter()) { | ||
| writer.beginObject(); | ||
| for (Map.Entry<String, ?> entry : map.entrySet()) { | ||
| writer.name(entry.getKey()); | ||
| Object value = entry.getValue(); | ||
| if (value == null) { | ||
| writer.nullValue(); | ||
| } else if (value instanceof String) { | ||
| writer.value((String) value); | ||
| } else if (value instanceof Double) { | ||
| writer.value((Double) value); | ||
| } else if (value instanceof Float) { | ||
| writer.value((Float) value); | ||
| } else if (value instanceof Long) { | ||
| writer.value((Long) value); | ||
| } else if (value instanceof Integer) { | ||
| writer.value((Integer) value); | ||
| } else if (value instanceof Boolean) { | ||
| writer.value((Boolean) value); | ||
| } else { | ||
| writer.value(value.toString()); | ||
| } | ||
| } | ||
| writer.endObject(); | ||
| return writer.toString(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a {@link Iterable<String>} to a JSON array. | ||
| * | ||
| * @param items The iterable to convert. | ||
| * @return The converted JSON array as Java string. | ||
| */ | ||
| @SuppressWarnings("DuplicatedCode") | ||
| public static String toJson(Collection<String> items) { | ||
| if (items == null || items.isEmpty()) { | ||
| return "[]"; | ||
| } | ||
| try (JsonWriter writer = new JsonWriter()) { | ||
| writer.beginArray(); | ||
| for (String item : items) { | ||
| writer.value(item); | ||
| } | ||
| writer.endArray(); | ||
| return writer.toString(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a String array to a JSON array. | ||
| * | ||
| * @param items The array to convert. | ||
| * @return The converted JSON array as Java string. | ||
| */ | ||
| @SuppressWarnings("DuplicatedCode") | ||
| public static String toJson(String[] items) { | ||
| if (items == null) { | ||
| return "[]"; | ||
| } | ||
| try (JsonWriter writer = new JsonWriter()) { | ||
| writer.beginArray(); | ||
| for (String item : items) { | ||
| writer.value(item); | ||
| } | ||
| writer.endArray(); | ||
| return writer.toString(); | ||
| } | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
components/json/src/main/java/datadog/json/JsonStructure.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,56 @@ | ||
| package datadog.json; | ||
|
|
||
| /** The {@link JsonStructure} keeps track of JSON value being built. */ | ||
| interface JsonStructure { | ||
| /** | ||
| * Begins an object. | ||
| * | ||
| * @throws IllegalStateException if the object can not be started at this position. | ||
| */ | ||
| void beginObject(); | ||
|
|
||
| /** | ||
| * Checks whether the current position is within an object. | ||
| * | ||
| * @return {@code true} if the current position is within an object, {@code false} otherwise. | ||
| */ | ||
| boolean objectStarted(); | ||
|
|
||
| /** | ||
| * Ends the current object. | ||
| * | ||
| * @throws IllegalStateException if the current position is not within an object. | ||
| */ | ||
| void endObject(); | ||
|
|
||
| /** Begins an array. */ | ||
| void beginArray(); | ||
|
|
||
| /** | ||
| * Checks whether the current position is within an array. | ||
| * | ||
| * @return {@code true} if the current position is within an array, {@code false} otherwise. | ||
| */ | ||
| boolean arrayStarted(); | ||
PerfectSlayer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Ends the current array. | ||
| * | ||
| * @throws IllegalStateException if the current position is not within an array. | ||
| */ | ||
| void endArray(); | ||
|
|
||
| /** | ||
| * Adds a name to the current object. | ||
| * | ||
| * @throws IllegalStateException if the current position is not within an object. | ||
| */ | ||
| void addName(); | ||
|
|
||
| /** | ||
| * Adds a value. | ||
| * | ||
| * @throws IllegalStateException if the current position can not have a value. | ||
| */ | ||
| void addValue(); | ||
| } | ||
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.