Skip to content

Commit ff855b1

Browse files
committed
feat(json): Add JSON component
# Conflicts: # dd-java-agent/agent-bootstrap/build.gradle
1 parent 7698f2f commit ff855b1

File tree

13 files changed

+1054
-0
lines changed

13 files changed

+1054
-0
lines changed

components/json/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apply(from = "$rootDir/gradle/java.gradle")
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package datadog.json;
2+
3+
import java.util.Collection;
4+
import java.util.Map;
5+
6+
/** Utility class for simple Java structure mapping into JSON strings. */
7+
public final class JsonMapper {
8+
9+
private JsonMapper() {}
10+
11+
/**
12+
* Converts a {@link String} to a JSON string.
13+
*
14+
* @param string The string to convert.
15+
* @return The converted JSON string.
16+
*/
17+
public static String toJson(String string) {
18+
if (string == null || string.isEmpty()) {
19+
return "";
20+
}
21+
try (JsonWriter writer = new JsonWriter()) {
22+
writer.value(string);
23+
return writer.toString();
24+
}
25+
}
26+
27+
/**
28+
* Converts a {@link Map} to a JSON object.
29+
*
30+
* @param map The map to convert.
31+
* @return The converted JSON object as Java string.
32+
*/
33+
public static String toJson(Map<String, ?> map) {
34+
if (map == null || map.isEmpty()) {
35+
return "{}";
36+
}
37+
try (JsonWriter writer = new JsonWriter()) {
38+
writer.beginObject();
39+
for (Map.Entry<String, ?> entry : map.entrySet()) {
40+
writer.name(entry.getKey());
41+
Object value = entry.getValue();
42+
if (value == null) {
43+
writer.nullValue();
44+
} else if (value instanceof String) {
45+
writer.value((String) value);
46+
} else if (value instanceof Double) {
47+
writer.value((Double) value);
48+
} else if (value instanceof Float) {
49+
writer.value((Float) value);
50+
} else if (value instanceof Long) {
51+
writer.value((Long) value);
52+
} else if (value instanceof Integer) {
53+
writer.value((Integer) value);
54+
} else if (value instanceof Boolean) {
55+
writer.value((Boolean) value);
56+
} else {
57+
writer.value(value.toString());
58+
}
59+
}
60+
writer.endObject();
61+
return writer.toString();
62+
}
63+
}
64+
65+
/**
66+
* Converts a {@link Iterable<String>} to a JSON array.
67+
*
68+
* @param items The iterable to convert.
69+
* @return The converted JSON array as Java string.
70+
*/
71+
@SuppressWarnings("DuplicatedCode")
72+
public static String toJson(Collection<String> items) {
73+
if (items == null || items.isEmpty()) {
74+
return "[]";
75+
}
76+
try (JsonWriter writer = new JsonWriter()) {
77+
writer.beginArray();
78+
for (String item : items) {
79+
writer.value(item);
80+
}
81+
writer.endArray();
82+
return writer.toString();
83+
}
84+
}
85+
86+
/**
87+
* Converts a String array to a JSON array.
88+
*
89+
* @param items The array to convert.
90+
* @return The converted JSON array as Java string.
91+
*/
92+
@SuppressWarnings("DuplicatedCode")
93+
public static String toJson(String[] items) {
94+
if (items == null) {
95+
return "[]";
96+
}
97+
try (JsonWriter writer = new JsonWriter()) {
98+
writer.beginArray();
99+
for (String item : items) {
100+
writer.value(item);
101+
}
102+
writer.endArray();
103+
return writer.toString();
104+
}
105+
}
106+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package datadog.json;
2+
3+
/** The {@link JsonStructure} keeps track of JSON value being built. */
4+
interface JsonStructure {
5+
/**
6+
* Begins an object.
7+
*
8+
* @throws IllegalStateException if the object can not be started at this position.
9+
*/
10+
void beginObject();
11+
12+
/**
13+
* Checks whether the current position is within an object.
14+
*
15+
* @return {@code true} if the current position is within an object, {@code false} otherwise.
16+
*/
17+
boolean objectStarted();
18+
19+
/**
20+
* Ends the current object.
21+
*
22+
* @throws IllegalStateException if the current position is not within an object.
23+
*/
24+
void endObject();
25+
26+
/** Begins an array. */
27+
void beginArray();
28+
29+
/**
30+
* Checks whether the current position is within an array.
31+
*
32+
* @return {@code true} if the current position is within an array, {@code false} otherwise.
33+
*/
34+
boolean arrayStarted();
35+
36+
/**
37+
* Ends the current array.
38+
*
39+
* @throws IllegalStateException if the current position is not within an array.
40+
*/
41+
void endArray();
42+
43+
/**
44+
* Adds a name to the current object.
45+
*
46+
* @throws IllegalStateException if the current position is not within an object.
47+
*/
48+
void addName();
49+
50+
/**
51+
* Adds a value.
52+
*
53+
* @throws IllegalStateException if the current position can not have a value.
54+
*/
55+
void addValue();
56+
}

0 commit comments

Comments
 (0)