diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/RawJsonValue.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/RawJsonValue.java
new file mode 100644
index 000000000000..576afaf64e87
--- /dev/null
+++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/RawJsonValue.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2004-2022, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.hisp.dhis.common;
+
+/** Wraps a raw JSON string so that serializers can emit it as inline JSON rather than a string. */
+public record RawJsonValue(String value) {}
diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializer.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializer.java
index 4508c6d667d6..8ca843091df4 100644
--- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializer.java
+++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializer.java
@@ -36,6 +36,7 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.List;
+import org.hisp.dhis.common.RawJsonValue;
/**
* TODO switch to jgen.writeObject( field )
@@ -54,7 +55,11 @@ public void serialize(List> values, JsonGenerator jgen, SerializerP
jgen.writeStartArray();
for (Object field : row) {
- jgen.writeString(field != null ? String.valueOf(maybeFormat(field)) : EMPTY);
+ if (field instanceof RawJsonValue json) {
+ jgen.writeRawValue(json.value());
+ } else {
+ jgen.writeString(field != null ? String.valueOf(maybeFormat(field)) : EMPTY);
+ }
}
jgen.writeEndArray();
diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/OutputFormatter.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/OutputFormatter.java
index 360a896deea0..471e76ca6d4c 100644
--- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/OutputFormatter.java
+++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/OutputFormatter.java
@@ -30,6 +30,7 @@
package org.hisp.dhis.common.adapter;
import java.math.BigDecimal;
+import org.hisp.dhis.common.RawJsonValue;
/**
* Simple component responsible do enforce specific output/format to types where this is required.
@@ -54,6 +55,10 @@ private OutputFormatter() {}
* or is null it will return the given parameter object itself
*/
public static Object maybeFormat(final Object object) {
+ if (object instanceof RawJsonValue json) {
+ return json.value();
+ }
+
if (object instanceof Double) {
return formatDouble((Double) object);
}
diff --git a/dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializerTest.java b/dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializerTest.java
new file mode 100644
index 000000000000..168be0a7769e
--- /dev/null
+++ b/dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializerTest.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2004-2022, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.hisp.dhis.common.adapter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.StringWriter;
+import java.util.List;
+import org.hisp.dhis.common.RawJsonValue;
+import org.junit.jupiter.api.Test;
+
+class JacksonRowDataSerializerTest {
+
+ private final JacksonRowDataSerializer serializer = new JacksonRowDataSerializer();
+
+ @Test
+ void regularStringValuesAreWrappedInJsonStrings() throws Exception {
+ List> rows = List.of(List.of("hello", "world"));
+
+ assertEquals("[[\"hello\",\"world\"]]", serialize(rows));
+ }
+
+ @Test
+ void rawJsonValueIsEmittedAsInlineJson() throws Exception {
+ List> rows = List.of(List.of("Alice", new RawJsonValue("{\"key\":\"value\"}")));
+
+ assertEquals("[[\"Alice\",{\"key\":\"value\"}]]", serialize(rows));
+ }
+
+ @Test
+ void rawJsonArrayIsEmittedAsInlineJsonArray() throws Exception {
+ List> rows = List.of(List.of(new RawJsonValue("[1,2,3]")));
+
+ assertEquals("[[[1,2,3]]]", serialize(rows));
+ }
+
+ @Test
+ void nullRawJsonValueIsEmittedAsEmptyString() throws Exception {
+ List