Skip to content

Commit

Permalink
Add JSON component (#7973)
Browse files Browse the repository at this point in the history
* feat(json): Add JSON component
* feat(internal-api): Remove Strings toJson and escapeToJson methods
* feat(ci-visibility): Migrate to JSON component
* feat(junit-4.10): Migrate to JSON component
* feat(junit-5.3): Migrate to JSON component
* feat(karate): Migrate to JSON component
* feat(testng): Migrate to JSON component
* feat(bootstrap): Migrate to JSON component
* feat(json): Improve structure performance
  • Loading branch information
PerfectSlayer authored Dec 4, 2024
1 parent d2c8a74 commit 686f51a
Show file tree
Hide file tree
Showing 41 changed files with 1,291 additions and 699 deletions.
9 changes: 9 additions & 0 deletions components/json/build.gradle.kts
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 components/json/src/jmh/java/datadog/json/JsonWriterBenchmark.java
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 components/json/src/main/java/datadog/json/JsonMapper.java
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 components/json/src/main/java/datadog/json/JsonStructure.java
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();

/**
* 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();
}
Loading

0 comments on commit 686f51a

Please sign in to comment.