use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.
the class JsonSerializerTestBase method encodesJsonArray.
@Test
void encodesJsonArray() {
JsonArray input = JsonArray.from("1", true, 2);
byte[] output = serializer().serialize(input);
assertEquals("[\"1\",true,2]", new String(output, UTF_8));
}
use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.
the class JsonSerializerTestBase method decodesJsonArray.
@Test
void decodesJsonArray() {
byte[] input = "[\"1\",true,2]".getBytes(UTF_8);
JsonArray decoded = serializer().deserialize(JsonArray.class, input);
assertEquals(JsonArray.fromJson(input), decoded);
JsonArray decodedWithTypeRef = serializer().deserialize(new TypeRef<JsonArray>() {
}, input);
assertEquals(decoded, decodedWithTypeRef);
}
use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.
the class JsonTranscoderTest method encodesJsonArray.
@Test
void encodesJsonArray() {
JsonArray input = JsonArray.from("1", true, 2);
Transcoder.EncodedValue output = JSON_TRANSCODER.encode(input);
assertEquals("[\"1\",true,2]", new String(output.encoded(), StandardCharsets.UTF_8));
}
use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.
the class ViewOptionsTest method shouldStoreKeysAsJsonOutsideParams.
@Test
void shouldStoreKeysAsJsonOutsideParams() {
JsonArray keys = JsonArray.create().add("1").add("2").add("3");
ViewOptions options = viewOptions();
assertNull(options.build().keys());
options.keys(keys);
assertEquals(JsonObject.create().put("keys", keys).toString(), options.build().keys());
assertFalse(options.export().contains("keys="));
assertFalse(options.export().contains("3"));
}
use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.
the class SearchRow method fromResponse.
public static SearchRow fromResponse(final SearchChunkRow row, final JsonSerializer serializer) {
try {
JsonObject hit = JacksonTransformers.MAPPER.readValue(row.data(), JsonObject.class);
String index = hit.getString("index");
String id = hit.getString("id");
double score = hit.getDouble("score");
JsonObject explanationJson = defaultIfNull(hit.getObject("explanation"), JsonObject::create);
Optional<SearchRowLocations> locations = Optional.ofNullable(hit.getObject("locations")).map(SearchRowLocations::from);
JsonObject fragmentsJson = hit.getObject("fragments");
Map<String, List<String>> fragments;
if (fragmentsJson != null) {
fragments = new HashMap<>(fragmentsJson.size());
for (String field : fragmentsJson.getNames()) {
List<String> fragment;
JsonArray fragmentJson = fragmentsJson.getArray(field);
if (fragmentJson != null) {
fragment = new ArrayList<>(fragmentJson.size());
for (int i = 0; i < fragmentJson.size(); i++) {
fragment.add(fragmentJson.getString(i));
}
} else {
fragment = Collections.emptyList();
}
fragments.put(field, fragment);
}
} else {
fragments = Collections.emptyMap();
}
byte[] fields = null;
if (hit.containsKey("fields")) {
// daschl: this is a bit wasteful and should be streamlined
fields = JacksonTransformers.MAPPER.writeValueAsBytes(hit.getObject("fields").toMap());
}
return new SearchRow(index, id, score, explanationJson, locations, fragments, fields, serializer);
} catch (IOException e) {
throw new DecodingFailureException("Failed to decode row '" + new String(row.data(), UTF_8) + "'", e);
}
}
Aggregations