Skip to content

Commit b1e4eef

Browse files
authored
docs: fix the serialization issue (#7562)
1 parent 551e504 commit b1e4eef

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

aiplatform/src/main/java/aiplatform/PredictTextSentimentAnalysisSample.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.aiplatform.v1.PredictResponse;
2323
import com.google.cloud.aiplatform.v1.PredictionServiceClient;
2424
import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
25+
import com.google.gson.JsonObject;
2526
import com.google.protobuf.Value;
2627
import com.google.protobuf.util.JsonFormat;
2728
import java.io.IOException;
@@ -52,13 +53,16 @@ static void predictTextSentimentAnalysis(String project, String content, String
5253
try (PredictionServiceClient predictionServiceClient =
5354
PredictionServiceClient.create(predictionServiceSettings)) {
5455
String location = "us-central1";
55-
String jsonString = "{\"content\": \"" + content + "\"}";
56+
57+
// Use JsonObject to ensure safe serialization of the content; handles characters like `"`.
58+
JsonObject contentJsonObject = new JsonObject();
59+
contentJsonObject.addProperty("content", content);
5660

5761
EndpointName endpointName = EndpointName.of(project, location, endpointId);
5862

5963
Value parameter = Value.newBuilder().setNumberValue(0).setNumberValue(5).build();
6064
Value.Builder instance = Value.newBuilder();
61-
JsonFormat.parser().merge(jsonString, instance);
65+
JsonFormat.parser().merge(contentJsonObject.toString(), instance);
6266

6367
List<Value> instances = new ArrayList<>();
6468
instances.add(instance.build());

0 commit comments

Comments
 (0)