Skip to content

Commit 78ba573

Browse files
authored
Fix a regression for parsing JSON field values returned by search/query (#1351)
Signed-off-by: yhmo <[email protected]>
1 parent d1c94b3 commit 78ba573

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

examples/src/main/java/io/milvus/v1/BulkWriterExample.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ public static void main(String[] args) throws Exception {
169169
exampleBulkWriter.createConnection();
170170

171171
List<BulkFileType> fileTypes = Lists.newArrayList(
172+
BulkFileType.JSON,
173+
BulkFileType.CSV,
172174
BulkFileType.PARQUET
173175
);
174176

@@ -387,7 +389,11 @@ private List<List<String>> allTypesRemoteWriter(CollectionSchemaParam collection
387389
rowObject.addProperty("float", i / 3);
388390
rowObject.addProperty("double", i / 7);
389391
rowObject.addProperty("varchar", "varchar_" + i);
390-
rowObject.addProperty("json", String.format("{\"dummy\": %s, \"ok\": \"name_%s\"}", i, i));
392+
393+
// Note: for JSON field, use gson.fromJson() to construct a real JsonObject
394+
// don't use rowObject.addProperty("json", jsonContent) since the value is treated as a string, not a JsonObject
395+
String jsonContent = String.format("{\"dummy\": %s, \"ok\": \"name_%s\"}", i, i);
396+
rowObject.add("json", GSON_INSTANCE.fromJson(jsonContent, JsonElement.class));
391397

392398
// vector field
393399
rowObject.add("float_vector", GSON_INSTANCE.toJsonTree(CommonUtils.generateFloatVector(DIM)));

examples/src/main/java/io/milvus/v1/JsonFieldExample.java

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public static void main(String[] args) {
130130
row.addProperty(ID_FIELD, i);
131131
row.add(VECTOR_FIELD, gson.toJsonTree(CommonUtils.generateFloatVector(VECTOR_DIM)));
132132

133+
// Note: for JSON field, always construct a real JsonObject
134+
// don't use row.addProperty(JSON_FIELD, strContent) since the value is treated as a string, not a JsonObject
133135
JsonObject metadata = new JsonObject();
134136
metadata.addProperty("path", String.format("\\root/abc/path%d", i));
135137
metadata.addProperty("size", i);

examples/src/main/java/io/milvus/v2/BulkWriterExample.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,11 @@ private static List<JsonObject> genImportData(List<Map<String, Object>> original
441441
rowObject.addProperty("double", (Number) row.get("double"));
442442
}
443443
rowObject.addProperty("varchar", row.get("varchar") == null ? null : (String) row.get("varchar"));
444-
rowObject.addProperty("json", row.get("json") == null ? null : (String) row.get("json"));
444+
445+
// Note: for JSON field, use gson.fromJson() to construct a real JsonObject
446+
// don't use rowObject.addProperty("json", jsonContent) since the value is treated as a string, not a JsonObject
447+
Object jsonContent = row.get("json");
448+
rowObject.add("json", jsonContent == null ? null : GSON_INSTANCE.fromJson((String)jsonContent, JsonElement.class));
445449

446450
// vector field
447451
rowObject.add("float_vector", GSON_INSTANCE.toJsonTree(row.get("float_vector")));
@@ -720,8 +724,8 @@ private static void comparePrint(CreateCollectionReq.CollectionSchema collection
720724
} else if (fetchedValue instanceof Double) {
721725
matched = Math.abs((Double)fetchedValue - (Double)expectedValue) < 1e-8;
722726
} else if (fetchedValue instanceof JsonElement) {
723-
String ss = fetchedValue.toString();
724-
matched = ss.equals(((String)expectedValue).replaceAll("\\s", "")); // compare ignore space
727+
JsonElement expectedJson = GSON_INSTANCE.fromJson((String)expectedValue, JsonElement.class);
728+
matched = fetchedValue.equals(expectedJson);
725729
} else if (fetchedValue instanceof ByteBuffer) {
726730
byte[] bb = ((ByteBuffer)fetchedValue).array();
727731
matched = Arrays.equals(bb, (byte[])expectedValue);

examples/src/main/java/io/milvus/v2/JsonFieldExample.java

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public static void main(String[] args) {
109109
row.addProperty(ID_FIELD, i);
110110
row.add(VECTOR_FIELD, gson.toJsonTree(CommonUtils.generateFloatVector(VECTOR_DIM)));
111111

112+
// Note: for JSON field, always construct a real JsonObject
113+
// don't use row.addProperty(JSON_FIELD, strContent) since the value is treated as a string, not a JsonObject
112114
JsonObject metadata = new JsonObject();
113115
metadata.addProperty("path", String.format("\\root/abc/path%d", i));
114116
metadata.addProperty("size", i);

sdk-core/src/main/java/io/milvus/response/FieldDataWrapper.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,7 @@ private JsonElement parseObjectData(int index) {
386386

387387
public static JsonElement ParseJSONObject(Object object) {
388388
if (object instanceof String) {
389-
// For JSON field, milvus server returns a string value with redundant escape character
390-
// and the string value is wrapped by a pair of quotations, the JsonParser.parseString() will
391-
// parse it as a JsonPrimitive, not a JsonObject.
392-
// Here we convert the string value to a valid JSON string so that
393-
// JsonParser.parseString() can parse it to be JsonObject.
394-
String ss = ((String)object).replace("\\\"", "\"").replaceAll("^\"|\"$", "");
395-
return JsonParser.parseString(ss);
389+
return JsonParser.parseString((String)object);
396390
} else if (object instanceof byte[]) {
397391
return JsonParser.parseString(new String((byte[]) object));
398392
} else {

0 commit comments

Comments
 (0)