|
22 | 22 | import com.google.cloud.aiplatform.v1.PredictResponse; |
23 | 23 | import com.google.cloud.aiplatform.v1.PredictionServiceClient; |
24 | 24 | import com.google.cloud.aiplatform.v1.PredictionServiceSettings; |
| 25 | +import com.google.gson.JsonObject; |
25 | 26 | import com.google.protobuf.Value; |
26 | 27 | import com.google.protobuf.util.JsonFormat; |
27 | 28 | import java.io.IOException; |
@@ -52,13 +53,16 @@ static void predictTextSentimentAnalysis(String project, String content, String |
52 | 53 | try (PredictionServiceClient predictionServiceClient = |
53 | 54 | PredictionServiceClient.create(predictionServiceSettings)) { |
54 | 55 | 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); |
56 | 60 |
|
57 | 61 | EndpointName endpointName = EndpointName.of(project, location, endpointId); |
58 | 62 |
|
59 | 63 | Value parameter = Value.newBuilder().setNumberValue(0).setNumberValue(5).build(); |
60 | 64 | Value.Builder instance = Value.newBuilder(); |
61 | | - JsonFormat.parser().merge(jsonString, instance); |
| 65 | + JsonFormat.parser().merge(contentJsonObject.toString(), instance); |
62 | 66 |
|
63 | 67 | List<Value> instances = new ArrayList<>(); |
64 | 68 | instances.add(instance.build()); |
|
0 commit comments