use of com.couchbase.client.java.json.JsonObject in project couchbase-jvm-clients by couchbase.
the class SearchOptionsTest method canProvideCollections.
/**
* Makes sure that the list of collection (when provided) are turned into their correct JSON
* payload.
*/
@Test
void canProvideCollections() {
JsonObject output = JsonObject.create();
searchOptions().collections("a", "b").build().injectParams("idx", output, Duration.ofSeconds(1));
assertEquals(output.getArray("collections"), JsonArray.from("a", "b"));
}
use of com.couchbase.client.java.json.JsonObject 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);
}
}
use of com.couchbase.client.java.json.JsonObject in project couchbase-jvm-clients by couchbase.
the class SearchRowLocations method from.
/**
* Parses a FTS JSON representation of a {@link SearchRowLocations}.
*/
public static SearchRowLocations from(JsonObject locationsJson) {
SearchRowLocations hitLocations = new SearchRowLocations();
if (locationsJson == null) {
return hitLocations;
}
for (String field : locationsJson.getNames()) {
JsonObject termsJson = locationsJson.getObject(field);
for (String term : termsJson.getNames()) {
JsonArray locsJson = termsJson.getArray(term);
for (int i = 0; i < locsJson.size(); i++) {
JsonObject loc = locsJson.getObject(i);
long pos = loc.getLong("pos");
long start = loc.getLong("start");
long end = loc.getLong("end");
JsonArray arrayPositionsJson = loc.getArray("array_positions");
long[] arrayPositions = null;
if (arrayPositionsJson != null) {
arrayPositions = new long[arrayPositionsJson.size()];
for (int j = 0; j < arrayPositionsJson.size(); j++) {
arrayPositions[j] = arrayPositionsJson.getLong(j);
}
}
hitLocations.add(new SearchRowLocation(field, term, pos, start, end, arrayPositions));
}
}
}
return hitLocations;
}
use of com.couchbase.client.java.json.JsonObject in project couchbase-jvm-clients by couchbase.
the class MutateInUtilTest method convertsMultiDocList.
/**
* Tests more than one doc so they are concatenated with a "," per protocol spec.
*/
@Test
void convertsMultiDocList() {
JsonObject i1 = JsonObject.create().put("hello", "world");
String i2 = "What?";
boolean i3 = false;
byte[] result = MutateInUtil.convertDocsToBytes(Arrays.asList(i1, i2, i3), JSON_SERIALIZER);
assertArrayEquals("{\"hello\":\"world\"},\"What?\",false".getBytes(StandardCharsets.UTF_8), result);
}
use of com.couchbase.client.java.json.JsonObject in project couchbase-jvm-clients by couchbase.
the class QueryCollectionIntegrationTest method performsNonAdhocQuery.
@Test
void performsNonAdhocQuery() {
Scope scope = cluster.bucket(config().bucketname()).scope(SCOPE_NAME);
Collection collection = scope.collection(COLLECTION_NAME);
String id = insertDoc(collection);
QueryResult result = scope.query("select meta().id as id from `" + COLLECTION_NAME + "`", queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS).adhoc(false));
boolean hasDoc = false;
for (JsonObject row : result.rowsAsObject()) {
if (row.getString("id").equals(id)) {
hasDoc = true;
}
}
assertTrue(hasDoc);
}
Aggregations