From 0b79ba9493a57ac0b852cd60f9cad8ce275b8687 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Tue, 10 Sep 2013 22:31:51 +0200 Subject: [PATCH] Rewrote SuggestResponse#toString method It only prints out json now (as the SearchResponse does) Added missing startObject & endObject (was causing JsonGenerationException) Closes #3661 --- .../action/suggest/SuggestResponse.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/suggest/SuggestResponse.java b/src/main/java/org/elasticsearch/action/suggest/SuggestResponse.java index 7abe4417513..fa3546ae11c 100644 --- a/src/main/java/org/elasticsearch/action/suggest/SuggestResponse.java +++ b/src/main/java/org/elasticsearch/action/suggest/SuggestResponse.java @@ -19,20 +19,19 @@ package org.elasticsearch.action.suggest; -import java.io.IOException; -import java.util.List; - import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.XContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.suggest.Suggest; +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS; + /** * The response of the suggest action. */ @@ -67,17 +66,17 @@ public final class SuggestResponse extends BroadcastOperationResponse { super.writeTo(out); this.suggest.writeTo(out); } - + @Override public String toString() { - String source; try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - suggest.toXContent(builder, null); - source = XContentHelper.convertToJson(builder.bytes(), true); + XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); + builder.startObject(); + suggest.toXContent(builder, EMPTY_PARAMS); + builder.endObject(); + return builder.string(); } catch (IOException e) { - source = "Error: " + e.getMessage(); + return "{ \"error\" : \"" + e.getMessage() + "\"}"; } - return "Suggest Response["+source+"]"; } }